top | item 15611265

Show HN: Ready-to-use API to convert any web page to PDF using headless Chrome

129 points| jancurn | 8 years ago |apify.com | reply

64 comments

order
[+] conradk|8 years ago|reply
Saving a webpage to a PDF is literally one command line away:

chromium --headless --disable-gpu --print-to-pdf=google.pdf http://google.com/

What does Apify add in this case?

[+] jancurn|8 years ago|reply
You 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.
[+] xilni|8 years ago|reply
This is exactly what I do bundled into a nice function added to my .zshrc

    chromepdf() {
        chrome --headless --disable-gpu --print-to-pdf="$1" $2
    }
[+] nickjj|8 years ago|reply
Also 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.
[+] scaryclam|8 years ago|reply
Thanks, 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.

[+] Natsu|8 years ago|reply
There's also wkhtmltopdf that more or less does the same thing using webkit.
[+] granda|8 years ago|reply
Are there any downsides to me using this to build my own archiver?
[+] Keats|8 years ago|reply
Is there a way to add a delay to let JS render?
[+] jotto|8 years ago|reply
I've been working on something similar: https://www.prerender.cloud/docs/api

  // 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/
[+] nikisweeting|8 years ago|reply
There are quite a few of these Chrome headless-as-a-service already out there, this is the third time I've seen one posted on HN.
[+] visarga|8 years ago|reply
By the way, is there an opposite service that converts PDF's into plain HTML for reading? I know about https://www.arxiv-vanity.com/papers/ but it only works on arXiv PDFs.
[+] jancurn|8 years ago|reply
There's another Apify act that extracts text from a PDF using the pdf-text-extract NPM package - see https://www.apify.com/juansgaitan/pdf-scraping If there's any library or tool that can convert PDF to HTML, it will only take a few minutes to setup such an API on Apify.
[+] jugjug|8 years ago|reply
Off-topic, but Apify as a service looks really good. I was spinning up a dedicated VM on AWS with Docker installed only to get a simple webscraper running. Apify solves this elegantly and removes an significant pain in my workflow.
[+] ak39|8 years ago|reply
Any info how this compares to commercial html to pdf renderers like PrinceXML?
[+] schneidmaster|8 years ago|reply
In my experience, Prince is great for static HTML + CSS rendering, but its JavaScript engine is pretty lackluster -- I couldn't get it to work with rendering React components, for example. So it depends a lot on your use case and if you can server-side render everything. It's also pretty pricey[1] -- not that I mind paying for quality software but that sticker could be prohibitive for a lot of folks.

[1] https://www.princexml.com/purchase

[+] defenestration|8 years ago|reply
I would like to know that as well. We are using wkhtmltopdf for around 2 years. Had many issues with incompatible css. I considered PrinceXML. Looked solid, but a bit expensive and also lacking some css support. I'm considering switching to Puppeteer / Chrome.
[+] waddlesworth|8 years ago|reply
My main concern is support for the CSS3 Paged Media Module.

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.

[+] laktek|8 years ago|reply
I built Screen.rip, which also supports PDF generation. https://screen.rip/#pdf

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).

[+] phmagic|8 years ago|reply
I love this service! I think ease of adoption you can allow pre-made scripts to be shared so the non-technical can easily set up work flows that go right into their email. For the technical folks, I think it would be great to have examples of things you can do with Apify that is a hassle to do with your local chrome headless.

Great job!

[+] sebazzz|8 years ago|reply
We are not too happy with our EvoPDF license so in the basis this is a good option. However, I do not think this allows adding headers, footers, page numbers etc.
[+] jeppebemad|8 years ago|reply
Is there a similar API around that accepts HTML instead of a URL? I’ve build one for my project, but I would prefer to delegate this to an external service.
[+] chrismorgan|8 years ago|reply
Bear in mind that you’ll need to either embed all your resources, or only use CORS-enabled resources, or fake the origin for your HTML document so that it can access non-CORS-enabled resources on a particular domain.

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.

[+] jancurn|8 years ago|reply
We can easily add this feature. So you'd like to pass a single HTML file?
[+] Robdel12|8 years ago|reply
Question: does this create accessible PDFs? That would be a really nice _possible_ work around for screen reader users having issues with a website.
[+] jancurn|8 years ago|reply
I believe it's possible, by adding something like "scale: 1.5" to "pdfOptions" you might render an accessible PDF
[+] tehlike|8 years ago|reply
you could rpobably launch this service free, and someone will probably create a docker image, and make it one click.
[+] colordrops|8 years ago|reply
As long as GPU support is not functional in headless, "any web page" is a misnomer. A large enough percentage of sites use GPU acceleration so that headless mode is useless. This needs to be addressed by the Chrome team.
[+] kyberias|8 years ago|reply
What do you mean "use GPU acceleration"? Are you saying that a large percentage of sites use WebGL? Using GPU acceleration for web page rendering is just a browser performance optimization. They can render the same page without GPU only slower.
[+] panda888888|8 years ago|reply
Does this work if the page is behind a password/SSO wall?

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?

[+] gbrits|8 years ago|reply
Assuming you want this done automatically, what's the advantage of 'printing' multiple tabs to PDF in a headless browser, over just sequentially loading and printing the pages you want done?