Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. @POST
  2. @Path("createPdf")
  3. public Response createPdf(String htmlTemplateParam, String json) {
  4.  
  5. //Read template from endpoint param or start off with reading local html template from resource folder
  6. String templateHtml = IOUtils.toString(getClass().getResourceAsStream(HTMLTemplateFiles.INCOMING_TEMPLATE));
  7.  
  8. RequestJson requestJson = [Prepare json before passing to HTML Template]];
  9.  
  10. if(requestJson.isContentValid()) {
  11.  
  12. LOG.info("incoming data successfully validated");
  13.  
  14. // TODO
  15. // Pass the requestJson (Endpoint Param JSON ) to templateHtml
  16. // Trigger the reading of the Json data and populating different HTML DOM elements using some sort of expression predifined in HTML
  17.  
  18. // Get hold of the rendered HTML
  19. String resolvedHtml = [HTML with data from json param into endpoint];
  20.  
  21. // The next bit is done
  22. String pdf = htmlToPdfaHandler.generatePdfFromHtml(resolvedHtml);
  23.  
  24. javax.ws.rs.core.Response response = Response.ok().entity(Base64.decodeBase64(pdf)).build();
  25. }
  26. }
  27.  
  28. Configuration cfg = new Configuration(new Version("2.3.23"));
  29. cfg.setDefaultEncoding("UTF-8");
  30.  
  31. // Loading you HTML template (via file or input stream):
  32. Template template = cfg.getTemplate("template.html");
  33.  
  34. // You need convert json to map of parameters (key-value):
  35. Map<String, Object> templateData = new HashMap<>();
  36. templateData.put("msg", "Today is a beautiful day");
  37.  
  38. try (StringWriter out = new StringWriter()) {
  39. // In output stream the result will be template with values from map:
  40. template.process(templateData, out);
  41. System.out.println(out.getBuffer().toString());
  42.  
  43. out.flush();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement