Show HN: Ready-to-use API to convert any web page to PDF using headless Chrome
apify.com
apify.com
chromium --headless --disable-gpu --print-to-pdf=google.pdf http://google.com/
What does Apify add in this case?
It's robust because the modern web is pretty much built for Chrome, although it can be resource-intensive if you're archiving many sites.
Looks like there are a lot of options available for this. I suspect Apify's using one of them.
chromepdf() {
chrome --headless --disable-gpu --print-to-pdf="$1" $2
}Edit, I see now that I replied to the wrong comment. It was meant to they who made an alias to it.
Alternatively, you can run Chrome headless with the remote debugging API (--remote-debugging-port=9222) and send a Page.printToPDF (https://chromedevtools.github.io/devtools-protocol/tot/Page/...) after some delay.
// URL to screenshot
service.prerender.cloud/screenshot/https://www.google.com/
// URL to pdf
service.prerender.cloud/pdf/https://www.google.com/
// URL to html (prerender)
service.prerender.cloud/https://www.google.com/I have been using PDFReactor for this reason, as wkhtml2pdf and weasyprint have had problems, as will any webkit or chromium based renderer because these layout systems simply do not support paged printing.
Screen.rip gives you more control over the generated PDF beyond Puppeteer's options (like it can wait for certain elements to appear, inject CSS or switch to screen stylesheet instead of the print stylesheet).
Great job!
Encoding your HTML as a data: URI might work for this service as-is (provided you use no non-CORS-enabled resources). Haven’t tried it.
And is it possible to print multiple Chrome tabs?
Printing pages to PDF is pretty straightforward. It's the above two issues were I've run into problems. Anyone know of a good solution to the second one?
e.g.
chrome --disable-gpu --headless --user-data-dir=/Users/username/Library/Application\ Support/Google/Chrome/Default https://example.com/paywall/article.html
Alternatively, use puppeteer to script the auth process. https://github.com/GoogleChrome/puppeteer