Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @POST
- @Path("createPdf")
- public Response createPdf(String htmlTemplateParam, String json) {
- //Read template from endpoint param or start off with reading local html template from resource folder
- String templateHtml = IOUtils.toString(getClass().getResourceAsStream(HTMLTemplateFiles.INCOMING_TEMPLATE));
- RequestJson requestJson = [Prepare json before passing to HTML Template]];
- if(requestJson.isContentValid()) {
- LOG.info("incoming data successfully validated");
- // TODO
- // Pass the requestJson (Endpoint Param JSON ) to templateHtml
- // Trigger the reading of the Json data and populating different HTML DOM elements using some sort of expression predifined in HTML
- // Get hold of the rendered HTML
- String resolvedHtml = [HTML with data from json param into endpoint];
- // The next bit is done
- String pdf = htmlToPdfaHandler.generatePdfFromHtml(resolvedHtml);
- javax.ws.rs.core.Response response = Response.ok().entity(Base64.decodeBase64(pdf)).build();
- }
- }
- Configuration cfg = new Configuration(new Version("2.3.23"));
- cfg.setDefaultEncoding("UTF-8");
- // Loading you HTML template (via file or input stream):
- Template template = cfg.getTemplate("template.html");
- // You need convert json to map of parameters (key-value):
- Map<String, Object> templateData = new HashMap<>();
- templateData.put("msg", "Today is a beautiful day");
- try (StringWriter out = new StringWriter()) {
- // In output stream the result will be template with values from map:
- template.process(templateData, out);
- System.out.println(out.getBuffer().toString());
- out.flush();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement