39 ms·
Wkhtmltopdf: Command line tools to render HTML into PDF
- rangoon626 6y agoI always liked wkhtmltopdf. It makes it super easy for front end devs to layout and generate PDFs using tools they already know. Can't beat the price either, especially compared to coldfusion or some of the .net libraries out there.
- aaronbrethorst 6y agoI use this via the Ruby gems pdfkit and wkhtmltopdf-heroku in a service I built in Rails. It's been a fantastic, near-bulletproof way to generate attractive, complex PDFs with minimal effort. https://github.com/pdfkit/pdfkit https://github.com/pdfkit/pdfkit https://github.com/rposborne/wkhtmltopdf-heroku https://github.com/rposborne/wkhtmltopdf-heroku
- pmontra 6y agoI also use it in that way but not on Heroku. My single largest complaint is that it cuts lines in two at the end of pages, as in the top half of this line on page 1, the bottom half on page 2. I didn't find a way to CSS out of that. To be fair, my customer never allocated budget to find a solution so that is probably not very important to them in our scenario.
- williamsmj 6y agoFor my use case[0], I found weasyprint[1] easier to work with than wkthmltopdf, it gives better results (in the sense they look more like what I get when I save to PDF using a browser), and it seems to be more actively maintained. But in the end, I switched[2] to "chrome --headless --print-to-pdf-no-header", since it reproduces browser behavior pretty much by definition and, while it's a colossal dependency, it's also trivial for non-technical users to install. [0] https://github.com/mikepqr/resume.md https://github.com/mikepqr/resume.md [1] https://weasyprint.org/ https://weasyprint.org/ [2] https://github.com/mikepqr/resume.md/commit/206a6cbc85fd04569081b602cbc9386b2dd8857e https://github.com/mikepqr/resume.md/commit/206a6cbc85fd0456...
- the_jeremy 6y agoI tried out weasyprint, wkhtmltopdf, and a trial of a paid one (forget which). They all vastly underperformed chrome and firefox's print to PDF functionality, which both worked exactly as expected (i.e., they print it looking like how the page would look under that browser).
- busymom0 6y agoI used jsPDF for my ResumeToPDF site and it worked pretty well. Only thing I sort of had pain with was that I had to include SVG icons in-line in the html/javascript and had to convert them to base64 to be included in the PDF. And same thing for the custom fonts - I had to include base64 of the font TTFs to be embedded in the PDF: https://resumetopdf.com https://resumetopdf.com https://github.com/MrRio/jsPDF https://github.com/MrRio/jsPDF
- dmje 6y agoWhat's the advantage over print > save as PDF? Asking for a friend.
- kimi 6y agoThat you can script it.
- deleted 6y ago[deleted]
- yreg 6y agoWe use it for a CV portal (think internal linkedin without the social stuff). Advantage for us is in UX (we can have a download PDF button), although I'm not sure it was worth the bother.
- casperb 6y agoThere are lots of use cases where you need a PDF document, without letting the user print to PDF it manually. For example sending PDF invoices per email, or generating reports and uploading it to a Dropbox directory.
- atleta 6y agoUser experience ("download PDF" link). You may also want to send it in an email. (We do both.)
- sebbeau 6y agoWe use it too, with an api on top of it https://github.com/acsone/kwkhtmltopdf https://github.com/acsone/kwkhtmltopdf
- jitl 6y agoI use Chromium and Puppeteer to automate creation of PDFs. I considered both wkhtlmtopdf and weasyprint but I decided you can’t beat a debugging UI like Chrome’s.
- racl101 6y agoChromium and puppeteer is pretty great with the exception of a few scenarios like having header images in a PDF. Such a small thing can turn into a very annoying endeavour.
- lihaoyi 6y agoI tried using wkhtmltopdf for rendering my book https://www.handsonscala.com/ https://www.handsonscala.com/ to pdf, but in general I found it pretty buggy and unreliable. Lines of text would get split between pages, some CSS like flexbox didnt render properly, font sizes and page scaling was sometimes off (e.g. a big image on a page would make everything else shrink), etc. In general it "worked", but it didn't work well. I ended up swapping in Google's Puppeteer library to render my PDFs, and despite needing a bit of plumbing to get my Scala build script talking to the node.js runtime (and I needed to do e.g. page numbers and table-of-contents extraction myself using Apache PDFBox) in the end it worked much better. Things looked the same in puppeteer PDF as they did in the browser, which is something I could never quite achieve with wkhtmltopdf
- captn3m0 6y agoI prefer pandoc for known-text-to-pdf, much more cleaner output.
- hutrdvnj 6y agoYou don't need X11 by the way. It's possible to render PDFs with wkhtmltopdf + qt patches without Xorg.[0] [0] https://beuke.org/docker-alpine-wkhtmltopdf/ https://beuke.org/docker-alpine-wkhtmltopdf/
- thrownaway561 6y agoi love this project but have found that i have experienced memory leaks where i would have to restart my rails application once a day.
- bni 6y agoOne interesting problem in this space is how to extract the actual article content from webpages without all the "fluff". PDFs with navigation menus and ads don't look so nice. I have been experimenting with Mozillas JavaScript library Readability (Powers Firefox's Reader feature) to convert wikipedia articles to cleaned up text. It works quite nice for everything that works with Reader in Firefox or Safari.
- abhinav22 6y agoIn CSS we can set media settings to hide many items from the print page and also change the styling of content in it. Not many people do it I guess
- gliderShip 6y agoif you want to generate beautifully paginated pdf / printable pages from your html i cannot recommend enough https://www.pagedjs.org/ https://www.pagedjs.org/. it supports page numbers, tos, footnotes, dynamic references (see table on page x link ) and a lot of other very cool stuff
- abhinav22 6y agoThanks!! This looks super interesting. I couldn’t find any decent solutions except princexml but I will give this a whirl The fact that they are achieving it via poly fill sounds like a genius approach!
- steve_mcdougall 6y agoon the paid side of things princexml is literally the best thing i have used, the owner of the company (who wrote the software) actually responds in the forums
- SigmundA 6y agoPrince is the only browser engine that fully supports the print css standards that I know of. There seems to be little incentive by the browser makers improve printable output so Prince has quite a niche.
- abhinav22 6y agoPrince is amazing, I’m thinking of buying a license at work soon. All the other solutions have some big flaws and are hard to use
- ognarb 6y agoDid you try paged.js?
- abhinav22 6y agoI’m trying it now - didn’t know about it until someone else mentioned it in this thread
- jbigelow76 6y agoWe use pajed.js in conjunction with Arachnys/AthenaPdf [1] for HTML to PDF conversion, its a pretty nice experience. 1. https://github.com/arachnys/athenapdf https://github.com/arachnys/athenapdf
- abhinav22 6y agoNice! Will check it out - I was trying to build a electron word processor app that would use html and then convert to pdf, maybe I will start it again with this. Can this handle svg or JavaScript libraries like Charts.js? I’ll check the details but if you know off the top of your head :) Thanks again for the link
- hnedeotes 6y agoThe only thing I don't understand is why browsers doesn't expose an API for this? Or whatever it sends down to the printer menu, since you can already save a page as PDF? And the same for bare pictures since the rendering engine has to have drawn all of that to display it? Is it something to do with pdf's being Adobe's? (I still see the utility of a command line program but just wondering since they have some issues around font-sizing, fonts loaded through the web, etc)
- andix 6y agoI tried once to use Chromium as a library to create PDF. But even they don’t expose a nice C API for that, with all the settings you can make in the UI. Super messy.
- abhinav22 6y agoPdf standard I believe is open now, or at least there is a very, very detailed guide by Adobe (I’ve read parts of it). Considering there are competing pdf readers to Adobe, this makes sense Browser pdf styling is quite good actually, I just hate the fact it prints date / web titles by default (only user can unselect) and doesn’t work that well with paged media I was thinking of building my own project in an adjacent area, so I did a lot of research. PrinceXML is my favourite, although it doesn’t work well as an integration to an app since then you have to share profits with them In the end I ditched my project (lol)
- hnedeotes 6y ago> Browser pdf styling is quite good actually It is but I remember going through so many hoops to do it, when a "browser.export_pdf()" would have been so handy. Does yours live in some repository online?
- abhinav22 6y agoUnfortunately not, I didn’t accomplish much of value in the end
- pluc 6y ago
- paulbjensen 6y agoWe used Wkhtmltopdf on a project making PDF reports. The challenge we ran into with it was generating PDFs with pixel-precision for content layout, and at the time support for CSS Flexbox. I think eventually we worked out how to do it, but that for us was the tricky bit.
- udp 6y agoI once used this with pygmentize as part of a really hacky pipeline to convert code snippets to syntax highlighted PDFs with selectable text: %.pdf: %.* pygmentize -f html -O full $< | wkhtmltopdf -T 0 -B 0 --page-width 210mm --page-height 5000mm --no-background - - > .tmp.pdf pdfcrop .tmp.pdf $@ rm -f .tmp.pdf
- SigmundA 6y agoWkhtmltopdf is based on QtWebkit which is long deprecated. There was work to move it QtWebEngine but doesn't seem to be going anywhere: https://wkhtmltopdf.org/status.html https://wkhtmltopdf.org/status.html In the meantime Chrome added headless support and with command line or puppeteer you can do the same thing with a much newer and more secure engine. However if you need full CSS-Paged support then PrinceXML is probably your only option and its not cheap: https://www.princexml.com https://www.princexml.com
- abhinav22 6y agoThere is a cheap alternative to Prince called DocRaptor which uses the prince engine but is pay per use Basically prince adds a watermark on the first page - so you do all your revisions and then only pay for the final copy to remove the watermark (however as any editor would know, we always have many final, final final, final v3 versions ;) so paying for prince once-off at 500$ is probably the best way for anyone who needs professional pdfing. I wrote a latex / markdown / html editor with a simple mechanism (using the browsers default print settings), I think it’s not too bad for those looking to pdf from html: https://writer.math.dev https://writer.math.dev Issue with mine is you would have to do all style sheets in line. I would recommend serious users to either pay for Prince if they want header/footer/page number control or just use print style sheets and code the html / css themselves and use headless chrome or chrome itself to print. Many people want JavaScript in their pages, I can’t recall if headless chrome can correctly account for that. File > print is the best imo :)
- blowski 6y agoDespite have a deprecated dependency, is there any issue using wkhtmltopdf? I'm running it in quite a few projects to generate letters and invoices, and not experiencing any issues.
- ognarb 6y agoif it works for you and you control the html/css/js that is rendered, I don't think there is any problem.
- 6y ago
- makkesk8 6y agoAt the company I work for we used chrome headless paired with https://github.com/pagedjs/pagedjs https://github.com/pagedjs/pagedjs to fill in the gaps and it works very very well.
- kevinhq 6y agoI am just wondering, why its gem (wkhtmltoimage-binary) takes so much disk space? 170MB [0] [0] https://imgur.com/xEcZ7sv https://imgur.com/xEcZ7sv
- SigmundA 6y agoBecause it is a full headless web browser (older version of Webkit).
- gitowiec 6y agoIn our project we use Chromium with puppeteer. The thing is good because it saves our time. Our project generates invoices PDFs. It's written in React so we can send invoice view component directly to PDF printer made of Chromium and puppeteer. Another pro is that puppeteer has a side project "swarm" and which makes it easy to scale when demand is high.
- jhoh 6y agoAfter trying many HTML2PDF libraries we're doing the same thing and are quite happy with this approach. I've tried using "professional" solutions like iText pdfHTML[0]. They all seem to be based on flyingsaucer[1] which only supports HTML4 and CSS2, and will fail rendering pages that use modern CSS let alone JavaScript. [0]: https://itextpdf.com/en/products/itext-7/convert-html-css-to-pdf-pdfhtml https://itextpdf.com/en/products/itext-7/convert-html-css-to... [1]: https://github.com/flyingsaucerproject/flyingsaucer https://github.com/flyingsaucerproject/flyingsaucer
- aww_dang 6y agohttps://xmlgraphics.apache.org/fop/faq.html#xhtml-to-pdf https://xmlgraphics.apache.org/fop/faq.html#xhtml-to-pdf I've been using Apache FOP with some success.
- cies 6y agoSince alternatives are being discussed, Latex needs to be mentioned, it is made for "page layouts" (contrary to HTML+CSS which is made for viewPort layouts (that scroll and need to respond to changing widths)). With Latex one writes the document in Latex, instead of some old version of HTML+CSS with limitations, some additional page break controllers, and some ways to set page numbers etc. Many docker containers exist for making Latex (the whole texlive[1] distro) into a service. [1]: https://www.tug.org/texlive/doc/texlive-en/texlive-en.pdf https://www.tug.org/texlive/doc/texlive-en/texlive-en.pdf
- SebastianKra 6y agoMuch of the appeal of these conversion tools is that they might finally allow us to drop Latex. Although HTML/CSS/JS aren't fully viable for print just yet, they're not missing much.
- cies 6y agoI've rarely seen someone choose between some HTML2PDF tool and Latex. Most people I've seen working with the H2P tools were not even aware of Latex's existence. I think HTML/CSS is viable for print (JS has no business in print as print is not interactive), as it is used a lot. The point I'm trying to make is that Latex is simply better equipped due to it being specifically designed for page layouts. How long do we have these H2P tools? And how far have they come in terms of supporting page layouts?
- sdeframond 6y agoI feel like this it is the right time to mention WeasyPrint, an HTML to PDF converter supporting CSS Paged Media. It is a CLI tool written in python and quite usable in my experience. https://weasyprint.org/ https://weasyprint.org/
- dsr_ 6y agoTake note: most of these HTML to PDF renderers run a headless browser. Does that pose a security concern to you? It might. Are you accepting any data from the outside? Are you requesting anything from the outside world? Does your rendering browser have a place to write any data?
- cjcampbell 6y agoMentioned some guidance in a separate thread, but encoding/sanitization of dynamic content is crucial. Also, dig into the options for the tool and disable features that aren’t needed. As an example, wkhtmltopdf enables you to disable local file inclusion except for predetermined locations.
- hyakosm 6y agoWkhtmltopdf is slow, inconsistent between platforms and can lead to leakage of sensitive informations on the server (an iframe pointing to file:///etc/passwd). Cf. https://news.ycombinator.com/item?id=17566887 https://news.ycombinator.com/item?id=17566887
- cjcampbell 6y agoWon’t debate the other critiques, but one thing I would say for any HTML to PDF solution, is that you need to properly encode or sanitize any dynamic content. As an additional layer of protection, dig into the options for the tool in order to limit the features supported. My recommendation would be to restrict local file support to a pre-determined directory and to disable JavaScript (if possible).
- subsubsub 6y agoCan anyone recommend a PDF to HTML converter?
- bob1029 6y agoThere is no reasonable way to do this. The only conversion of PDFs that makes sense in the general case is to rasterize them to images of some arbitrary DPI. Then, you could simply insert these images into an HTML document. This is more-or-less what we do in our application when we want to present a PDF for review or perform drawing on top of PDFs in contexts where we cannot rely on PDF libraries for whatever reason.
- racl101 6y agoBoth amazing and frustrating software.
- tyingq 6y agoChrome headless is nice for this: $ google-chrome --headless --disable-gpu --print-to-pdf-no-header http://www.example.com/
- thangalin 6y agoConTeXt[1] can render XHTML to PDF[2]. Throw Pandoc[3] into the mix for a powerful command-line document conversion solution. My desktop text editor, KeenWrite[4], will soon have the ability to render Markdown as PDF using ConTeXt by first converting Markdown to XHTML, then rendering XHTML as a PDF; however, KeenWrite doesn't support command-line operation, yet. [1]: https://wiki.contextgarden.net/Installation https://wiki.contextgarden.net/Installation [2]: https://dl.contextgarden.net/myway/tas/xhtml.pdf https://dl.contextgarden.net/myway/tas/xhtml.pdf [3]: https://pandoc.org/ https://pandoc.org/ [4]: https://github.com/DaveJarvis/keenwrite https://github.com/DaveJarvis/keenwrite
- Lammy 6y agoI use(d) wkhtmltopdf to implement PDF generation of certain pages in a Jekyll site (e.g. of my CV page) but was never really happy with the output since it was very hard to get it to look like a purpose-built PDF document even with a good print stylesheet. One of these days I'd like to try replacing it with a custom Kramdown converter that invokes HexaPDF: https://hexapdf.gettalong.org/ https://hexapdf.gettalong.org/
- trenchgun 6y agoHtml into pdf? What's the problem with pandoc?
- lancebeet 6y agoPandoc converts to pdf via latex the last time I checked. This may be what you want, but it may not be. When I did html to pdf conversion for a larger project, it wasn't what I wanted. Some of the html structure was lost in translation, and a lot of latex defaults were inserted which isn't what I wanted for that project, and overriding them would have been too time-consuming. Weasyprint didn't have these issues (weasyprint produced better results than wkhtmltopdf).
- noxer 6y agoSadly this one messes up rendering of many more complex pages. Pages like nyan.cat are good way to test such tool and sadly the output is nothing like the website would render in a modern browser.
- ogre_codes 6y agoWe used this about 10 years ago and it worked... ok. The problem as some have pointed out already is it’s using a very out-of-date HTML/ JS engine. If you want to turn some HTML reports into PDFs, it’ll do the job. But there are ways to do this same thing with Puppeteer which uses a modern version of headless Chrome. It’s pretty straight forward and there are easy to chase down documents on how to do it.
- pedregal 6y agoThis is dope
- bmilleare 6y agoWe use wkhtmltopdf on AWS Lambda and have had zero issues in production for the last ~2 years. We're generating internal owned documents (invoices, letters, paged forms, etc) so I wouldn't necessarily trust it with untested input HTML but it really has been bulletproof for our use-case. At some point we'll likely move to chrome/puppeteer but in most of our tests the templates required modifications to mimic the existing PDFs so it's something we've been putting off.