Ask HN: How to generate PDFs from the back end?

12 points by michidk ↗ HN
Ask HN: How to generate PDFs (certificates/invoices) from the backend? Can you recommend a easy to integrate service?

32 comments

[ 4.0 ms ] story [ 33.3 ms ] thread
What do you want to do? I've done some crude ones by having a program dump values into a LaTeX template that I then ran through pdflatex.
Like generating invoices. LaTeX could be a solutions, but since it's rather slow I would have to build a queuing system around it... Maybe there is already some neat service out there, that does this?
If you don't mind sending your data to third parties to generate pdfs for you then I'm sure you'll find plenty solutions.

Otherwise perhaps you can find some language specific libraries, like the python based one mentioned above.

You can also use puppeteer or playwright to create pdfs from web pages.

A one-or-two page document like an invoice would take a second or so on pdflatex, queueing might be overdoing it ...
Workflow I've used previously:

1. Render html (easy with a templating language e.g. Jinja for Python)

2. Turn the html into a pdf (e.g. wkhtmltopdf in Python)

But this is a rather bloated and slow approach since it's based on a webbrowser, right? I think it also requires some desktop environment like x11?
Headless Chromium is the way. Bloated maybe, but does the job.
I inherited a codebase that uses this approach. You can use xvfb to get around the x11 frame buffer requirement. It works, but we are looking to migrate to another solution, since it appears that wkhtmltopdf is no longer maintained.
I tried wkhtmltopdf some months ago and the PDFs it generated where nowhere near the original documents.
there is borbpdf.com
Great! Though I guess it's missing the typesetting capabilities of LaTeX
On the command line, pdftk.

If you are using Linux, it is probably a package in your distro.

https://linux.die.net/man/1/pdftk

The homepage for the free (server) version.

https://www.pdflabs.com/tools/pdftk-server/

Wow this is so cool! Not sure it will fit my use-case, since I am still looking for something that can handle proper typesetting. But I bookmarked this - very handy!
The question did not mention typesetting.

Per *NIX philosophy, that would be a separate tool in a pipeline.

Troff perhaps?

But, this starts to look like a hire a consultant problem where paying for expertise is more effective than learning through mistakes.

Because PDF does not imply typesetting…or layout which is a third thing technically.

Or “proper typesetting” which is another layer of craft. Which you might have staffed, and if so those are the people to talk to for recommendations.

Aside from troff, TeX or LaTeX can be used for typesetting and generating PDFs.
You need to at least tell us what your backend is.

Have you googled your question yet? Is there an issue with the solutions you did find? Why would you want an external service? You could be exposing private info using external services.

Yes, I googled >.>; The question was asked openly by intend, since I don't care which backend technology such solution might use. I want an external service, since I do not want to set up stuff like a LaTeX runner myself and handling faults, scaling etc. Fair point with the private info though!
I can absolutely recommend "gotenberg" in a docker and some react web app to render the actual web app!
Clarification question: what is your backend tech?
We are using z3c.rml (https://github.com/zopefoundation/z3c.rml) based on ReportLab. Layout is written in HTML-like XML and render performance is pretty good.
This looks incredibly interesting. I have previously used the free version of Reportlab, but this templating system would be significantly easier than futzing with the Reportlab DSL.
What backend exactly? If you're into old school PHP, try FPDF maybe. But you'll probabl have to write your own layout code.
I'm curious maybe anyone have experience in large bank or broker how do they generate statements for customers ?
Generate from what format and in what language?

You could run pandoc in a lambda perhaps?

I recently found out that a Norwegian government agency open sourced a micro service for generating PDF

I haven't used it, but it looks promising. You create templates based on html (Handlebars templates), which should make it easy to get started.

https://github.com/navikt/pdfgen

It may be worth a look

Markdown template + pandoc would be a very minimal typeset solution.
ReactPDF (https://react-pdf.org/) if: a) React is an option b) you (can) use nodejs on backend c) PDF preview is required on frontend (optional) d) you need to generate same PDFs on backend (optional)
We use PDFlib, but we started using it a long time ago when there wasn't a lot of good open source options for building pdfs. PDFlib isn't open source or free. So far PDFlib has worked well and not been so expensive that we've needed to look for anything else.

https://www.pdflib.com/

Generating PDFs is a pain with most libraries, especially if the content is dynamic and should span multiple pages.

If you can use NodeJS on your backend, you might want to take a look at my PDF Library: https://github.com/DevLeoko/painless-pdf