Live data from Hacker News

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

news.ycombinator.com

71–80 of 170 posts

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

#71
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

Or, please do? I use PDF's so I can send them to my iPad to read offline, highlight them, annotate them, and then send them back to my filesystem with highlights and annotations intact. I sure can't do that with any "nice formats" like HTML or TXT or EPUB or MOBI.

PDF is literally digital paper. HTML has logical structure, it can adapt to different displays, etc.

Sometimes you want one, sometimes, the other.

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

#73
post #13

chrome --headless --disable-gpu --print-to-pdf https://example.com

Can Firefox do this? with an elaborate script that relies on xdotool

Yes, kind of...

/path/to/firefox --window-size 1700 --headless -screenshot myfile.png file://myfile.html

Easy, right ?

Used this for many years... but beware:

- caveat 1: this is (or was) a more or less undocumented function and a few years ago it just disappeared only to come back in a later release.

- caveat 2: even though you can convert local files it does require internet access as any references to icons, style sheets, fonts and tracker pixels cause Firefox to attempt to retrieve them without any (sensible) timeout. So, running this on a server without internet access will make the process hang forever.

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

#74
post #13

chrome --headless --disable-gpu --print-to-pdf https://example.com

Can Firefox do this? with an elaborate script that relies on xdotool

Why, Firefox has a headless mode. It can't just print a document via a simple CLI command, you have to go for Selenium (or maybe Playwright, I did not try it in that capacity). Foxdriver would work, but its development ceased.

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

#78
post #60
post #51

Earlier quoted context omitted.

PDF annotations sit within the file.

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 original content in its own non-standardized, non-portable way, which is not desirable for a number of reasons.

So when you say:

> What you are describing are features of an editor, not a file format.

That is incorrect. It is an intentionally designed and standardized feature of the file format.

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

#79
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/

Most website do not have a print CSS, so it doesn’t print that nicely in PDF.

But, I upvote weasyprint for that instead.

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

#80
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/

Prince XML looks nice but what about creating a PDF directly from a website? This often adds some problems, for example links still pointing to other pages on the web. But in my experience printing to PDF is often not good enough.

Yes, I did that for a recent small program. The @media print media query is powerful enough for most of the stuff I wanted to format nicely. Even page breaks are possible.
Post reply on HN