Dynamic HTML to PDF

vdh_ant picture vdh_ant · Feb 25, 2010 · Viewed 8.8k times · Source

I need to be able to convert dynamic HTML (html that is rendered on page load by javascript) to a PDF. I know there are plenty of HTML to PDF converters but none of the ones I have found thus far cope with dynamic HTML.

The given tool should be able to successfully convert the following page - http://www.simile-widgets.org/timeline/

Cheers Anthony

UPDATE:

I don't need the JavaScript functionality here... i.e. i don't need to be able to interact screen... I just want the finial rendering of the screen to be captured in the PDF - like taking a photo after the page is loaded. And in the example I provided the javascript is only rendering divs to the screen so its nothing that it shouldn't be able to handle as long as it "lets" the "page" render first.

Answer

bobince picture bobince · Feb 25, 2010

There is no way it can be done. The interfaces available for scripts in PDF are extremely limited compared to the full DOM and BOM access you enjoy in a web browser. Such interaction as you can achieve in PDF is not readily translatable from how it works in a browser and would almost certainly need hand authoring.

Your example page has many effects that PDF, as an essentially static document layout format, simply cannot reproduce at all.

Edit:

I just want the finial rendering of the screen to be captured in the PDF

Ah, OK, that's a far easier and more common problem then.

In that case you'll have to use and automate a real web browser (like Firefox), or a toolkit that provides all the logic of a web browser (like WebKit), then either:

  • export to PDF, either using built-in tools like ‘Print to file’ in Firefox (with background images/colours turned on) or one of the PDF export add-ons, or

  • take a image snapsnot of the browser (and include the image in a PDF if you have to)

See these questions for some discussion of browser snapshotting.