Dynamic HTML to PDF [closed]
Asked Answered
H

6

7

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.

Hardpressed answered 25/2, 2010 at 1:7 Comment(3)
Because there are so many questions similar to this one but not quite the same, I decided to try to collect a complete list of HTML to PDF converters into a community wiki question #3178948Adin
@Adin - that sounded really useful, but the page has gone - did you manage to post the list anywhere else?Rivi
Did you find an answer?Rivi
L
1

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.

Lungan answered 25/2, 2010 at 2:0 Comment(1)
Not true. Flash can be added to pdf files now. However I am not aware of a javascript to actionscript/flash conversion path. :)Ethelyn
R
1

The fact that it uses any JavaScript at all means a lot of converters won't work. The JavaScript may be simple, but you still need an interpreter to handle it.

I haven't used it for myself, but you might try wkhtmltopdf. It uses the webkit rendering engine, and I believe it includes full javascript support. You would need to be able to install the software and run the executable, but otherwise it should be fairly straightforward.

Royster answered 25/2, 2010 at 4:47 Comment(0)
P
0

You could use a javascript URI to alert the current DOM. eg:

javascript:alert("<html>" + document.documentElement.innerHTML + "</html>")

Copy the HTML and save to a file.
Then run it through the HTML2PDF converter.

Penelope answered 25/2, 2010 at 1:42 Comment(2)
It needs to be an automated process... i.e. the user clicks a button and they can download a report...Hardpressed
If you mean a button in the page (rather than the browser) then you obviously control the site so you can use XMLHttpRequest to POST the HTML to the server and run it through the converter on the server.Penelope
F
0

dynamic-html-pdf

This is best library for node js convert dynamic html to pdf.

https://www.npmjs.com/package/dynamic-html-pdf

Forwarding answered 26/5, 2021 at 13:31 Comment(1)
Are you sure, it's not only called "dynamic" because of it's handlebars templates? Just because server-side javascript (node) is executed, it doesn't mean, client-side javascript is as well. After a quick look, the documentation didn't mention client-side javascript execution.Stlaurent
S
0

You can probably use PhantomJS or headless chrome.

Stlaurent answered 26/5, 2021 at 13:56 Comment(0)
E
-1

Try xhtml2pdf. Here's the project page at python.org.

Ethelyn answered 25/2, 2010 at 1:10 Comment(4)
He wants a solution that will understand changes the page made by JavaScript.Its
I can't see where I can test a live HTML page and it doesn't say much about javascript or dhtml.Hardpressed
Sorry, there is a link to the python project page for Pisa. I've changed my link.Ethelyn
The question still remains... can it handle the case that I have described... i.e. dhtml generated via JavaScript on load...Hardpressed

© 2022 - 2024 — McMap. All rights reserved.