Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String generatePdf(String id, JSONObject data, String userEmail, String userFirstName, String userLastName) {
- PageProperties pageProperties = new PageProperties(pearsonLogo,headingFont, userFirstName, userLastName);
- try {
- Document document = new Document(PageSize.A4, Constants.PdfProperties.MARGIN_LEFT, Constants.PdfProperties.MARGIN_RIGHT, Constants.PdfProperties.MARGIN_TOP, Constants.PdfProperties.MARGIN_BOTTOM);
- BaseFont pFont = BaseFont.createFont(documentFont,"CP1251", BaseFont.EMBEDDED);
- Font font = new Font(pFont);
- Font pearsonMessageFont = new Font(pFont);
- pearsonMessageFont.setStyle(Font.BOLD);
- pearsonMessageFont.setSize(Constants.PdfProperties.FONTSIZE_SMALL);
- String stringValue = data.toString();
- Object objectValue = mapper.readValue(stringValue, Object.class);
- String prettyString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectValue);
- String pdfContent = prettyString.replaceAll("\\\\n", "\n")
- .replaceAll("[-+.^,{}()\"\'\\[\\]]","")
- .replaceAll("( {1,}\\r\n){1,}","\n");
- PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(id + Constants.EXTENTION_PDF));
- pdfwriter.setPageEvent(pageProperties);
- document.open();
- document.add(new Paragraph(" "));
- String disclaimer = String.format(Constants.DISCLAIMER,dateFormat.format(date), userEmail);
- Paragraph pearsonMessage = new Paragraph(disclaimer,pearsonMessageFont);
- pearsonMessage.setAlignment(Element.ALIGN_JUSTIFIED);
- pearsonMessage.setIndentationLeft(Constants.PdfProperties.PARAGRAPH_INDENTATION_LEFT);
- pearsonMessage.setIndentationRight(Constants.PdfProperties.PARAGRAPH_INDENTATION_RIGHT);
- pageProperties.setActive(true);
- document.add(pearsonMessage);
- pageProperties.setActive(false);
- document.add(new Paragraph(pdfContent, font));
- document.close();
- FileInputStream inputPdf = new FileInputStream(id + Constants.EXTENTION_PDF);
- return awsS3Service.uploadFile(inputPdf, id);
- }catch (IOException | DocumentException ex) {
- LOGGER.error("Exception on Pdf generation - { }", ex);
- throw new PdfGenerationException("Pdf Generation Failed");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement