如何在Python中将PDF转换为图像 | IronPDF

Python PDF 到图像的转换

This article was translated from English: Does it need improvement?
Translated
View the article in English

PDF到图像转换是Python应用程序中的常见需求。 开发者使用它来生成文档缩略图、Web预览,并将PDF内容输入图像处理管道。 IronPDF for Python 提供了 RasterizeToImageFiles 方法,该方法可将任何 PDF 文档(或 URL)转换为 JPEG、PNG 或 TIFF 图像文件,并支持配置 DPI 和尺寸。

本指南展示了如何在Python中将PDF文件转换为图像、如何控制输出质量以及如何调整生成的图像以满足布局约束。

快速入门:将PDF转换为图像

  1. 安装 IronPDF:pip install ironpdf
  2. 加载 PDF 文档:pdf = PdfDocument.FromFile("document.pdf")
  3. 将每页转换为 PNG 格式:pdf.RasterizeToImageFiles("output/*.png", DPI=96)
#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/quickstart.py
from ironpdf import PdfDocument

pdf = PdfDocument.FromFile("document.pdf")
pdf.RasterizeToImageFiles("output/*.png", DPI=96)
#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/quickstart.py
from ironpdf import PdfDocument

pdf = PdfDocument.FromFile("document.pdf")
pdf.RasterizeToImageFiles("output/*.png", DPI=96)
PYTHON

为什么开发者将PDF转换为图像?

PDF到图像的转换解决了软件开发中的若干实际问题。 最常见的需求是预览生成——在Web界面或移动应用程序中显示PDF文档的缩略图,而无需用户下载并打开完整文件。

其他常见用例包括:

  • 机器学习管道:OCR模型、布局分析工具和计算机视觉系统接受图像输入,而非PDF
  • 档案和合规性:在原始PDF旁边存储视觉快照,以确保长时间可读,即使PDF查看器发生变化
  • 报告嵌入:将特定PDF页面作为图像包含在其他文档或电子邮件模板中
  • 自动化测试:通过比较渲染页面快照,检测PDF输出的视觉回归

Python的标准库不包括原生的PDF到图像功能。 像 pdf2image 这样的工具需要安装 Poppler 作为外部二进制文件,这增加了部署的复杂性。 IronPDF 内部处理渲染,因此要在任何 Python 项目中添加 PDF 转图像功能,只需添加一行 pip install 代码即可。

请注意IronPDF使用基于Chromium的渲染引擎。HTML渲染的PDF生成看起来与浏览器显示完全相同的图像,这在PDF源自HTML或URL时很有用。

如何将PDF文件转换为图像?

