Membuat PDF yang mematuhi PDF/3-A dan melampirkan faktur ZUGFeRD dengan Python

Lampirkan ZUGFeRD ke PDF

Kami merekomendasikan langkah-langkah berikut untuk melampirkan ZUGFeRD ke PDF:

  1. Impor pustaka Aspose.PDF dan beri alias ap untuk kemudahan.
  2. Tentukan path ke direktori tempat file PDF input dan output berada.
  3. Tentukan path ke file PDF yang akan diproses.
  4. Muat file PDF dari variabel path dan buat objek Document.
  5. Buat objek FileSpecification untuk file XML yang berisi metadata faktur. Gunakan variabel path dan string deskripsi untuk membuat objek FileSpecification.
  6. Atur mime_type dan af_relationship properti dari objek FileSpecification ke text/xml dan ALTERNATIVE, masing-masing.
  7. Tambahkan objek fileSpecification ke koleksi file tertanam dari objek dokumen. Ini melampirkan file XML ke dokumen PDF sebagai file metadata faktur.
  8. Konversi dokumen PDF ke format PDF/A-3A. Gunakan jalur ke file log, the PdfFormat.PDF_A_3A enumerasi, dan ConvertErrorAction.DELETE enumerasi untuk mengubah objek dokumen.
  9. Simpan dokumen PDF dengan ZUGFeRD yang terlampir.
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)