Python で PDF/3-A に準拠した PDF を作成し、ZugFerd 請求書を添付する

PDF にツークファードを添付

ZugFerdをPDFに添付するには、以下の手順をお勧めします。

  1. Aspose.PDF ライブラリをインポートし、わかりやすいように ap というエイリアスを付けます。
  2. 入出力 PDF ファイルが保存されているディレクトリへのパスを定義します。
  3. 処理する PDF ファイルへのパスを定義します。
  4. パス変数から PDF ファイルを読み込み、Document オブジェクトを作成します。
  5. 請求書メタデータを含む XML ファイルの FileSpecification オブジェクトを作成します。パス変数と説明文字列を使用して FileSpecification オブジェクトを作成します。
  6. を設定 mime_typeaf_relationship FileSpecification オブジェクトのプロパティを text/xml そして ALTERNATIVEそれぞれ。
  7. FileSpecification オブジェクトをドキュメントオブジェクトの埋め込みファイルコレクションに追加します。これにより、XML ファイルが PDF ドキュメントに請求書メタデータファイルとして添付されます。
  8. PDF ドキュメントを PDF/A-3A フォーマットに変換します。ログファイルへのパスを使用して、 PdfFormat.PDF_A_3A 列挙、および ConvertErrorAction.DELETE 文書オブジェクトを変換するための列挙。
  9. ZugFerd が添付された PDF ドキュメントを保存します。
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)