Crear PDF compatible con PDF/3-A y adjuntar factura ZUGFeRD en Python

Adjuntar ZUGFeRD a PDF

Recomendamos los siguientes pasos para adjuntar ZUGFeRD al PDF:

  1. Importe la biblioteca Aspose.PDF y asígnele el alias ap para mayor comodidad.
  2. Defina la ruta al directorio donde se encuentran los archivos PDF de entrada y salida.
  3. Defina la ruta al archivo PDF que será procesado.
  4. Cargue el archivo PDF desde la variable de ruta y cree un objeto Document.
  5. 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.
  6. Establecer el mime_type y el af_relationship propiedades del objeto FileSpecification a text/xml y ALTERNATIVE, respectivamente.
  7. 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.
  8. Convertir el documento PDF al formato PDF/A-3A. Utilice la ruta al archivo de registro, el PdfFormat.PDF_A_3A enumeración, y el ConvertErrorAction.DELETE enumeración para convertir el objeto del documento.
  9. 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)