إنشاء ملف PDF متوافق مع PDF/3-A وإرفاق فاتورة ZugFerd بلغة Python

قم بإرفاق ZugFerd إلى ملف PDF

نوصي باتباع الخطوات التالية لإرفاق ZugFerd بـ PDF:

  1. قم باستيراد مكتبة Aspose.PDF وأعطها اسمًا مستعارًا للتطبيق من أجل الراحة.
  2. حدد المسار إلى الدليل حيث توجد ملفات PDF المدخلة والمخرجة.
  3. حدد المسار إلى ملف PDF الذي ستتم معالجته.
  4. قم بتحميل ملف PDF من متغير المسار وإنشاء كائن مستند.
  5. قم بإنشاء كائن FileSpecification لملف XML الذي يحتوي على بيانات تعريف الفاتورة. استخدم متغير المسار وسلسلة الوصف لإنشاء كائن FileSpecification.
  6. قم بتعيين mime_type و ال af_relationship خصائص كائن تحديد الملف إلى text/xml و ALTERNATIVE، على التوالي.
  7. أضف كائن FileSpecification إلى مجموعة الملفات المضمنة لكائن المستند. يؤدي هذا إلى إرفاق ملف XML بمستند PDF كملف بيانات تعريف الفاتورة.
  8. قم بتحويل وثيقة PDF إلى صيغة PDF/A-3A. استخدم المسار لتسجيل الملف، PdfFormat.PDF_A_3A التعداد، و ConvertErrorAction.DELETE التعداد لتحويل كائن المستند.
  9. احفظ مستند PDF مع ZugFerd المرفق.
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)