Crear PDF compatible con PDF/3-A y adjuntar factura ZUGFeRD en Python
Contents
[
Hide
]
Adjuntar ZUGFeRD a PDF
Recomendamos los siguientes pasos para adjuntar ZUGFeRD al PDF:
- Importe la biblioteca Aspose.PDF y asígnele el alias ap para mayor comodidad.
- Defina la ruta al directorio donde se encuentran los archivos PDF de entrada y salida.
- Defina la ruta al archivo PDF que será procesado.
- Cargue el archivo PDF desde la variable de ruta y cree un objeto Document.
- Cree un objeto FileSpecification para el archivo XML que contiene los metadatos de la factura. Use la variable de ruta y una cadena de descripción para crear el objeto FileSpecification.
- Establecer el
mime_typey elaf_relationshippropiedades del objeto FileSpecification atext/xmlyALTERNATIVE, respectivamente. - Añada el objeto fileSpecification a la colección de archivos incrustados del objeto document. Esto adjunta el archivo XML al documento PDF como un archivo de metadatos de factura.
- Convertir el documento PDF al formato PDF/A-3A. Utilice la ruta al archivo de registro, el
PdfFormat.PDF_A_3Aenumeración, y elConvertErrorAction.DELETEenumeración para convertir el objeto del documento. - Guarde el documento PDF con el ZUGFeRD adjunto.
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)