自訂 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或Letter尺寸,並以英吋指定確切的寬度和高度。

入門指南

設置 RenderingOptions.PaperSize = PdfPaperSize.Custom,然後在渲染前以您想要的尺寸調用 SetCustomPaperSizeInInches(width, height)。

理解程式碼

  • PaperSize = PdfPaperSize.Custom:切換渲染器,不使用標準紙張尺寸預設,並啟用由 SetCustomPaperSizeInInches 定義的自定義尺寸。
  • SetCustomPaperSizeInInches(width, height):以英吋設置確切的頁面尺寸。 在此範例中,寬度和高度均設為5.0英吋,製作出一個正方形頁面。
  • RenderHtmlAsPdf(html):在自定義紙張尺寸下呈現HTML內容。
  • SaveAs("CustomPaperSize.pdf"):儲存輸出文件。

標準紙張尺寸預設

如果不需要自定義尺寸,IronPDF還支持通過PdfPaperSize枚舉的標準預設,包括:

Tabloid,及其他許多。

在不調用RenderingOptions.PaperSize。

學習使用IronPDF for Python自訂PDF輸出!

準備開始了嗎?

版本:2026.11剛剛發布