Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. public String generatePdf(String id, JSONObject data, String userEmail, String userFirstName, String userLastName) {
  2.  
  3. PageProperties pageProperties = new PageProperties(pearsonLogo,headingFont, userFirstName, userLastName);
  4. try {
  5. Document document = new Document(PageSize.A4, Constants.PdfProperties.MARGIN_LEFT, Constants.PdfProperties.MARGIN_RIGHT, Constants.PdfProperties.MARGIN_TOP, Constants.PdfProperties.MARGIN_BOTTOM);
  6.  
  7. BaseFont pFont = BaseFont.createFont(documentFont,"CP1251", BaseFont.EMBEDDED);
  8. Font font = new Font(pFont);
  9. Font pearsonMessageFont = new Font(pFont);
  10. pearsonMessageFont.setStyle(Font.BOLD);
  11. pearsonMessageFont.setSize(Constants.PdfProperties.FONTSIZE_SMALL);
  12.  
  13. String stringValue = data.toString();
  14. Object objectValue = mapper.readValue(stringValue, Object.class);
  15. String prettyString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectValue);
  16. String pdfContent = prettyString.replaceAll("\\\\n", "\n")
  17. .replaceAll("[-+.^,{}()\"\'\\[\\]]","")
  18. .replaceAll("( {1,}\\r\n){1,}","\n");
  19.  
  20. PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(id + Constants.EXTENTION_PDF));
  21. pdfwriter.setPageEvent(pageProperties);
  22. document.open();
  23. document.add(new Paragraph(" "));
  24. String disclaimer = String.format(Constants.DISCLAIMER,dateFormat.format(date), userEmail);
  25. Paragraph pearsonMessage = new Paragraph(disclaimer,pearsonMessageFont);
  26. pearsonMessage.setAlignment(Element.ALIGN_JUSTIFIED);
  27. pearsonMessage.setIndentationLeft(Constants.PdfProperties.PARAGRAPH_INDENTATION_LEFT);
  28. pearsonMessage.setIndentationRight(Constants.PdfProperties.PARAGRAPH_INDENTATION_RIGHT);
  29. pageProperties.setActive(true);
  30. document.add(pearsonMessage);
  31. pageProperties.setActive(false);
  32. document.add(new Paragraph(pdfContent, font));
  33. document.close();
  34. FileInputStream inputPdf = new FileInputStream(id + Constants.EXTENTION_PDF);
  35. return awsS3Service.uploadFile(inputPdf, id);
  36. }catch (IOException | DocumentException ex) {
  37. LOGGER.error("Exception on Pdf generation - { }", ex);
  38. throw new PdfGenerationException("Pdf Generation Failed");
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement