Gofpdf: Generate PDFs from Go with No Dependencies
github.com
github.com
Create pdf with Chrome headless: chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/
Source: https://developers.google.com/web/updates/2017/04/headless-c...
The API can be made much more high level, because of all functionality and packages LaTeX has. The most important thing is to have a good escape function that is used by default for all text, otherwise it's very easy to execute arbitrary shell commands for an attacker. If anyone wants to create a Go package similar to mine, feel free to contact me if you want any more advice (email in github profile).
It definitely is a large, and more importantly external, dependency though. However, if you control the installation environment of your software this isn't really a problem. For all use cases where I had to generate pdfs this was the case (webapps and local one off scripts).
Can't say it's completely painless due to the imperative nature of the API but it served me well. If you're reading this, thank you maintainers!
If you just need to post your own HTML/CSS and get a PDF, then I'd recommend DocRaptor [2].
I wouldn't recommend it however if your design is complex, you're on a very tight schedule and you can stomach Webkit. Truth is Gofpdf requires careful coding and testing to make sure you're drawing things in the right places. You draw using simple primitives positioned around the page with a "cursor"; not something that lends itself to pumping out flashy results in little time.
If you want to create something similar, be sure to escape untrusted input though. With an escape function similar to the one in my library. Otherwise it's very easy for an attacker to execute arbitrary commands.
[1] https://github.com/ajstarks/deck/
[2] https://github.com/ajstarks/deck/tree/master/cmd/pdfdeck
[3] https://speakerdeck.com/ajstarks/the-other-side-of-go-progra...
[4] https://speakerdeck.com/ajstarks/a-view-of-design-and-user-e...
It's not just a HTML to PDF library, it lets you make very precise stuff, and it was very useful back in the day.