5 ms·
Saving a webpage to a PDF is literally one command line away: chromium --headless --disable-gpu --print-to-pdf=google.pdf http://google.com/ http://google.com/
by conradk 9y ago
Saving a webpage to a PDF is literally one command line away:
chromium --headless --disable-gpu --print-to-pdf=google.pdf http://google.com/ http://google.com/
What does Apify add in this case?
- granda 9y agoAre there any downsides to me using this to build my own archiver?
- nikisweeting 9y agoMany projects are already using this for archiving purposes. Check out https://github.com/pirate/bookmark-archiver https://github.com/pirate/bookmark-archiver 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.
- xfer 9y agoconverting to pdf is lossy, i wouldn't use this for archiving purposes, normally i use webrecorder for archiving.
- jancurn 9y agoYou can call the API from anywhere, from resource-constrained servers, Docker containers that cannot run headless Chrome, JavaScript on a website etc. Also, we'll keep adding new features to this act to make it worthwhile to use it, e.g. retries on failures, posting of the file to some URL etc.
- nickjj 9y agoAlso if you can't access this command for whatever reason, another option is to open the print dialog box in Chrome and set the destination to "Save as PDF" and it will work. You'll even get to see a preview. It's very useful for 1 off saves where you want to consume a really long post offline in a PDF viewer.
- Scarbutt 9y agoSure, but the idea is to do it programatically.
- scaryclam 9y agoThanks, I didn't know that was a thing. I was checking out wkhtmltox earlier this week as well. Looks like there are a lot of options available for this. I suspect Apify's using one of them.
- jancurn 9y agoActually we're simply using Puppeteer - see the source code at the bottom of https://www.apify.com/jancurn/url-to-pdf https://www.apify.com/jancurn/url-to-pdf
- zulln 9y agoAny special reason to why you use this over Chrome?
- nikisweeting 9y agoPuppeteer is a scripting library for Chrome, built by the Chrome team.
- xilni 9y agoThis is exactly what I do bundled into a nice function added to my .zshrc chromepdf() { chrome --headless --disable-gpu --print-to-pdf="$1" $2 }
- zulln 9y agoWhat do you use this for?
- nsomaru 9y agoSave a webpage as a PDF
- zulln 9y agoDid not mean to sound negative, but what is the use-case of saving web pages as PDFs? I understand building in the functionality in something else, but here it sounds like you manually type/paste in URLs on a regular basis. Edit, I see now that I replied to the wrong comment. It was meant to they who made an alias to it.
- sgolestane 9y agoIs there anyway to make chrome wait until the page loads?
- lashkari 9y agoLooks like it prints automatically once Page.loadEventFired is triggered. 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/#method-printToPDF https://chromedevtools.github.io/devtools-protocol/tot/Page/...) after some delay.
- Keats 9y agoIs there a way to add a delay to let JS render?
- jancurn 9y agoNow there is - I've just added the "sleepMillis" input option.
- Natsu 9y agoThere's also wkhtmltopdf that more or less does the same thing using webkit.