使用 Python 将 PostScript 文件合并为 PDF
您可以通过免费在线工具 PostScipt Merger
按照以下步骤,使用 Python 将多个 PS 文件合并为一个 PDF:
- 为第一个输入 PS 文件初始化输入流。
- 为输出 PDF 文档初始化输出流。
- 创建一个将与第一个 PS 文件合并的 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,则允许合并过程继续,合并后可以在“Exceptions”字段中查看此类错误列表。默认值为 true。
- Debug 允许将调试信息输出到控制台。默认情况下,设置为 false。