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_type
and theaf_relationship
properties of the FileSpecification object totext/xml
andALTERNATIVE
, 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_3A
enumeration, and theConvertErrorAction.DELETE
enumeration to convert the document object. - Save the PDF document with the attached ZUGFeRD.
import aspose.pdf as ap
# Define the path to the directory where the input and output PDF files are located
_dataDir = "./"
# Load the PDF file that will be processed
path = _dataDir + "ZUGFeRD/ZUGFeRD-test.pdf"
document = ap.Document(path)
# Create a FileSpecification object for the XML file that contains the invoice metadata
description = "Invoice metadata conforming to ZUGFeRD standard"
path = _dataDir + "ZUGFeRD/factur-x.xml"
fileSpecification = ap.FileSpecification(path, description)
# Set the MIME type and the AFRelationship properties of the embedded file
fileSpecification.mime_type = "text/xml"
fileSpecification.af_relationship = ap.AFRelationship.ALTERNATIVE
# Add the embedded file to the PDF document's embedded files collection
document.embedded_files.add("factur",fileSpecification)
# Convert the PDF document to the PDF/A-3A format
path = _dataDir + "ZUGFeRD/log.xml"
document.convert(path, ap.PdfFormat.PDF_A_3A, ap.ConvertErrorAction.DELETE)
# Save the PDF document with the attached ZUGFeRD
path = _dataDir + "ZUGFeRD/ZUGFeRD-res.pdf"
document.save(path)