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