Создание PDF/3-A совместимого PDF и прикрепление счета ZUGFeRD в Python
Contents
[
Hide
]
Прикрепить ZUGFeRD к PDF
Мы рекомендуем выполнить следующие шаги, чтобы прикрепить ZUGFeRD к PDF:
- Импортируйте библиотеку Aspose.PDF и присвойте ей псевдоним ap для удобства.
- Определите путь к каталогу, в котором находятся входные и выходные файлы PDF.
- Определите путь к файлу PDF, который будет обрабатываться.
- Загрузите файл PDF из переменной path и создайте объект Document.
- Создайте объект FileSpecification для XML‑файла, содержащего метаданные счета. Используйте переменную path и строку‑описание для создания объекта FileSpecification.
- Установите
mime_typeиaf_relationshipсвойства объекта FileSpecification кtext/xmlиALTERNATIVE, соответственно. - Добавьте объект fileSpecification в коллекцию встроенных файлов объекта document. Это присоединит XML‑файл к PDF‑документу в качестве файла метаданных счёта.
- Преобразуйте PDF‑документ в формат PDF/A-3A. Используйте путь к файлу журнала, the
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)