Play Framework: create a PDF from a template that uses Javascript for graphic display

I have a template that has Javascript used to generate graphs in a browser. I would like to use the same template to create a PDF file and send as an attachment in an email. In this case, there will be no interaction with the browser and client.

I am using the PDF module available on the Play website, and I was able to get the PDF rendering to work. The only problem is that the graphics are not displayed in PDF, but all other static texts. I assume that the graphics do not appear in the PDF due to the fact that Javascript is not executed before the creation of the PDF.

Does anyone have any ideas on how to get around this problem?

+2
source share
3 answers

Since you are using the Play platform, the easiest way is to use Rhino. http://www.mozilla.org/rhino/ This is a Javascript Mozilla implementation of the JVM that runs the playback environment. You may need to make some changes to Javascript, for example, if it uses the Canvas API browser, you will need to make a facade object that sends these drawing commands to the PDF drawing object instead of the screen. Or in both places, if that's what you want.

You can get a more detailed answer if you provide more information about Javascript graphic code.

+1
source

If you do not find the best options, consider using itext directly. This is a PDF rendering library that is used in the pdf plugin. You will have to manually rewrite the template in terms of generating PDF, but you will get full control over the result.

0
source

I don’t know if this will help you, but this is what worked for me very quickly.

I turned the canvas into PNG with:

var datastring = document.getElementById('myCanvas').toDataURL("image/png"); 

Then I sent this date to the server so that Play would generate the PDF file and I would pass the variable to the Generator PDF

 public static void reportPDF(String graphData){ PDF.renderPDF(graphData); } 

My pdf code is as follows:

  <body> <img width="100%" src="${graphData}"/> </body> 
0
source

Source: https://habr.com/ru/post/903744/


All Articles