5

Is it possible to create a PDF out of JavaFX Webview, with the help of a PDF library maybe?

I know that the webview supports printing but would it be possible to print to a pdf without one of the typical pdf print drivers available on the host system.

Vad1mo
  • 5,156
  • 6
  • 36
  • 65
  • Have a look at http://stackoverflow.com/questions/633780/converting-html-files-to-pdf. It should be possible to extract the HTML from the document (`webView.getEngine().getDocument()`) and pass it to iText to convert to a PDF. – James_D Oct 05 '14 at 17:41
  • @James this will use iTexts html render engine and not webkit. the result may be different from what the user sees and what he gets. – Vad1mo Oct 05 '14 at 17:49
  • Yes, true: though typically I think a user would expect the print appearance of a web page to be substantially different to the screen appearance. But I can see that may not be what you want. So another approach might be to call `snapshot` on the web view, convert the resulting JavaFX image to an iText image (via an AWT image), and add that to an iText PdfDocument. You will need to take some steps to ensure you snapshot the whole page (not just the screen-visible portion) and probably do some pagination. – James_D Oct 05 '14 at 17:57
  • Agree a user expects the printout to be different but if you try to print out a html5 + JS + Bootstap page with iText the result might be far away from what a user would expect & accept. Making a ```snapshot``` is not much of a PDF then, just an image PDFized but it is solution :) – Vad1mo Oct 06 '14 at 07:09

1 Answers1

0

I was unable to find pure java solution to this. I prefer to use PhantomJS for non Java projects and I think it would fit nicely with WebView too (as it is also WebKit based).

Davor Hrg
  • 187
  • 1
  • 11