在 Python 中将 PDF 转换为图像格式
Python 将 PDF 转换为图像
Aspose.PDF for Python via .NET 支持多种将 PDF 内容转换为图像的方法。在实际中,大多数工作流使用以下两种选项之一:
- 用于将 PDF 页面渲染为光栅图像格式的设备方法
- 用于将 PDF 内容导出为 SVG 的 SaveOptions 方法
本文展示如何将 PDF 文件转换为 TIFF、BMP、EMF、JPEG、PNG、GIF 和 SVG。
该库包含多个渲染类。 DocumentDevice 旨在进行全文件转换,而 ImageDevice 针对单个页面。
使用 DocumentDevice 类转换 PDF
使用 DocumentDevice 当您想将整个 PDF 渲染为单个多页 TIFF 文件时。
这 Tiff设备 类是基于 DocumentDevice 并提供 处理 将 PDF 文件中的所有页面转换为单个 TIFF 输出的方法。
将 PDF 页面转换为一个 TIFF 图像
Aspose.PDF for Python via .NET 可以将 PDF 文件中的每一页渲染为一个 TIFF 图像。
步骤:在 Python 中将 PDF 转换为 TIFF
以下代码片段展示了如何将所有 PDF 页面转换为单个 TIFF 图像。
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_TIFF(infile, outfile):
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
tiffSettings = ap.devices.TiffSettings()
tiffSettings.compression = ap.devices.CompressionType.LZW
tiffSettings.depth = ap.devices.ColorDepth.DEFAULT
tiffSettings.skip_blank_pages = False
tiffDevice = ap.devices.TiffDevice(resolution, tiffSettings)
tiffDevice.process(document, f"{outfile}.tiff")
print(infile + " converted into " + outfile)
使用 ImageDevice 类转换 PDF
使用 ImageDevice 当您需要以栅格图像格式逐页输出时。
ImageDevice 是…的基类 BmpDevice, JpegDevice, GifDevice, PngDevice,和 EmfDevice.
- 这 Bmp设备 类允许您将 PDF 页面转换为 BMP 图像。
- 这 Emf设备 类允许您将 PDF 页面转换为 EMF 图像。
- 这 Jpeg设备 类允许您将 PDF 页面转换为 JPEG 图像。
- 这 PngDevice 类允许您将 PDF 页面转换为 PNG 图像。
- 这 GifDevice 类允许您将 PDF 页面转换为 GIF 图像。
每种格式的工作流程相同:加载文档,创建相应的设备,然后处理所需的页面。
Bmp设备 公开了 处理 将特定页面渲染为 BMP 的方法。其他图像设备类遵循相同的模式,因此您可以通过更改设备类来切换格式。
以下链接和代码示例展示了如何将 PDF 页面渲染为常见的图像格式:
- 在 Python 中将 PDF 转换为 BMP
- 在 Python 中将 PDF 转换为 EMF
- 在 Python 中将 PDF 转换为 JPEG
- 在 Python 中将 PDF 转换为 PNG
- 在 Python 中将 PDF 转换为 GIF
步骤:在 Python 中将 PDF 转换为图像(BMP、EMF、JPG、PNG、GIF)
- 使用 … 加载 PDF 文件 文档 类。
- 创建所需的实例 图像设备 子类:
- 遍历您想要导出的页面。
- 调用 ImageDevice.process() 将每页保存为图像的方法。
将 PDF 转换为 BMP
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_BMP(infile, outfile):
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
device = ap.devices.BmpDevice(resolution)
page_count = 1
while page_count <= len(document.pages):
image_stream = FileIO(outfile + str(page_count) + "_out.bmp", "w")
device.process(document.pages[page_count], image_stream)
image_stream.close()
page_count = page_count + 1
print(infile + " converted into " + outfile)
将 PDF 转换为 EMF
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_EMF(infile, outfile):
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
device = ap.devices.EmfDevice(resolution)
page_count = 1
while page_count <= len(document.pages):
image_stream = FileIO(outfile + str(page_count) + "_out.emf", "w")
device.process(document.pages[page_count], image_stream)
image_stream.close()
page_count = page_count + 1
print(infile + " converted into " + outfile)
将 PDF 转换为 JPEG
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_JPEG(infile, outfile):
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
device = ap.devices.JpegDevice(resolution)
page_count = 1
while page_count <= len(document.pages):
image_stream = FileIO(outfile + str(page_count) + "_out.jpeg", "w")
device.process(document.pages[page_count], image_stream)
image_stream.close()
page_count = page_count + 1
print(infile + " converted into " + outfile)
将 PDF 转换为 PNG
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_PNG(infile, outfile):
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
device = ap.devices.PngDevice(resolution)
page_count = 1
while page_count <= len(document.pages):
image_stream = FileIO(outfile + str(page_count) + "_out.png", "w")
device.process(document.pages[page_count], image_stream)
image_stream.close()
page_count = page_count + 1
将 PDF 转换为 PNG,使用默认字体
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_PNG_with_default_font(infile, outfile):
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
rendering_options = ap.RenderingOptions()
rendering_options.default_font_name = "Arial"
device = ap.devices.PngDevice(resolution)
device.rendering_options = rendering_options
page_count = 1
while page_count <= len(document.pages):
image_stream = FileIO(outfile + str(page_count) + "_out.png", "w")
device.process(document.pages[page_count], image_stream)
image_stream.close()
page_count = page_count + 1
将 PDF 转换为 GIF
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_GIF(infile, outfile):
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
device = ap.devices.GifDevice(resolution)
page_count = 1
while page_count <= len(document.pages):
image_stream = FileIO(outfile + str(page_count) + "_out.gif", "w")
device.process(document.pages[page_count], image_stream)
image_stream.close()
page_count = page_count + 1
print(infile + " converted into " + outfile)
尝试在线将 PDF 转换为 PNG
作为我们的应用程序工作方式的示例,请查看下一个功能。
Aspose.PDF for Python 为您呈现在线应用 “PDF 转 PNG”,在此您可以尝试调查其功能以及其工作质量。
使用 SaveOptions 类转换 PDF
使用 SaveOptions 当您想要将 PDF 内容导出为 SVG 时。
可缩放矢量图形 (SVG) 是一种基于 XML 的二维矢量图形格式。由于 SVG 保持矢量特性,当您需要为网页、UI 或设计工作流提供可缩放的输出时,它非常有用。
SVG 文件是基于文本的、可搜索的,并且易于在其他工具中进行后处理。
Aspose.PDF for Python via .NET 可以将 SVG 转换为 PDF,也可以将 PDF 页面导出为 SVG。对于 PDF 转 SVG 转换,创建一个 SvgSaveOptions 对象并将其传递给 document.save() 方法。
以下步骤展示如何使用 Python 将 PDF 文件转换为 SVG。
步骤:在 Python 中将 PDF 转换为 SVG
- 使用以下方式加载源 PDF 文档 类。
- 创建一个 SvgSaveOptions 对象并配置所需的选项。
- 调用 document.save() 带有 the 的方法
SvgSaveOptions用于写入 SVG 输出的实例。
将 PDF 转换为 SVG
import aspose.pdf as ap
from io import FileIO
from os import path
import sys
def convert_PDF_to_SVG(infile, outfile):
document = ap.Document(infile)
save_options = ap.SvgSaveOptions()
save_options.compress_output_to_zip_archive = False
save_options.treat_target_file_name_as_directory = True
document.save(f"{outfile}.svg", save_options)
相关转换
- 将图像格式转换为 PDF 当您需要从 JPG、PNG、TIFF、SVG 或其他图像来源重建 PDF 时。
- 将 PDF 转换为 HTML 用于生成对浏览器友好的输出,而不是光栅图像。
- 将 PDF 转换为其他格式 用于 EPUB、Markdown、文本和 XPS 导出工作流。


