Python を使って PostScript ファイルを PDF に結合
Aspose.Page PS Merger の品質を確認し、結果を確認するには、無料のオンラインツール PostScipt Merger をご利用ください。
Python を使用して複数の PS ファイルを 1 つの PDF に結合するには、次の簡単な手順に従います。
- 最初の入力 PS ファイルの入力ストリームを初期化します。
- 出力 PDF ドキュメントの出力ストリームを初期化します。
- 最初のファイルと結合する PS ファイルの配列を作成します。
- 作成した入力ストリームから PsDocument のインスタンスを作成します。
- PdfSaveOptions を使用して、AdditionalFontsFolder と SuppressError のブール値を指定します。
- 先ほど作成した出力ストリームから PdfDevice のインスタンスを作成します。
- 作成したドキュメントと PostScript ファイルを結合し、PDF 保存オプションを使用して PDF として保存します。
次のコード スニペットは、Python で PS ファイルを PDF ドキュメントに結合する方法を示しています。
1# For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Python
2
3# The path to the documents directory.
4data_dir = Util.get_data_dir_working_with_document_merging()
5# Initialize PDF output stream
6pdf_stream = open(data_dir + "outputPDF_out.pdf", "wb")
7# Initialize the first PostScript file input stream
8ps_stream = open(data_dir + "input.ps", "rb",)
9document = PsDocument(ps_stream)
10
11# Create an array of PostScript files that will be merged with the first one
12files_for_merge = [ data_dir + "input2.ps", data_dir + "input3.ps" ]
13
14# If you want to convert Postscript file despite of minor errors set this flag
15suppress_errors = True
16
17#Initialize options object with necessary parameters.
18options = PdfSaveOptions(suppress_errors)
19# If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
20options.additional_fonts_folders = [ """{FONT_FOLDER}""" ]
21
22# Default page size is 595x842 and it is not mandatory to set it in PdfDevice
23device = PdfDevice(pdf_stream)
24# But if you need to specify size and image format use following line
25#Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream, new aspose.pydrawing.Size(595, 842));
26
27try:
28 document.merge(files_for_merge, device, options)
29finally:
30 ps_stream.close()
31 pdf_stream.close()
PdfSaveOptionsについて考えてみましょう。このクラスを使用すると、PSファイルをPDFに結合する際に、異なる変換パラメータを指定できます。
- AdditionalFontsFolder は、コンバーターが追加フォントを見つける場所を指定します。デフォルトでは、システムフォントフォルダーが常に含まれます。
- SuppressError は、重大ではないエラーが発生した場合の EPS から PDF へのマージ処理の動作を制御します。true に設定すると、マージ処理が続行され、マージ後に例外フィールドにエラーの一覧が表示されます。デフォルト値は true です。
- Debug は、デバッグ情報をコンソールに出力します。デフォルトでは false に設定されています。