在 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

  1. 使用以下方式加载源 PDF 文档 类。
  2. 创建 Tiff设置Tiff设备 对象。
  3. 配置 TIFF 选项,例如压缩、色彩深度和空白页处理。
  4. 调用 处理 写入 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、EMF、JPG、PNG、GIF)

  1. 使用 … 加载 PDF 文件 文档 类。
  2. 创建所需的实例 图像设备 子类:
  3. 遍历您想要导出的页面。
  4. 调用 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)

使用 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

  1. 使用以下方式加载源 PDF 文档 类。
  2. 创建一个 SvgSaveOptions 对象并配置所需的选项。
  3. 调用 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)

相关转换