Creating PDF/3-A compliant PDF and attaching ZUGFeRD invoice in Python
Contents
[
Hide
]
Attach ZUGFeRD to PDF
We recommend following steps to attach ZUGFeRD to PDF:
- Import the Aspose.PDF library and give it an alias of ap for convenience.
- Define the path to the directory where the input and output PDF files are located.
- Define the path to the PDF file that will be processed.
- Load the PDF file from the path variable and create a Document object.
- Create a FileSpecification object for the XML file that contains the invoice metadata. Use the path variable and a description string to create the FileSpecification object.
- Set the
mime_typeand theaf_relationshipproperties of the FileSpecification object totext/xmlandALTERNATIVE, respectively. - Add the fileSpecification object to the document object’s embedded files collection. This attaches the XML file to the PDF document as an invoice metadata file.
- Convert the PDF document to the PDF/A-3A format. Use the path to log file, the
PdfFormat.PDF_A_3Aenumeration, and theConvertErrorAction.DELETEenumeration to convert the document object. - Save the PDF document with the attached ZUGFeRD.
import sys
import os
import aspose.pdf as ap
def attach_invoice_zugferd_format(infile, invoice, outfile):
document = ap.Document(infile)
# Create a FileSpecification object for the XML file that contains the invoice metadata
description = "Invoice metadata conforming to ZUGFeRD standard"
file_specification = ap.FileSpecification(invoice, description)
# Set the MIME type and the AFRelationship properties of the embedded file
file_specification.mime_type = "text/xml"
file_specification.af_relationship = ap.AFRelationship.ALTERNATIVE
# Add the embedded file to the PDF document's embedded files collection
document.embedded_files.add("factur", file_specification)
# Convert the PDF document to the PDF/A-3A format
log_path = outfile.replace(".pdf", "_log.xml")
document.convert(log_path, ap.PdfFormat.PDF_A_3A, ap.ConvertErrorAction.DELETE)
document.save(outfile)