From f7f411ab776ac8874996ef8419f8a4dea725c3d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20R=C3=B8sdal?= Date: Wed, 2 Dec 2026 22:28:04 +0100 Subject: [PATCH] Support PDF/A extension schemas and Factur-X XMP metadata PDF/A allows XMP properties of a namespace that is not predefined only if the metadata describes the namespace in an extension schema (pdfaExtension:schemas). ZUGFeRD and Factur-X invoices are PDF/A-3 documents with such properties (fx:DocumentFileName, DocumentType, Version and ConformanceLevel). - PdfAExtensionSchema writes the extension schema container, with records for the schema and property descriptions. - FacturXSchema has the fx properties and their extension schema, for Factur-X and ZUGFeRD 2.1 and later, and for ZUGFeRD 2.0. - PdfWriter.addXmpSchema(XmpSchema) and addXmpSchema(XmpSchema, PdfAExtensionSchema.Schema) add schemas to the metadata that createXmpMetadata() creates. A Factur-X style PDF/A-3b invoice, with the invoice XML as an associated file, passes veraPDF. Co-Authored-By: Claude Opus 5.5 --- changelogs/4.0.1.md | 1 + .../java/org/openpdf/text/pdf/PdfWriter.java | 43 +++++ .../openpdf/text/xml/xmp/FacturXSchema.java | 182 ++++++++++++++++++ .../text/xml/xmp/PdfAExtensionSchema.java | 168 ++++++++++++++++ .../text/xml/xmp/PdfAExtensionSchemaTest.java | 152 +++++++++++++++ 5 files changed, 546 insertions(+) create mode 100644 openpdf-core/src/main/java/org/openpdf/text/xml/xmp/FacturXSchema.java create mode 100644 openpdf-core/src/main/java/org/openpdf/text/xml/xmp/PdfAExtensionSchema.java create mode 100644 openpdf-core/src/test/java/org/openpdf/text/xml/xmp/PdfAExtensionSchemaTest.java diff --git a/changelogs/4.0.1.md b/changelogs/4.0.1.md index b44aea3a0..8e4007f40 100644 --- a/changelogs/4.0.1.md +++ b/changelogs/4.0.1.md @@ -5,6 +5,7 @@ * **Associated files (PR #1806).** `PdfFileSpecification.setAFRelationship(PdfName)` and `associateWith(PdfDictionary)`, `PdfWriter.addAssociatedFile` and `addPageAssociatedFile`, and `PdfStamper.addAssociatedFile` link embedded files to the document, a page or another component, as PDF 2.0 and PDF/A-3 define them, for example the XML of a ZUGFeRD or Factur-X invoice. * **Tagged annotations (PR #1804, port of #1617).** `PdfStructureElement.addAnnotation(PdfAnnotation)` references a link or another annotation from the structure tree, as PDF/UA requires. The structure tree root gets a `/ParentTreeNextKey` entry. +* **PDF/A extension schemas and Factur-X metadata (PR #1808).** `PdfWriter.addXmpSchema(XmpSchema, PdfAExtensionSchema.Schema)` adds custom XMP properties and the PDF/A extension schema that describes them to the metadata of `createXmpMetadata()`. `FacturXSchema` has the `fx` properties of ZUGFeRD and Factur-X invoices, so that a PDF/A-3 invoice with the invoice XML as an associated file passes veraPDF. ## 🛠 Bug Fixes diff --git a/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfWriter.java b/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfWriter.java index 974207fb1..8d98f22fb 100644 --- a/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfWriter.java +++ b/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfWriter.java @@ -73,6 +73,8 @@ import org.openpdf.text.pdf.interfaces.PdfXConformance; import org.openpdf.text.pdf.internal.PdfVersionImp; import org.openpdf.text.pdf.internal.PdfXConformanceImp; +import org.openpdf.text.xml.xmp.PdfAExtensionSchema; +import org.openpdf.text.xml.xmp.XmpSchema; import org.openpdf.text.xml.xmp.XmpWriter; import java.awt.Color; import java.awt.color.ICC_Profile; @@ -89,6 +91,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -573,6 +576,16 @@ public class PdfWriter extends DocWriter implements * The files associated with the whole document, written as the {@code /AF} array of the catalog. */ private PdfArray documentAssociatedFiles; + + /** + * Additional XMP schemas that {@link #createXmpMetadata()} writes. + */ + private final List additionalXmpSchemas = new ArrayList<>(); + + /** + * The PDF/A extension schemas that describe {@link #additionalXmpSchemas}. + */ + private final PdfAExtensionSchema pdfAExtensionSchema = new PdfAExtensionSchema(); /** * The root of the page tree. */ @@ -1846,6 +1859,30 @@ public void createXmpMetadata() { setXmpMetadata(createXmpMetadataBytes()); } + /** + * Adds an XMP schema with properties of a standard namespace, for example Dublin Core, to the metadata that + * {@link #createXmpMetadata()} creates. Add the schemas before calling {@link #createXmpMetadata()}. + * + * @param schema the schema with its properties + */ + public void addXmpSchema(XmpSchema schema) { + additionalXmpSchemas.add(Objects.requireNonNull(schema, "schema")); + } + + /** + * Adds an XMP schema with properties of a custom namespace to the metadata that {@link #createXmpMetadata()} + * creates, together with the PDF/A extension schema that describes the namespace. PDF/A requires this description + * for every namespace that is not predefined. An example is the {@link org.openpdf.text.xml.xmp.FacturXSchema} of + * a ZUGFeRD or Factur-X invoice in a PDF/A-3 document. Add the schemas before calling {@link #createXmpMetadata()}. + * + * @param schema the schema with its properties + * @param description the description of the namespace and its properties + */ + public void addXmpSchema(XmpSchema schema, PdfAExtensionSchema.Schema description) { + addXmpSchema(schema); + pdfAExtensionSchema.addSchema(Objects.requireNonNull(description, "description")); + } + /** * @return an XmpMetadata byte array */ @@ -1853,6 +1890,12 @@ private byte[] createXmpMetadataBytes() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { XmpWriter xmp = new XmpWriter(baos, pdf.getInfo(), pdfxConformance.getPDFXConformance()); + for (XmpSchema schema : additionalXmpSchemas) { + xmp.addRdfDescription(schema); + } + if (!pdfAExtensionSchema.isEmpty()) { + xmp.addRdfDescription(pdfAExtensionSchema); + } xmp.close(); } catch (IOException ioe) { LOGGER.log(Level.WARNING, "Could not create the XMP metadata", ioe); diff --git a/openpdf-core/src/main/java/org/openpdf/text/xml/xmp/FacturXSchema.java b/openpdf-core/src/main/java/org/openpdf/text/xml/xmp/FacturXSchema.java new file mode 100644 index 000000000..d90f79f71 --- /dev/null +++ b/openpdf-core/src/main/java/org/openpdf/text/xml/xmp/FacturXSchema.java @@ -0,0 +1,182 @@ +package org.openpdf.text.xml.xmp; + +import java.util.List; + +/** + * The XMP properties of a ZUGFeRD or Factur-X electronic invoice ({@code fx} prefix). A ZUGFeRD 2.1 or later and + * Factur-X invoice is a PDF/A-3 document with the invoice XML as an associated file. The XMP metadata names that file + * and the profile of the invoice with these properties, and describes them in a PDF/A extension schema, see + * {@link #getPdfAExtensionSchema()}. + *
{@code
+ * FacturXSchema fx = new FacturXSchema();
+ * fx.setDocumentFileName(FacturXSchema.FACTUR_X_FILE_NAME);
+ * fx.setConformanceLevel(FacturXSchema.EN_16931);
+ * writer.addXmpSchema(fx, fx.getPdfAExtensionSchema());
+ * writer.createXmpMetadata();
+ * }
+ */ +public class FacturXSchema extends XmpSchema { + + /** + * The namespace URI of Factur-X 1.0 and ZUGFeRD 2.1 and later. + */ + public static final String FACTUR_X_NAMESPACE_URI = "urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#"; + /** + * The namespace URI of ZUGFeRD 2.0. + */ + public static final String ZUGFERD_2P0_NAMESPACE_URI = "urn:zugferd:pdfa:CrossIndustryDocument:invoice:2p0#"; + /** + * The namespace prefix of the properties. + */ + public static final String PREFIX = "fx"; + /** + * The name of the invoice XML in Factur-X and ZUGFeRD 2.1 and later. + */ + public static final String FACTUR_X_FILE_NAME = "factur-x.xml"; + /** + * The name of the invoice XML of an XRechnung profile in ZUGFeRD 2.1 and later. + */ + public static final String XRECHNUNG_FILE_NAME = "xrechnung.xml"; + /** + * The document type of an invoice. + */ + public static final String INVOICE = "INVOICE"; + /** + * The conformance level (profile) MINIMUM. + */ + public static final String MINIMUM = "MINIMUM"; + /** + * The conformance level (profile) BASIC WL. + */ + public static final String BASIC_WL = "BASIC WL"; + /** + * The conformance level (profile) BASIC. + */ + public static final String BASIC = "BASIC"; + /** + * The conformance level (profile) EN 16931, also called COMFORT. + */ + public static final String EN_16931 = "EN 16931"; + /** + * The conformance level (profile) EXTENDED. + */ + public static final String EXTENDED = "EXTENDED"; + /** + * The conformance level (profile) XRECHNUNG. + */ + public static final String XRECHNUNG = "XRECHNUNG"; + + /** + * The property with the name of the invoice XML. + */ + public static final String DOCUMENT_FILE_NAME = "fx:DocumentFileName"; + /** + * The property with the document type. + */ + public static final String DOCUMENT_TYPE = "fx:DocumentType"; + /** + * The property with the version of the XML schema of the invoice. + */ + public static final String VERSION = "fx:Version"; + /** + * The property with the conformance level (profile) of the invoice. + */ + public static final String CONFORMANCE_LEVEL = "fx:ConformanceLevel"; + + private static final long serialVersionUID = -4520412961417532094L; + + private final String namespaceUri; + + /** + * Creates the properties of a Factur-X or ZUGFeRD 2.1 or later invoice, with the document type + * {@link #INVOICE} and the version {@code 1.0}. + */ + public FacturXSchema() { + this(FACTUR_X_NAMESPACE_URI); + } + + /** + * Creates the properties of an invoice with the document type {@link #INVOICE} and the version {@code 1.0}. + * + * @param namespaceUri the namespace URI, {@link #FACTUR_X_NAMESPACE_URI} or {@link #ZUGFERD_2P0_NAMESPACE_URI} + */ + public FacturXSchema(String namespaceUri) { + super("xmlns:" + PREFIX + "=\"" + namespaceUri + "\""); + this.namespaceUri = namespaceUri; + setDocumentType(INVOICE); + setVersion("1.0"); + } + + /** + * Sets the name of the invoice XML, which is the name of the associated file. + * + * @param fileName the file name, for example {@link #FACTUR_X_FILE_NAME} + */ + public void setDocumentFileName(String fileName) { + setProperty(DOCUMENT_FILE_NAME, fileName); + } + + /** + * Sets the document type. + * + * @param documentType the document type, usually {@link #INVOICE} + */ + public void setDocumentType(String documentType) { + setProperty(DOCUMENT_TYPE, documentType); + } + + /** + * Sets the version of the XML schema of the invoice. + * + * @param version the version, for example {@code 1.0} + */ + public void setVersion(String version) { + setProperty(VERSION, version); + } + + /** + * Sets the conformance level (profile) of the invoice. + * + * @param conformanceLevel the conformance level, for example {@link #EN_16931} + */ + public void setConformanceLevel(String conformanceLevel) { + setProperty(CONFORMANCE_LEVEL, conformanceLevel); + } + + /** + * Gets the namespace URI of the properties. + * + * @return the namespace URI + */ + public String getNamespaceUri() { + return namespaceUri; + } + + /** + * Gets the PDF/A extension schema that describes these properties. PDF/A requires it in the XMP metadata. + * + * @return the description of the schema + */ + public PdfAExtensionSchema.Schema getPdfAExtensionSchema() { + String external = PdfAExtensionSchema.Property.EXTERNAL; + return new PdfAExtensionSchema.Schema("Factur-X PDFA Extension Schema", namespaceUri, PREFIX, List.of( + new PdfAExtensionSchema.Property("DocumentFileName", "Text", external, + "The name of the embedded XML document"), + new PdfAExtensionSchema.Property("DocumentType", "Text", external, + "The type of the hybrid document in capital letters, e.g. INVOICE or ORDER"), + new PdfAExtensionSchema.Property("Version", "Text", external, + "The actual version of the standard applying to the embedded XML document"), + new PdfAExtensionSchema.Property("ConformanceLevel", "Text", external, + "The conformance level of the embedded XML document"))); + } + + @Override + public synchronized boolean equals(Object o) { + return super.equals(o) && o instanceof FacturXSchema other && namespaceUri.equals(other.namespaceUri); + } + + @Override + public synchronized int hashCode() { + return 31 * super.hashCode() + namespaceUri.hashCode(); + } +} diff --git a/openpdf-core/src/main/java/org/openpdf/text/xml/xmp/PdfAExtensionSchema.java b/openpdf-core/src/main/java/org/openpdf/text/xml/xmp/PdfAExtensionSchema.java new file mode 100644 index 000000000..78d5c310a --- /dev/null +++ b/openpdf-core/src/main/java/org/openpdf/text/xml/xmp/PdfAExtensionSchema.java @@ -0,0 +1,168 @@ +package org.openpdf.text.xml.xmp; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +/** + * The PDF/A extension schema container ({@code pdfaExtension:schemas}) of the XMP metadata. PDF/A (ISO 19005-1, 6.7.8, + * and ISO 19005-2 and -3, 6.6.2.3) allows XMP properties of a namespace that is not predefined only if the metadata + * describes that namespace in an extension schema. Examples are the {@code fx} properties of ZUGFeRD and Factur-X + * electronic invoices, see {@link FacturXSchema}. + *

+ * Add the descriptions with {@link #addSchema(Schema)} and write the container with + * {@link XmpWriter#addRdfDescription(XmpSchema)}, or let {@link org.openpdf.text.pdf.PdfWriter#createXmpMetadata()} + * write it for the schemas added with + * {@link org.openpdf.text.pdf.PdfWriter#addXmpSchema(XmpSchema, PdfAExtensionSchema.Schema)}. + */ +public class PdfAExtensionSchema extends XmpSchema { + + /** + * The namespace URI of the extension schema container. + */ + public static final String NAMESPACE_URI = "http://www.aiim.org/pdfa/ns/extension/"; + /** + * The namespace URI of the schema descriptions. + */ + public static final String SCHEMA_NAMESPACE_URI = "http://www.aiim.org/pdfa/ns/schema#"; + /** + * The namespace URI of the property descriptions. + */ + public static final String PROPERTY_NAMESPACE_URI = "http://www.aiim.org/pdfa/ns/property#"; + + private static final long serialVersionUID = 2862174325482734528L; + + private final List schemas = new ArrayList<>(); + + /** + * Creates an empty extension schema container. + */ + public PdfAExtensionSchema() { + super("xmlns:pdfaExtension=\"" + NAMESPACE_URI + "\" xmlns:pdfaSchema=\"" + SCHEMA_NAMESPACE_URI + + "\" xmlns:pdfaProperty=\"" + PROPERTY_NAMESPACE_URI + "\""); + } + + /** + * Adds the description of a schema. A description with the same namespace URI as one that was added before is + * ignored. + * + * @param schema the description of the schema + */ + public void addSchema(Schema schema) { + Objects.requireNonNull(schema, "schema"); + boolean known = schemas.stream().anyMatch(s -> s.namespaceUri().equals(schema.namespaceUri())); + if (!known) { + schemas.add(schema); + } + } + + /** + * Gets the descriptions of the schemas. + * + * @return the descriptions, in the order they were added + */ + public List getSchemas() { + return Collections.unmodifiableList(schemas); + } + + /** + * Tells if the container has no schema descriptions. + * + * @return {@code true} if no schema was added + */ + @Override + public synchronized boolean isEmpty() { + return schemas.isEmpty(); + } + + @Override + public synchronized String toString() { + StringBuilder buf = new StringBuilder(""); + for (Schema schema : schemas) { + buf.append(""); + element(buf, "pdfaSchema:schema", schema.description()); + element(buf, "pdfaSchema:namespaceURI", schema.namespaceUri()); + element(buf, "pdfaSchema:prefix", schema.prefix()); + buf.append(""); + for (Property property : schema.properties()) { + buf.append(""); + element(buf, "pdfaProperty:name", property.name()); + element(buf, "pdfaProperty:valueType", property.valueType()); + element(buf, "pdfaProperty:category", property.category()); + element(buf, "pdfaProperty:description", property.description()); + buf.append(""); + } + buf.append(""); + buf.append(""); + } + buf.append(""); + return buf.toString(); + } + + @Override + public synchronized boolean equals(Object o) { + return o instanceof PdfAExtensionSchema other && schemas.equals(other.schemas); + } + + @Override + public synchronized int hashCode() { + return schemas.hashCode(); + } + + private static void element(StringBuilder buf, String name, String value) { + buf.append('<').append(name).append('>').append(escape(value)).append("'); + } + + /** + * The description of an XMP schema ({@code pdfaSchema}). + * + * @param description a description of the schema, for example "Factur-X PDFA Extension Schema" + * @param namespaceUri the namespace URI of the schema + * @param prefix the preferred namespace prefix of the schema + * @param properties the descriptions of the properties of the schema + */ + public record Schema(String description, String namespaceUri, String prefix, List properties) { + + /** + * Creates the description of an XMP schema. + */ + public Schema { + Objects.requireNonNull(description, "description"); + Objects.requireNonNull(namespaceUri, "namespaceUri"); + Objects.requireNonNull(prefix, "prefix"); + properties = List.copyOf(properties); + } + } + + /** + * The description of an XMP property ({@code pdfaProperty}). + * + * @param name the name of the property, without the namespace prefix + * @param valueType the value type, for example {@code Text}, {@code Date} or {@code Integer} + * @param category {@link #EXTERNAL} if the value is set by the user, {@link #INTERNAL} if it is computed from the + * document + * @param description a description of the property + */ + public record Property(String name, String valueType, String category, String description) { + + /** + * The category of a property whose value is set by the user. + */ + public static final String EXTERNAL = "external"; + /** + * The category of a property whose value is computed from the document. + */ + public static final String INTERNAL = "internal"; + + /** + * Creates the description of an XMP property. + */ + public Property { + Objects.requireNonNull(name, "name"); + Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(category, "category"); + Objects.requireNonNull(description, "description"); + } + } +} diff --git a/openpdf-core/src/test/java/org/openpdf/text/xml/xmp/PdfAExtensionSchemaTest.java b/openpdf-core/src/test/java/org/openpdf/text/xml/xmp/PdfAExtensionSchemaTest.java new file mode 100644 index 000000000..719a44c8a --- /dev/null +++ b/openpdf-core/src/test/java/org/openpdf/text/xml/xmp/PdfAExtensionSchemaTest.java @@ -0,0 +1,152 @@ +package org.openpdf.text.xml.xmp; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.awt.color.ColorSpace; +import java.awt.color.ICC_Profile; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.openpdf.text.Document; +import org.openpdf.text.Font; +import org.openpdf.text.PageSize; +import org.openpdf.text.Paragraph; +import org.openpdf.text.pdf.BaseFont; +import org.openpdf.text.pdf.PdfDate; +import org.openpdf.text.pdf.PdfDictionary; +import org.openpdf.text.pdf.PdfFileSpecification; +import org.openpdf.text.pdf.PdfName; +import org.openpdf.text.pdf.PdfReader; +import org.openpdf.text.pdf.PdfStream; +import org.openpdf.text.pdf.PdfWriter; +import org.verapdf.gf.model.GFModelParser; +import org.verapdf.pdfa.PDFAValidator; +import org.verapdf.pdfa.flavours.PDFAFlavour; +import org.verapdf.pdfa.results.TestAssertion; +import org.verapdf.pdfa.results.ValidationResult; +import org.verapdf.pdfa.validation.validators.ValidatorFactory; + +/** + * Tests the PDF/A extension schemas and the Factur-X XMP properties, and validates a Factur-X style PDF/A-3 invoice + * with veraPDF. + */ +class PdfAExtensionSchemaTest { + + private static final String FONT = "/fonts/liberation/LiberationSerif-Regular.ttf"; + private static final byte[] INVOICE_XML = ("" + + "") + .getBytes(StandardCharsets.UTF_8); + + @Test + void extensionSchemaDescribesNamespaceAndProperties() { + PdfAExtensionSchema extension = new PdfAExtensionSchema(); + extension.addSchema(new PdfAExtensionSchema.Schema("Example & Co schema", "urn:example:ns#", "ex", + List.of(new PdfAExtensionSchema.Property("Size", "Integer", PdfAExtensionSchema.Property.INTERNAL, + "The size ")))); + + String xml = extension.toString(); + + assertThat(extension.getXmlns()).contains("xmlns:pdfaExtension=\"http://www.aiim.org/pdfa/ns/extension/\""); + assertThat(xml).startsWith("") + .contains("Example & Co schema") + .contains("urn:example:ns#") + .contains("ex") + .contains("Size") + .contains("Integer") + .contains("internal") + .contains("The size <in bytes>"); + } + + @Test + void sameNamespaceIsDescribedOnce() { + PdfAExtensionSchema extension = new PdfAExtensionSchema(); + FacturXSchema fx = new FacturXSchema(); + extension.addSchema(fx.getPdfAExtensionSchema()); + extension.addSchema(fx.getPdfAExtensionSchema()); + + assertThat(extension.getSchemas()).hasSize(1); + } + + @Test + void facturXSchemaHasDefaultsAndNamespace() { + FacturXSchema fx = new FacturXSchema(); + fx.setDocumentFileName(FacturXSchema.FACTUR_X_FILE_NAME); + fx.setConformanceLevel(FacturXSchema.EN_16931); + + assertThat(fx.getXmlns()).isEqualTo("xmlns:fx=\"" + FacturXSchema.FACTUR_X_NAMESPACE_URI + "\""); + assertThat(fx.toString()).contains("INVOICE") + .contains("1.0") + .contains("factur-x.xml") + .contains("EN 16931"); + assertThat(new FacturXSchema(FacturXSchema.ZUGFERD_2P0_NAMESPACE_URI).getPdfAExtensionSchema() + .namespaceUri()).isEqualTo(FacturXSchema.ZUGFERD_2P0_NAMESPACE_URI); + } + + @Test + void facturXInvoiceIsPdfA3Compliant() throws Exception { + byte[] pdf = createFacturXInvoice(); + + try (PdfReader reader = new PdfReader(pdf)) { + String xmp = new String(reader.getMetadata(), StandardCharsets.UTF_8); + assertThat(xmp).contains("3") + .contains("EN 16931") + .contains("" + FacturXSchema.FACTUR_X_NAMESPACE_URI); + } + assertThat(validate(pdf, PDFAFlavour.PDFA_3_B)).as("veraPDF PDF/A-3b violations").isEmpty(); + } + + private static byte[] createFacturXInvoice() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (Document document = new Document(PageSize.A4)) { + PdfWriter writer = PdfWriter.getInstance(document, out); + writer.setPDFXConformance(PdfWriter.PDFA3B); + document.addTitle("Invoice 2026-001"); + document.addAuthor("OpenPDF"); + document.open(); + writer.setOutputIntents("sRGB IEC61966-2.1", null, "http://www.color.org", "sRGB IEC61966-2.1", + ICC_Profile.getInstance(ColorSpace.CS_sRGB)); + + BaseFont baseFont = BaseFont.createFont("LiberationSerif-Regular.ttf", BaseFont.WINANSI, + BaseFont.EMBEDDED, true, readResource(FONT), null); + document.add(new Paragraph("Invoice 2026-001", new Font(baseFont, 14))); + + PdfDictionary params = new PdfDictionary(); + params.put(PdfName.MODDATE, new PdfDate()); + PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer, null, + FacturXSchema.FACTUR_X_FILE_NAME, INVOICE_XML, "text/xml", params, PdfStream.BEST_COMPRESSION); + fs.setAFRelationship(PdfName.ALTERNATIVE); + writer.addAssociatedFile("Factur-X invoice", fs); + + FacturXSchema fx = new FacturXSchema(); + fx.setDocumentFileName(FacturXSchema.FACTUR_X_FILE_NAME); + fx.setConformanceLevel(FacturXSchema.EN_16931); + writer.addXmpSchema(fx, fx.getPdfAExtensionSchema()); + writer.createXmpMetadata(); + } + return out.toByteArray(); + } + + private static List validate(byte[] pdf, PDFAFlavour flavour) throws Exception { + try (GFModelParser parser = GFModelParser.createModelWithFlavour(new ByteArrayInputStream(pdf), flavour)) { + PDFAValidator validator = ValidatorFactory.createValidator(flavour, false, 100); + ValidationResult result = validator.validate(parser); + return result.getTestAssertions().stream() + .filter(assertion -> assertion.getStatus() == TestAssertion.Status.FAILED) + .map(assertion -> assertion.getRuleId().getClause() + "-" + assertion.getRuleId().getTestNumber() + + ": " + assertion.getMessage()) + .distinct() + .toList(); + } + } + + private static byte[] readResource(String name) throws IOException { + try (InputStream in = PdfAExtensionSchemaTest.class.getResourceAsStream(name)) { + assertThat(in).as(name).isNotNull(); + return in.readAllBytes(); + } + } +}