1

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?

wrschneider
  • 17,913
  • 16
  • 96
  • 176
  • 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]: http://www.zingchart.com/reference/faq-image-export.html [2]: http://thinktibits.blogspot.ie/2012/12/batik-convert-svg-to-pdf-java-example.html – Rachel Gallen Jan 28 '13 at 18:33

2 Answers2

1

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.

Community
  • 1
  • 1
Mathieu Rodic
  • 6,637
  • 2
  • 43
  • 49
  • wkhtmltopdf looks like a viable option. Is that better than phantomjs? – wrschneider Feb 11 '13 at 15:31
  • 1
    I found it much easier to use; you just have to execute one command line to get your PDF (or PNG) in the desired folder. And if the page you have to display is session-dependent, it is possible to add cookies. – Mathieu Rodic Feb 12 '13 at 08:53
1

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

Francois Marot
  • 1,145
  • 11
  • 18