from Patrick Gundlach |

High speed PDF generation with boxes and glue

Categories: Development

I have been playing with electronic invoices (especially the German and French ZUGFeRD/Factur-X format) for a while now. The speedata Publisher has support for ZUGFeRD since 2017.

The next step is to make (my other PDF generation software) boxes and glue work with electronic invoices. For that, I use the new command line interface for boxes and glue (which I will describe in a future blog post).

There only a few things that need to be done to make this work:

  • You need an XML file that contains the invoice data. This is usually generated by an accounting software. This must be conformant to the ZUGFeRD XML schema (EN 16931-1).
  • Attach the XML to the generated PDF.
  • Create a visual representation of the invoice data in the PDF.

There is an example of the script at GitHub. Get the software as described on https://github.com/boxesandglue/cli and run

bag main.rsr

Which generates a file out0.pdf from the sample zugferd.xml file.

The console output is something like

Define font family (name=text,id=0)
Load image (filename=img/logo.pdf)
Load font (filename=../../fonts/crimsonpro/CrimsonPro-Regular.ttf)
Load font (filename=../../fonts/crimsonpro/CrimsonPro-Bold.ttf)
Add attachment (filename=factur-x.xml)
Write image to PDF (filename=img/logo.pdf)
Write font to PDF (filename=../../fonts/crimsonpro/CrimsonPro-Regular.ttf,psname=CrimsonPro-Regular)
Write font to PDF (filename=../../fonts/crimsonpro/CrimsonPro-Bold.ttf,psname=CrimsonPro-Bold)
Output written (filename=out0.pdf,bytes=28073)
finished in 26.91ms

27 milliseconds (on my 2021 MacBook Pro) for a PDF is not bad, I guess. This includes loading fonts and the PDF logo, parsing the XML file and writing the PDF, subsetting and including fonts and attaching the XML file.

The 27 milliseconds include some startup time, so let me check what happens when I run the script 1000 times (set maxdocuments in the script to 1000):

...
finished in 14224.74ms

This is about 14 milliseconds for one document or approximately 70 documents per second.