要将 PDF 转换为图像,请在 PdfDocument 实例上调用 RasterizeToImageFiles。 该方法将每页生成一个图像文件写入指定目录。 输出路径中的星号 (*) 将被替换为从 1 开始的页码,因此 assets/images/*.png 将生成 assets/images/2.png,依此类推。

在调用该方法之前创建输出目录。 如果不存在,IronPDF不会自动创建。

#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/convert-pdf-to-images.py
from ironpdf import PdfDocument

# Load a PDF from disk
pdf = PdfDocument.FromFile("my-content.pdf")

# Write each page to a PNG file in the "assets/images" folder
# The folder must exist before calling this method
pdf.RasterizeToImageFiles("assets/images/*.png", DPI=96)
#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/convert-pdf-to-images.py
from ironpdf import PdfDocument

# Load a PDF from disk
pdf = PdfDocument.FromFile("my-content.pdf")

# Write each page to a PNG file in the "assets/images" folder
# The folder must exist before calling this method
pdf.RasterizeToImageFiles("assets/images/*.png", DPI=96)
PYTHON

DPI 参数用于控制图像分辨率。 更高的DPI产生更清晰的图像,但代价是文件大小更大和处理时间更长。标准值包括:

DPI值及其典型用例
DPI典型用途
72屏幕分辨率,快速处理
96标准Web质量
150良好的打印质量
300高质量的打印输出
File explorer showing numbered PNG output files (1.png through 11.png) generated by RasterizeToImageFiles from an 11-page PDF

每个PDF页面生成一个PNG文件的输出目录

如果输出图像出现模糊,请增加 DPI 的数值。 在一个50页的文档上处理300 DPI明显比96 DPI要耗时更久——请根据用例的实际质量需求做出选择。

提示对于Web缩略图生成,96 DPI就足够了,并保持文件尺寸小。 为将要打印或以全尺寸显示的情况预留150-300 DPI。

如何将特定页面范围转换为图像?

若仅需转换部分页面,请将页面索引作为第二个参数传递给 RasterizeToImageFiles。 页面索引是从0开始的整数。

#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/convert-page-range.py
from ironpdf import PdfDocument

pdf = PdfDocument.FromFile("my-content.pdf")

# Convert only pages 1, 2, and 3 (zero-based indices: 0, 1, 2)
pdf.RasterizeToImageFiles("assets/images/*.png", [0, 1, 2], DPI=96)
#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/convert-page-range.py
from ironpdf import PdfDocument

pdf = PdfDocument.FromFile("my-content.pdf")

# Convert only pages 1, 2, and 3 (zero-based indices: 0, 1, 2)
pdf.RasterizeToImageFiles("assets/images/*.png", [0, 1, 2], DPI=96)
PYTHON

选择性页面提取对仅需预览前几页的文档很有用,避免不必要地处理整个文件。对于提取PDF中已嵌入的图像的工作流,IronPDF提供了单独的提取方法。

如何将URL转换为图像?

IronPDF可以将网页渲染为PDF,然后在单一工作流中将该PDF转换为图像。 使用 ChromePdfRenderer 获取并渲染 URL,然后对生成的 PdfDocument 调用 RasterizeToImageFiles。 这种方法可以创建网页或生成基于Web内容的预览图像的视觉档案,而无需单独的截屏工具。

#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/url-to-images.py
from ironpdf import ChromePdfRenderer

# Render a web page to PDF
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://www.example.com")

# Convert each rendered page to a PNG image
pdf.RasterizeToImageFiles("assets/images/*.png", DPI=96)
#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/url-to-images.py
from ironpdf import ChromePdfRenderer

# Render a web page to PDF
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://www.example.com")

# Convert each rendered page to a PNG image
pdf.RasterizeToImageFiles("assets/images/*.png", DPI=96)
PYTHON

渲染的图像反映了页面在Chromium浏览器中的显示效果,包括CSS样式和JavaScript渲染的内容。 对于需要身份验证或交互的页面,考虑使用IronPDF的HTML到PDF教程来直接提供预渲染标记。 然后可使用上面显示的相同方法栅格化生成的PDF。

File explorer showing 5 PNG files (1.png through 5.png) generated from a URL rendered to PDF by IronPDF

从通过IronPDF渲染的URL生成的图像

如何控制输出图像尺寸?

默认情况下,RasterizeToImageFiles 会根据 DPI 系数按原生页面尺寸生成图像。 为限制输出尺寸,请设置 ImageMaxWidthImageMaxHeight,两者均以像素为单位。

IronPDF 会根据 ImageMaxHeight 的值保留原始宽高比。 即使指定的宽度和高度与源页面的比例不同,输出也不会被拉伸或变形。

#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/constrained-dimensions.py
from ironpdf import PdfDocument

pdf = PdfDocument.FromFile("my-content.pdf")

# Generate thumbnail images constrained to 500x500 pixels
pdf.RasterizeToImageFiles(
    "assets/images/*.png",
    ImageMaxWidth=500,
    ImageMaxHeight=500,
    DPI=200
)
#:path=/static-assets/pdf/content-code-examples/how-to/python-pdf-to-image/constrained-dimensions.py
from ironpdf import PdfDocument

pdf = PdfDocument.FromFile("my-content.pdf")

# Generate thumbnail images constrained to 500x500 pixels
pdf.RasterizeToImageFiles(
    "assets/images/*.png",
    ImageMaxWidth=500,
    ImageMaxHeight=500,
    DPI=200
)
PYTHON

ImageMaxWidth=500, ImageMaxHeight=500 设置为 200 DPI 可生成适用于缩略图显示或卡片式 UI 布局的图像。 调整这些值以匹配应用程序设计所需的尺寸。 为了生成即将被栅格化的PDF内容,IronPDF还支持从Python创建PDF合并多个PDF后再进行转换。

重要基于高度保留纵横比。 如果源页面的宽度大于高度,生成的图像可能比 ImageMaxWidth 更窄。 设计布局以处理可变宽度的缩略图。)}]

PDF到图像转换的下一步是什么?

本指南涵盖了三个核心工作流:将PDF文件转换为图像,将URL转换为图像,控制图像尺寸和质量。 IronPDF 支持 JPEG、JPG、PNG、TIFF 以及通过相同 RasterizeToImageFiles 方法处理的其他格式。 更改输出路径模式中的文件扩展名以切换格式。

欲实现进一步自动化,请考虑这些后续步骤:

  • 批量处理:遍历 PDF 文件所在的目录并依次转换每个文件,或使用 Python 的 concurrent.futures 模块进行并行处理
  • 选择性提取:使用页面索引参数仅提取相关页面
  • 图像管道集成:将输出文件输入PIL/PillowOpenCV进行进一步处理,如裁剪、注释或格式标准化
  • 转换前压缩:使用IronPDF的PDF压缩在栅格化大文档之前减小文件大小
  • 表单工作流编程填写PDF表单,然后将填好的表单页面转换为图像以供审核或存档

要继续使用IronPDF构建,请探索IronPDF for Python文档栅格化PDF至图像示例

开始免费使用IronPDF for Python的30天试用以在生产中使用这些功能。 准备部署时,查看许可选项以获取适合项目规模的方案。

请注意延伸阅读:将 PDF 转换为图像

常见问题解答

我如何在Python中将PDF转换为图像?

使用pip install ironpdf安装IronPDF,然后使用PdfDocument.FromFile("file.pdf")加载PDF,并调用RasterizeToImageFiles("output/*.png", DPI=96)。这将每页写入一个图像到指定目录。

IronPDF支持的PDF转换图像格式有哪些?

IronPDF支持PNG、JPEG、JPG和TIFF输出格式。通过改变传递给RasterizeToImageFiles的输出路径模式中的文件扩展名来指定格式,如*.jpg用于JPEG或*.tiff用于TIFF。

我如何在转换PDF时控制图像质量?

RasterizeToImageFiles中使用DPI参数。96是标准的网络质量。150适合打印预览。300可生成高品质打印输出。更高的DPI值增加文件大小和处理时间。

我可以为输出图像设置最大宽度或高度吗?

可以。在RasterizeToImageFiles中传递ImageMaxWidthImageMaxHeight,单位为像素。IronPDF根据高度值保留纵横比,因此图像不会变形。

我如何仅将PDF中的特定页面转换为图像?

将零基页面索引的列表作为第二个参数传递给RasterizeToImageFiles。例如,[0, 1, 2]只转换前三页。

我可以使用IronPDF将网页URL转换为图像吗?

可以。使用ChromePdfRenderer调用RenderUrlAsPdf与URL,返回一个PdfDocument。然后在该文档上调用RasterizeToImageFiles以从每个渲染的页面生成图像文件。

IronPDF的PDF到图像转换是否需要任何外部二进制文件?

不需要。IronPDF包含自己的基于Chromium的渲染引擎,不需要Poppler、Ghostscript或任何外部二进制文件安装。一个pip install ironpdf就足够了。

为什么我转换的图像看起来模糊?

图像模糊是由低DPI值引起的。将它从默认的96增加到150或300以获得更清晰的结果。请注意,较高的DPI会产生更大的文件大小并需要更长的处理时间。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
版本: 2026.5 just released
Still Scrolling Icon

还在滚动吗?

想快速获得证据?
运行示例看着你的HTML代码变成PDF文件。