Создание PDF/3-A совместимого PDF и прикрепление счета ZUGFeRD в Python

Прикрепить ZUGFeRD к PDF

Мы рекомендуем выполнить следующие шаги, чтобы прикрепить ZUGFeRD к PDF:

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