ZUGFeRD and Ghostscript - How to Create Industry Standard and Compliant PDF E-invoices
Jamie Lemon·October 23, 2024

As touched upon in our previous blog post 2025 will be a big year for ZUGFeRD and e-invoicing. With that in mind, how can we stay ahead of the game and ensure that we have the capability and the tools to create ZUGFeRD PDFs?
This blog post assumes some knowledge of the ZUGFeRD format and what it is for, if you need a refresher then please read Understanding ZUGFeRD: The Future of E-Invoices
Why Choose Ghostscript for PDF Generation?
Ghostscript is a versatile open-source tool for rendering, converting, and managing PDF and PostScript files. It has been under active development for over 30 years and has been ported to several different systems during this time.
Many companies use Ghostscript as part of automated document workflows as it can be scripted to perform tasks like converting documents, adding watermarks, or extracting pages from a PDF.
Ghostscript’s Role in Creating ZUGFeRD-Compliant PDFs
Ghostscript plays a role in generating PDF/A compliant documents, which are required for long-term archiving and e-invoicing standards like ZUGFeRD and Factur-X. This ensures that businesses can create PDFs that comply with specific archival and e-invoicing standards.
Ghostscript's versatility, ability to process complex PDF and PostScript operations, and open-source nature make it a critical tool in the software industry for document processing.
How to Guide
Step 1: Creating a PDF/A compliant PDF
The first step is to ensure the input PDF is PDF/A compliant. PDF/A is a specialized version of the PDF format designed for long-term archiving of digital documents. Its main purpose is to ensure that documents can be reproduced accurately in the future, preserving their visual appearance and structure.
Key Features of PDF/A:
- Self-Contained: PDF/A files must be self-contained, meaning that all elements needed to render the document—such as fonts, images, and color profiles—are embedded within the PDF itself. This ensures that the document will look the same even if software, hardware, or operating systems change.
- No External Dependencies: PDF/A prohibits the use of certain features that might make a document dependent on external resources. For example, JavaScript, audio/video content, and external links are not allowed, as they could hinder the ability to accurately reproduce the document in the future.
- ISO Standard: PDF/A is an ISO-standardized format, with the standard published under ISO 19005. This makes it suitable for industries where compliance and regulatory standards for document preservation are critical, such as legal, government, and archival sectors.
Ghostscript comes in handy to ensure that your input PDF will adhere to this compliance and has a command line switch which can be used to generate a compliant PDF as follows:
-dPDFA=3
This tells Ghostscript to use version 3 of the PDF/A standard - this version permits files to be embedded into the PDF and is obviously required as we need to embed the XML invoice data into the PDF later on. For full information refer to the Ghostscript documentation on creating PDF/A files.
Step 2: Preparing the XML Data for Your Invoice
Ghostscript itself won’t validate the XML schema for ZUGFeRD, rather it will assume you have supplied a compliant XML file for your invoice. Therefore, you should ensure that you have an XML file which is correctly formatted.
The ZUGFeRD XML follows a structure that organizes data into elements that represent key information, such as:
- Header Information: Contains general information about the invoice, such as the invoice number, date, currency, and reference to related documents.
- Parties Involved: Details about the seller, buyer, and any intermediaries involved in the transaction. This includes addresses, contact details, and tax identification numbers.
- Line Items: A detailed list of the goods or services provided, including descriptions, quantities, unit prices, and applicable taxes.
- Tax Breakdown: Specifies the tax amounts and types (e.g., VAT), providing a breakdown of the total amount payable.
- Payment Instructions: Information on how to settle the invoice, including bank account details and payment terms.
Example of a Simplified ZUGFeRD XML
A basic snippet of the XML structure might look like this:
<rsm:crossindustryinvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100">
<rsm:exchangeddocumentcontext>
<ram:guidelinespecifieddocumentcontextparameter>
<ram:id>ZUGFeRD</ram:id>
</ram:guidelinespecifieddocumentcontextparameter>
</rsm:exchangeddocumentcontext>
<rsm:exchangeddocument>
<ram:id>INV-12345</ram:id>
<ram:issuedatetime>
<udt:datetimestring format="102">20231003</udt:datetimestring>
</ram:issuedatetime>
</rsm:exchangeddocument>
<rsm:supplychaintradetransaction>
<!-- Line item details go here -->
</rsm:supplychaintradetransaction>
</rsm:crossindustryinvoice>
For a full example see: https://www.mustangproject.org/zugferd/ which has a sample PDF with the XML as follows: https://www.mustangproject.org/files/ZUGFeRD-invoice.xml. As stated this blog post will not delve into the e-invoicing intricacies of the XML here!
So let’s assume you have compliant XML and are ready to embed the file into your PDF and move onto the next step.
Step 3: Embedding the XML Data and creating your ZUGFeRD PDF
Here is where the knowledge, open source contribution and time investment put into Ghostscript development over the years comes in. We’ve bundled up a command line which references a prepared PostScript file (zugferd.ps) to do the hard lifting for you and ensures that everything is in place to make it as easy as possible.
Note
💡 Note: Ensure to use Ghostscript version 10.03.01 or HEAD from the repository as there is a bug with Ghostscript 10.04.0 in the zugferd.ps file
Let’s assume you put the following files into /usr/home/me/zugferd/
:
zugferd.ps ( the handy PostScript file to handle the commands )
default_rgb.icc ( an ICC color profile )
invoice.xml
( the file you created / acquired in the previous stage )
Then we need to run the following command:
gs
--permit-file-read=/usr/home/me/zugferd/
-sDEVICE=pdfwrite
-dPDFA=3
-sColorConversionStrategy=RGB
-sZUGFeRDXMLFile=/usr/home/me/zugferd/invoice.xml
-sZUGFeRDProfile=/usr/home/me/zugferd/default_rgb.icc
-o /usr/home/me/zugferd/output.pdf
/usr/home/me/zugferd/zugferd.ps
/usr/home/me/zugferd/input.pdf
Let’s quickly break down what is happening here in each line:
Invoke Ghostscript:
gs
Grant access to the folder we want to read from:
--permit-file-read=/usr/home/me/zugferd/
Select a device to write a PDF:
-sDEVICE=pdfwrite
Define the PDF A version:
-dPDFA=3
Define the color conversion:
-sColorConversionStrategy=RGB
Define the path to the invoice XML:
-sZUGFeRDXMLFile=/usr/home/me/zugferd/invoice.xml
Define the path to the ICC profile:
-sZUGFeRDProfile=/usr/home/me/default_rgb.icc
Defines the output file:
-o /usr/home/me/zugferd/output.pdf
Defines the path to the zugferd.ps PostScript file:
/usr/home/me/zugferd/zugferd.ps
Defines the path to the input PDF file:
/usr/home/me/zugferd/input.pdf
Note there are further options for -sZUGFeRDVersion
& -sZUGFeRDConformanceLevel
, these have sensible defaults, however you can find out more about them in the Ghostscript ZUGFeRD documentation.
Once the command is run, all being well, you should have your ZUGFeRD PDF as “output.pdf” ready to go!
Validating your ZUGFeRD PDF
There are a few user-friendly validation options for businesses seeking a service based solution, these require sign up, but allow users to validate ZUGFeRD invoices directly by uploading their PDF/A-3 documents or XML.
Alternatively you can use Artifex’s command line mutool for basic ZUGFeRD validation with:
mutool info -Z input.pdf
Benefits of Using ZUGFeRD and Ghostscript
Utilizing Ghostscript as part of your solution brings the following benefits:
- ZUGFeRD and Ghostscript helps businesses comply with EU and international e-invoicing regulations.
- Reduces the risk of non-compliance when working with government and large enterprise clients.
- This solution is scalable, making it suitable for small businesses needing a simple solution as well as larger enterprises managing high invoice volumes.
In conclusion, if you need to get ahead with building a future-ready e-invoicing system then Ghostscript’s capability offers a powerful, compliant solution for modern e-invoicing.
We hope this “How To” blog introduces how to create e-invoices with ease using Ghostscript, if you have any questions please reach out to us on the Ghostscript Discord channel.