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