IRONSOFTWAREHOME
設定自訂邊距
from ironpdf import *
 
renderer = ChromePdfRenderer()

# Set Margins (in millimeters)
renderer.RenderingOptions.MarginTop = 40
renderer.RenderingOptions.MarginLeft = 20
renderer.RenderingOptions.MarginRight = 20
renderer.RenderingOptions.MarginBottom = 40

renderer.RenderHtmlFileAsPdf("my-content.html").SaveAs("my-content.pdf")
pip install ironpdf
設定自訂邊距

設定自訂邊距

IronPDF for Python 可讓您使用 RenderingOptions 的 MarginTop、MarginLeft、MarginRight 和 MarginBottom 屬性,在生成的 PDF 上設定精確的頁面邊距。

入門指南

建立一個ChromePdfRenderer,將每個邊距屬性設為所需的毫米值,然後呼叫渲染方法。 邊距會套用到生成的PDF中的每一頁。

理解程式碼

  • MarginTop:頁面的頂部邊緣與內容區之間的毫米距離。 也用於保留HTML標頭的空間。
  • MarginLeft:頁面的左邊緣與內容區之間的距離。
  • MarginRight:頁面的右邊緣與內容區之間的距離。
  • MarginBottom:頁面的底部邊緣與內容區之間的距離。 也用於保留HTML頁尾的空間。
  • RenderHtmlFileAsPdf("my-content.html"):使用配置好的邊距渲染本地HTML文件。

常用的邊距值

使用案例建議的邊距
標準文件各邊20毫米
含標頭/標尾上下40毫米,左右20毫米
狹窄裝訂左側30毫米,右側15毫米
寬邊列印各邊10毫米

邊距以毫米為單位設置。 要使用英寸,請乘以25.4。

準備開始了嗎?

版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
PDF的Python模塊下載
使用pip安裝

版本: 2026.9

  1. 下載並安裝Python 3.7+。
  2. 如果尚未安裝,請從 pypi.org 安裝 pip。
  3. 在終端執行上述命令。
Python PDF模塊
下載模塊

版本: 2026.9

手動安裝到您的專案中

  1. 下載套件
  2. 從終端運行此命令
    pip install ironpdf-2026.9-py37-none-win_amd64.whi

從$999起的授權