Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Make an invoice Factur-X

You’ll turn a PDF/A-3 invoice into a Factur-X one: the same file, carrying the invoice’s data as XML for the buyer’s software to read. You need this for e-invoicing in France and Germany, where Factur-X and ZUGFeRD are one format.

The function opens the invoice, attaches factur-x.xml as the document’s alternative form, records the four Factur-X properties in the XMP metadata, and saves:

packages/sdk/examples/attach-factur-x.ts
// Make a PDF/A-3 invoice a Factur-X (ZUGFeRD) one: attach its structured data as factur-x.xml,
// the document's alternative form, and record the Factur-X properties in the XMP metadata, which
// keeps the file valid PDF/A-3.
// The XML here is a placeholder: a real one is the invoice in the EN 16931 Cross Industry Invoice
// syntax, made by your invoicing system. The file must already be PDF/A-3.
import { Document } from "@laminapdf/sdk";
/** The Factur-X XMP namespace and prefix (Factur-X 1.0, §6.2.2). */
const FX = "urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#";
/** A placeholder for the invoice's Cross Industry Invoice XML. */
const XML = `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100">
<!-- The invoice's data, from your invoicing system. -->
</rsm:CrossIndustryInvoice>
`;
/**
* The invoice with factur-x.xml attached and the Factur-X properties recorded, saved incrementally.
*
* @param savedAt - the time of the save, as a PDF date (`D:20261110093000Z`) your app takes
* from its clock.
*/
export function attachFacturX(bytes: Uint8Array, savedAt: string): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
// PDF/A-3 asks every attached file for an association, a MIME type and a modification date.
const xml = doc.attachFileOp("factur-x.xml", new TextEncoder().encode(XML), "text/xml", {
attachTo: "document",
relationship: "alternative",
description: "Factur-X invoice",
modified: savedAt,
});
doc.transact((tx) => {
tx.apply({ op: "attachFile", args: xml });
const fx = (name: string, value: string) => tx.setXmpProperty(FX, "fx", name, { type: "text", value });
fx("DocumentType", "INVOICE");
fx("DocumentFileName", "factur-x.xml");
fx("Version", "1.0");
fx("ConformanceLevel", "MINIMUM");
});
// An attachment that breaks the file's PDF/A level says so before the save; this one keeps it.
for (const warning of doc.plan({ mode: "incremental", metadataDate: savedAt }).conformanceWarnings) {
console.warn(warning.code);
}
return doc.save({ mode: "incremental", metadataDate: savedAt });
}
Page 1 of attach_factur_x.pdf
Open attach_factur_x.pdf (1 page, 15 KB)

The result is the original bytes followed by one revision. It lists factur-x.xml beside the invoice’s own XML and still meets PDF/A-3b.

  • attachFileOp(name, bytes, mime, options) keeps the bytes for the session and makes the attachFile edit; attach_file does the same in Rust. attachFile applies it at once.
  • attachTo: "document" associates the file with the whole document, and relationship: "alternative" says it is another form of the same content. PDF/A-3 asks every attached file for an association, a MIME type and a modification date (modified), which your app takes from its clock.
  • Each file records its size, its dates and an MD5 checksum. Reading it back with attachmentBytes compares the checksum, so a file changed after it was attached shows as changed.
  • The fx: properties are not part of PDF/A, so Lamina PDF declares them in the metadata’s extension schema, as for any property of your own.
  • On a file that claims PDF/A-1 or PDF/A-2, plan() warns pdfa-attachment-forbidden: those levels allow no attached files but PDF/A ones.
  • The XML: replace the placeholder with the invoice in the EN 16931 Cross Industry Invoice syntax, from your invoicing system. Keep the name factur-x.xml; ZUGFeRD 1 files use ZUGFeRD-invoice.xml.
  • The profile: set ConformanceLevel to the profile your XML follows: MINIMUM, BASIC WL, BASIC, EN 16931 or EXTENDED.
  • A file that isn’t PDF/A-3 yet: make it with the builder, as A PDF/A-3 invoice with XML does. Converting another file to PDF/A is a separate job.

Next: Attachments, for reading, saving out and deleting attached files.