自定义PDF大小
from ironpdf import *
 
html = "<p>Lorem ipsum dolor sit amet..."

renderer = ChromePdfRenderer()
renderer.RenderingOptions.PaperSize = PdfPaperSize.Custom
renderer.RenderingOptions.SetCustomPaperSizeInInches(5.0, 5.0)

pdf = renderer.RenderHtmlAsPdf(html)

pdf.SaveAs("CustomPaperSize.pdf")
pip install ironpdf
自定义PDF大小

自定义PDF大小

IronPDF for Python允许您以任何自定义纸张大小渲染PDF,覆盖以英寸指定的标准A4或信纸尺寸。

开始

设置SetCustomPaperSizeInInches(width, height)。

理解代码

  • SetCustomPaperSizeInInches定义的自定义尺寸。
  • SetCustomPaperSizeInInches(width, height):以英寸设置精确页面尺寸。 在此示例中,宽度和高度均设置为5.0英寸,生成正方形页面。
  • RenderHtmlAsPdf(html):在自定义纸张尺寸上渲染HTML内容。
  • SaveAs("CustomPaperSize.pdf"):保存输出文件。

标准纸张尺寸预设

如果不需要自定义尺寸,IronPDF还通过PdfPaperSize枚举支持标准预设,包括:

Tabloid,及其他更多。

将所需的预设分配给SetCustomPaperSizeInInches。

学习如何使用IronPDF for Python自定义PDF输出!

准备开始了吗?

版本:2026.11刚刚发布