IRONSOFTWAREHOME
新增 HTML 頁首與頁尾
from ironpdf import *
import os

# Instantiate Renderer
renderer = ChromePdfRenderer()
 
# Build a footer using html to style the text
# mergeable fields are:
# {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
renderer.RenderingOptions.HtmlFooter = HtmlHeaderFooter()
renderer.RenderingOptions.HtmlFooter.MaxHeight = 15  # millimeters
renderer.RenderingOptions.HtmlFooter.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
renderer.RenderingOptions.HtmlFooter.DrawDividerLine = True
 
# Use sufficient MarginBottom to ensure that the HtmlFooter does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginBottom = 25  # mm
 
# Build a header using an image asset
# Note the use of BaseUrl to set a relative path to the assets
renderer.RenderingOptions.HtmlHeader = HtmlHeaderFooter()
renderer.RenderingOptions.HtmlHeader.MaxHeight = 20  # millimeters
renderer.RenderingOptions.HtmlHeader.HtmlFragment = "<img src='iron.png'>"
renderer.RenderingOptions.HtmlHeader.BaseUrl = os.path.abspath("C:/Users/lyty1/OneDrive/Documents/IronPdfPythonNew")

# Use sufficient MarginTop to ensure that the HtmlHeader does not overlap with the main PDF page content.
renderer.RenderingOptions.MarginTop = 25  # mm
pip install ironpdf
新增 HTML 頁首與頁尾

新增 HTML 頁首與頁尾

IronPDF for Python支援完全樣式化的HTML標頭和頁尾,讓您可以在PDF的標頭和頁尾區域使用任何HTML和CSS的組合,包括圖像、自訂字型和動態合併欄位。

入門指南

建立RenderingOptions.HtmlFooter。 設置MarginTop / MarginBottom以防止與頁面正文重疊。

理解程式碼

  • HtmlHeaderFooter:一個接受HTML片段並將其渲染為每頁的標頭或頁尾的物件。
  • HtmlFragment:要顯示的HTML內容。 HTML中支援動態合併欄位,如{total-pages}。
  • MaxHeight:標頭或頁尾區域可以佔用的最大高度(毫米)。
  • True時,在標頭/頁尾和頁面內容之間畫一條橫線。
  • BaseUrl:用於解析HTML片段中的相對資源路徑(如圖片)的基礎路徑。 使用os.path.abspath來構建絕對路徑。
  • MarginTop / MarginBottom:頁邊距(毫米)。 確保這些至少和MaxHeight一樣大,以防止標頭或頁尾與正文內容重疊。

標頭和頁尾中支援的HTML

任何在瀏覽器中正確渲染的HTML都可以使用,包括<table>佈局、內嵌樣式和Google字型連結。

學習如何透過IronPDF for Python將頁首和頁尾新增到PDF中!

準備開始了嗎?

版本: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起的授權