News for hackers. Stuff that matters.
Re:Firefox 56 supports headless mode on Windows by arunitc on Thursday June 22, 2017 @07:06AM (#14611349)
I was wondering if I could use this (using a plugin) or Chrome to generate PDF files on the web server. Most of the PDF generation software out there are quite expensive.
Re:Firefox 56 supports headless mode on Windows by boredpudding on Thursday June 22, 2017 @07:08AM (#14611359)
Maybe it's possible with the print option Chrome offers. Regular Chrome can print to PDF files. Maybe headless Chrome supports that.

Edit: It's possible. It's on the introduction page of Chrome Headless

https://developers.google.com/web/updates/2017/04/headless-c...

Look at 'Create a PDF'

Re:Firefox 56 supports headless mode on Windows by robk on Thursday June 22, 2017 @08:46AM (#14611841)
Here's a simple library to do that: https://github.com/LucianoGanga/simple-headless-chrome
Re:Firefox 56 supports headless mode on Windows by aidos on Thursday June 22, 2017 @07:42AM (#14611504)
You can. In my experience, there are very few systems as good at producing accurate renders as well as Chrome. There are a few Chrome/webkit based options.

PhantomJS made the process pretty easy, but that's probably not a good option these days as it's on the way out.

NightmareJS is pretty much the same and can do it (though I haven't tested myself) [0]

If you want to get a little deeper you can use CEF (Chromium Embedded Framework [1 ]. It basically turns Chrome into a library and there are a bunch of projects that build on top of it (eg Electron, which is what NightmareJS uses underneath).

Here's a project that just uses CEF for converting to PDF [2].

[0] https://github.com/segmentio/nightmare#pdfpath-options [1] https://bitbucket.org/chromiumembedded/cef/overview [2] https://github.com/spajak/cef-pdf

Re:Firefox 56 supports headless mode on Windows by RandomBookmarks on Thursday June 22, 2017 @10:40AM (#14612543)
For C#, there is also CEFSharp. This is used, for example, by the image(recognition)-driven Kantu automation: https://a9t9.com/kantu/web-automation
Re:Firefox 56 supports headless mode on Windows by mchiron on Thursday June 22, 2017 @07:11AM (#14611368)
We use wkhtmltopdf to do this server side. It as a few problems and quirks, but it work well enough for us.
Re:Firefox 56 supports headless mode on Windows by pilif on Thursday June 22, 2017 @07:26AM (#14611423)
it has a lot of quirks, and, frankly, webkit is pretty bad at printing in general as it lacks a ton of printing specific features like re-printing table headers across page boundaries.

There's also only very rudimentary support for page headers and footers.

wkhtmktopdf is probably ok if you just need a quick single page invoice printout, but the moment you need more (in our case, we definitely do), I can highly recommend paying for https://www.princexml.com/ which has support for a lot of printing specific features.

Re:Firefox 56 supports headless mode on Windows by bowersbros on Thursday June 22, 2017 @10:17AM (#14612379)
One of the issues we had with wkhtmltopdf was that we use Angular for our views, and we have a view for an invoice, and the ability to print it.

We wanted the same view to be passed into wkhtmltopdf to download it as PDF (with a couple of things omitted).

Unfortunately, the JS didn't execute properly, so you get a borked output.

End result was we render it, capture the HTML after render, and pipe that through to wkhtmltopdf.

Using chrome _should_ resolve that issue and let us just pass the page through.

Re:Firefox 56 supports headless mode on Windows by veza on Thursday June 22, 2017 @09:30AM (#14612064)
wkhtmltopdf works quite well for us, even with modern web technologies.

Here's an example fully rendered in React: https://s3.amazonaws.com/ud-reports/comps-report-86.pdf

And one with svg charts: https://s3.amazonaws.com/ud-reports/chart-report-89.pdf

With -webkit- prefixes you can use even flexbox, and and with js polyfills you'll have all you need.

Of course if I would start a project now, I would use chrome headless, but I don't feel wkhtmltopdf is that bad.

Re:Firefox 56 supports headless mode on Windows by 0XAFFE on Thursday June 22, 2017 @09:13AM (#14611968)
We used node-html-pdf[1] for this, but it depends on phantomjs which uses an ancient version of webkit. We migrated to electron and its contents.printToPDF(options, callback), but that has the drawback that we have to start a X-server[2] with our backend code. I have hopes with a headless chrome we can ditch the xvfb-server and have one less dependencies. The setup is a pain, but the results are very good and looking very decent.

[1] https://github.com/marcbachmann/node-html-pdf [2] https://en.wikipedia.org/wiki/Xvfb

Re:Firefox 56 supports headless mode on Windows by mhb on Thursday June 22, 2017 @07:23AM (#14611402)
What about ReportLab (free version)? http://www.reportlab.com/opensource/
Re:Firefox 56 supports headless mode on Windows by wnevets on Thursday June 22, 2017 @11:04AM (#14612720)
I currently use phantomjs for this, I haven't switched to headless chrome yet.
Re:Firefox 56 supports headless mode on Windows by gjjrfcbugxbhf on Thursday June 22, 2017 @08:34AM (#14611768)
Latex is free.
Re:Firefox 56 supports headless mode on Windows by JBorrow on Thursday June 22, 2017 @09:16AM (#14611981)
Really not the same thing (e.g. pandoc for html -> pdf via LaTeX) as having a webkit based renderer. They do not look the same at all; sometimes it is good to have a LaTeX output but most of the time you are going to want a more accurate representation of your HTML.
Re:Firefox 56 supports headless mode on Windows by majewsky on Thursday June 22, 2017 @09:58AM (#14612251)
The original question didn't state that he was starting from HTML. If you want to generate an invoice or such from plain data, then LaTeX seems like a good fit. Rendering HTML is a whole different bag.
Re:Firefox 56 supports headless mode on Windows by tremon on Thursday June 22, 2017 @11:23AM (#14612856)
That is true. TeX is a typesetting engine, and allows you exact control over the final rendering, while HTML is a markup language, the rendering of which depends on very many factors.
Re:Firefox 56 supports headless mode on Windows by criddell on Thursday June 22, 2017 @09:43AM (#14612148)
My time isn't. Sometimes the most cost effective option isn't the one with the lowest price tag.