Membuat PDF yang mematuhi PDF/3-A dan melampirkan faktur ZUGFeRD dengan Python
Contents
[
Hide
]
Lampirkan ZUGFeRD ke PDF
Kami merekomendasikan langkah-langkah berikut untuk melampirkan ZUGFeRD ke PDF:
- Impor pustaka Aspose.PDF dan beri alias ap untuk kemudahan.
- Tentukan path ke direktori tempat file PDF input dan output berada.
- Tentukan path ke file PDF yang akan diproses.
- Muat file PDF dari variabel path dan buat objek Document.
- Buat objek FileSpecification untuk file XML yang berisi metadata faktur. Gunakan variabel path dan string deskripsi untuk membuat objek FileSpecification.
- Atur
mime_typedanaf_relationshipproperti dari objek FileSpecification ketext/xmldanALTERNATIVE, masing-masing. - Tambahkan objek fileSpecification ke koleksi file tertanam dari objek dokumen. Ini melampirkan file XML ke dokumen PDF sebagai file metadata faktur.
- Konversi dokumen PDF ke format PDF/A-3A. Gunakan jalur ke file log, the
PdfFormat.PDF_A_3Aenumerasi, danConvertErrorAction.DELETEenumerasi untuk mengubah objek dokumen. - 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)