Live data from Hacker News

Ask HN: What is nowadays (opensource) way of converting HTML to PDF?

news.ycombinator.com

81–90 of 170 posts

Re: Ask HN: What is nowadays (opensource) way of converting HTML to PDF?

#82
post #26

Please don't turn nice formats into a format that's similar to screenshots of text. Pandoc has an option to pack all images and styles needed to render the page into one html file: pandoc --self-contained input.html -o output.html

HTML+CSS+media files isn’t a nice format, and much less portable through time and space than PDF.

Re: Ask HN: What is nowadays (opensource) way of converting HTML to PDF?

#83
post #5

Puppeteer and Playwright are the main open-source options nowadays, both solid for HTML → PDF once your print CSS is sorted. Don’t forget proper page breaks (break-before/after/inside) — e.g. break-after: page works in Chromium, while always doesn’t. For trickier pagination you can look at Paged.js, and I’d test layouts in Chrome/Edge before automating. Shameless plug: I run yakpdf.com, a hosted Puppeteer-based servi…

Seconded. I went with C# + Playwright. I tried iTextSharp, iText, PDFSharp, and wkhtmltopdf, but they all had limitations. I had good results with Playwright in minutes, outside of tweaking the CSS like you mention. I documented the process here[0] if anyone needs examples of the CSS and loading web fonts. Apologies for the article being long-winded – it was the first one I published. [0] https://johnh.co/blog/creati…

Thirded, you can build this straight into your backend or into a microservice very easily.

You can also easily generate screenshots if that's more suitable than PDFs.

You can also easily use this to do stuff like jam a set of images into a HTML table and PDF or screenshot them in that format.

Re: Ask HN: What is nowadays (opensource) way of converting HTML to PDF?

#84
post #55
post #7

Just print to PDF in a browser, or automate that using a browser automation tool. For a non-browser-based open source solution, WeasyPrint. https://weasyprint.org/ For a proprietary solution, try Prince XML: https://www.princexml.com/

I’ve had excellent experience with Prince XML and poor experience with everything else I’ve tried. Prince is fast, robust and reliable. Yes it costs money. So does developer time.

Agreed. Prince also has a lot of good features for headers, footers, page numbering, etc, that make it very powerful.

Re: Ask HN: What is nowadays (opensource) way of converting HTML to PDF?

#87
post #60

Earlier quoted context omitted.

I know, even though that depends on the editor. Okular for example places them in an extra file, last I checked. That's not unique to PDFs. HTML files are modifiable. There is nothing preventing an editor to put annotations in it as well.

PDF is designed for annotations in the file format. You annotate in one editor, you can change the annotations in another. You can always distinguish between original content and annotations. I see no indication that Okular stores highlights or annotations in a separate file, that would be bizarre. There is no mechanism for annotations in HTML or the other formats I listed. An editor would just be editing the origina…

It definitely used to be bizarre then:

https://superuser.com/questions/333378/where-does-okular-sto...

Re: Ask HN: What is nowadays (opensource) way of converting HTML to PDF?

#88
Ghostscript. Depending on specific needs it may be much more turnkey than Pandoc, which isn’t actually doing much directly with things other than intermediating, iiuc. (LaTex) does the heavy lifting.

Ghost script is working with postscript natively and will likely manage idiosyncrasies of web content better. It’s got a decent ecosystem, command line, you can find gui’s if that’s your thing (no judgement, your lifestyle is none of my business).

Many other good tools mentioned here as well, but if your asking because you need more, or fine grained (near infinite) control over the pdf composition, there’s nothing OSS I can think of that approaches its capabilities.

https://ghostscript.com/

Re: Ask HN: What is nowadays (opensource) way of converting HTML to PDF?

#90
I just wrote a quick HTML renderer in Python with ReportLab: https://GitHub.com/kragen/dercuano/blob/master/genpdf.py

It only handles like 5% of HTML, but it's the 5% I was using.

I've also had success producing PDFs with GhostScript from a PostScript file. PostScript is really easy to write, almost like SVG.

Post reply on HN