1
votes

I've been converting HTML + CSS to PDF in server-side Java, e.g., Flying Saucer (http://code.google.com/p/flying-saucer/)

Now I need to look at alternative approaches with the growth in client-side visualization APIs like RaphaelJS, that should also be included in PDF exports.

Ideally, I would just have a print-friendly layout and CSS (kind of like Google maps does) and there would be some magic way that a Javascript call could silently "print" the window to a PDF file, rather than having a separate PDF generation pipeline.

Is there any such thing?

If not, what other approaches would you recommend?

For example

  • run Raphael server-side with node.js or Rhino, use Batik to convert SVG output to image, render PDF server-side
  • same thing but do some hack to grab SVG from the work that Raphael already did client-side
  • Do a headless-browser on the server, like wkhtmltopdf or phantomjs, to capture and execute the generated HTML/JS server side.

Any success with any of those approaches or other suggestions?

2
you might want to look at [zing][1] or you can use java and batik to convert svg to pdf directly - see [here][2] - don't know if they have a javascript verison. [1]: zingchart.com/reference/faq-image-export.html [2]: thinktibits.blogspot.ie/2012/12/…Rachel Gallen

2 Answers

1
votes

Those two options worked for me:

  • Client-side PNG generation with html2canvas (only works where canvas is supported)

  • Server-side PDF or PNG generation, with wkhtmltopdf (you might need xvfb)

Please ask if you need more details about how to use those solutions.

1
votes

I set up a (very) simple test of both PhantomJS and CasperJS in order to be able to print a PDF server-side with a headless browser. It sure needs additionnal non-Java tools but I think this approach is more future-proof. In those exemples, I am able to dynamically load a web page, let angular initialize and then interact with it with Javascript code in order to print the page as a user would have modify it in a real browser.

Here are my examples: https://github.com/fmarot/miscPOC/tree/master/angularAppOutputPDF