开发者

Dynamic HTML to PDF [closed]

开发者 https://www.devze.com 2022-12-21 08:41 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, to开发者_运维问答ols, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed last year.

Improve this question

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.


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.


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.


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.


dynamic-html-pdf

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

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


You can probably use PhantomJS or headless chrome.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消