package at.gv.egiz.simpleSigning.helper; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.lowagie.text.Document; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; import at.gv.egiz.simpleSigning.ErrorSignature; public class PDFHelper { private static final Logger logger = LoggerFactory .getLogger(PDFHelper.class); private static byte[] createPDFiText(String text) { try { Document document = new Document(PageSize.A4, 36, 72, 108, 180); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document,baos); document.open(); document.add(new Paragraph(text)); System.out.println("Text is inserted into pdf file"); document.close(); baos.close(); return baos.toByteArray(); } catch(Throwable e) { logger.error("Failed to create PDF", e); } return null; } /* private static byte[] createPDFPdfBox(String text) { PDDocument document = null; try { // Create a document and add a page to it document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); PDFont pdfFont = PDType1Font.HELVETICA; float fontSize = 25; float leading = 1.5f * fontSize; PDRectangle mediabox = page.findMediaBox(); float margin = 72; float width = mediabox.getWidth() - 2*margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; List lines = new ArrayList(); int lastSpace = -1; while (text.length() > 0) { int spaceIndex = text.indexOf(' ', lastSpace + 1); if (spaceIndex < 0) { lines.add(text); text = ""; } else { String subString = text.substring(0, spaceIndex); float size = fontSize * pdfFont.getStringWidth(subString) / 1000; if (size > width) { if (lastSpace < 0) // So we have a word longer than the line... draw it anyways lastSpace = spaceIndex; subString = text.substring(0, lastSpace); lines.add(subString); text = text.substring(lastSpace).trim(); lastSpace = -1; } else { lastSpace = spaceIndex; } } } contentStream.beginText(); contentStream.setFont(pdfFont, fontSize); contentStream.moveTextPositionByAmount(startX, startY); for (String line: lines) { contentStream.drawString(line); contentStream.moveTextPositionByAmount(0, -leading); } contentStream.endText(); // Make sure that the content stream is closed: contentStream.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Save the results and ensure that the document is properly closed: document.save(baos); return baos.toByteArray(); } catch (Throwable e) { logger.error("Failed to create PDF", e); } finally { if(document != null) { try { document.close(); } catch (Throwable e) { logger.error("Failed to close PDF", e); } } } return null; } */ public static byte[] createPDFDocument(String text) { return createPDFiText(text); } public static void toError(HttpServletRequest req, HttpServletResponse resp, String error, String cause) throws ServletException, IOException { req.setAttribute(ErrorSignature.PARAM_ERROR, error); req.setAttribute(ErrorSignature.PARAM_CAUSE, cause); req.getRequestDispatcher("Error").forward(req, resp); } }