Managing Headers, Footers & Page Breaks in PDFs

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

分頁符號可能會造成困惑。

page-break-after 的 CSS 規則如果正確應用,將總是有效。 然而,如果您的樣式表將 divdisplay:block 更改,則此規則將無效。 確保 page-break-after 規則應用於塊級元素,最好是一個直接是 body 標籤子元素的 div

下面的 HTML 示例演示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Page Breaks with Header and Footer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        header, footer {
            position: fixed;
            width: 100%;
            text-align: center;
            background-color: #f1f1f1;
            padding: 10px 0;
        }
        header {
            top: 0;
        }
        footer {
            bottom: 0;
        }
        .content {
            margin: 150px 20px; /* Adjust margins to prevent content overlap with header and footer */
        }
        .page-break {
            page-break-after: always;
            display: block; /* Ensure it remains a block-level element */
        }
    </style>
</head>
<body>
    <header>
        <h1>Document Title</h1>
        <p>Header Content</p>
    </header>
    <footer>
        <p>Footer Content</p>
    </footer>
    <div class="content">
        <div class="page-break">
            <h2>Section 1</h2>
            <p>This is the first section. After this section, there will be a page break.</p>
        </div>
        <div class="page-break">
            <h2>Section 2</h2>
            <p>This is the second section. The header and footer continue to be displayed, and the content starts on a new page.</p>
        </div>
    </div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Page Breaks with Header and Footer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        header, footer {
            position: fixed;
            width: 100%;
            text-align: center;
            background-color: #f1f1f1;
            padding: 10px 0;
        }
        header {
            top: 0;
        }
        footer {
            bottom: 0;
        }
        .content {
            margin: 150px 20px; /* Adjust margins to prevent content overlap with header and footer */
        }
        .page-break {
            page-break-after: always;
            display: block; /* Ensure it remains a block-level element */
        }
    </style>
</head>
<body>
    <header>
        <h1>Document Title</h1>
        <p>Header Content</p>
    </header>
    <footer>
        <p>Footer Content</p>
    </footer>
    <div class="content">
        <div class="page-break">
            <h2>Section 1</h2>
            <p>This is the first section. After this section, there will be a page break.</p>
        </div>
        <div class="page-break">
            <h2>Section 2</h2>
            <p>This is the second section. The header and footer continue to be displayed, and the content starts on a new page.</p>
        </div>
    </div>
</body>
</html>
HTML

如果您需要每個 PDF 頁面上重複的表格標題和頁腳,請參閱本文:在 HTML PDF 中添加或避免分頁符號

如果上述步驟不能解決您的問題,請嘗試以下方法:

1. 簡化問題

如果您的內容長度不一,請測試不同長度的內容,以確保您使用正確的 HTML/CSS。 這個工具可以用來協助。

2. 驗證您的 HTML 和 CSS

CSS 驗證器可以用來檢查所用的 CSS 樣式。

HTML 驗證器可以協助您驗證 HTML 輸入。

此外,您可以請求您的網頁設計師來檢查您的 HTML,並追蹤變更。

另一方面,披露部分的位置受到多種因素的影響,包括表格結構、tfoot等。 因此仔細檢查您的 HTML 字符串和結構肯定會有所幫助。

3. 檢查 Chrome 的打印預覽中的輸出

IronPDF 使用 IronPDF 中所用的 Chrome PDF 渲染引擎,將輸出與桌面版 Chrome 網頁瀏覽器相同的 PDF。

因此,您可以通過 Chrome 打印預覽來檢查 HTML 的外觀,並相應地調整 HTML。 這可能需要反覆試驗,但這是調試 HTML 的最簡單方法。

了解更多有關如何通過 Chrome 打印預覽調試您的 HTML 的信息:像素完美 HTML 格式化

查看這篇文章以獲取有關 Chrome PDF 渲染引擎的更多信息:什麼是 IronPDF 的 Chrome 渲染器?

4. 使用 Chrome 開發者工具

另外,您可以使用 Chrome 開發者工具中的 Inspect 來尋找元素中計算出的最終 CSS。 這將提供字符串中每個 HTML 元素的更多細節。

5. 使用 IronPDF 渲染 HTML

將您的最終 HTML 字符串或 HTML 文件發送到 IronPDF Chrome 渲染器,使用 RenderHtmlAsPdf()RenderHtmlFileAsPdf() 方法來創建您的 PDF 文檔。

您可能需要為渲染器指定一些渲染選項,以微調輸出 PDF 和渲染操作。 參閱這篇文章了解更多信息。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布