主要内容

本页采用了机器翻译。点击此处可查看英文原文。

PDF 模板中的页码

此示例显示如何使用 PDF 模板对报告的页面进行编号。它通过使用两个不同的页脚来定义奇数页的右对齐页码和偶数页的左对齐页码。下图显示了使用示例模板创建的文档中的两个页面:

Two blank pages with text. The left page has "Hello world" at the top and "Page 1" at the bottom. The right page has "Hello again" at the top and "Page 2" at the bottom.

创建模板

在当前工作目录中创建一个 PDF 模板 myPDFTemplate.pdftx。解压模板进行编辑。exampleTemplate.pdftx 中提供了已包含示例页脚的参考模板。

mlreportgen.dom.Document.createTemplate("myPDFTemplate","pdf");
unzipTemplate("myPDFTemplate.pdftx", "myPDFTemplate_pdftx");
%unzipTemplate("exampleTemplate.pdftxt", "exampleTemplate_pdftx");

定义页脚内容

myPDFTemplate_pdftx\docpart_templates.html 中,定义包含页脚内容的模板部件。在 <dplibrary> 标签中,为奇数页创建名为 MyPageFooter<dptemplate> 元素,为偶数页创建名为 MyEvenFooter 的同类元素。创建包含每个页脚中所含文本的段落以及应放置页码的 page 元素。将 text-align 样式设置为 rightMyPageFooter 以及 leftMyEvenFooter。例如:

<dplibrary>
    <dptemplate name="rgChapter">
        <h1 class="rgChapterTitle">
        <hole id="rgChapterTitlePrefix" default-style-name="rgChapterTitlePrefix" /><span> </span>  
        <hole id="rgChapterTitleNumber" default-style-name="rgChapterTitleNumber" /><span>. </span>
        <hole id="rgChapterTitleText" default-style-name="rgChapterTitleText" />
        </h1>
        <hole id="rgChapterContent"/>
    </dptemplate>
    <dptemplate name="ReportTOC"><TOC number-of-levels ="3" leader-pattern="dots" /></dptemplate>
    
    <!-- Document part templates defining the footers -->
    <dptemplate name="MyPageFooter">
        <p style="text-align:right;font-size:10pt;white-space:preserve">Page <page/></p>
    </dptemplate>
    
    <dptemplate name="MyEvenFooter">
        <p style="text-align:left;font-size:10pt;white-space:preserve">Page <page/></p>
    </dptemplate>    
</dplibrary>

创建页脚元素

myPDFTemplate_pdftx\root.html 的正文部分,取消注释 <layout> 元素并添加两个 <pfooter> 元素。按照下面的示例 HTML 代码所示设置 typetemplate-name 属性。default 类型的页脚用于第一页和奇数页。even 类型的页脚用于偶数页。template-name 属性设置为先前定义的模板部件的名称。要指定起始页码,请添加 <pnumber> 元素。

<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Report Template</title>
    <link rel="StyleSheet" href="./stylesheets/root.css" type="text/css" />
</head>

<body>
    
<!-- Uncomment and edit this layout to customize a document or document part layout based on this template. -->
<layout style="page-margin: 1in 1in 1in 1in 0.5in 0.5in 0in; page-size: 8.5in 11in portrait">
    <pfooter type="default" template-name="MyPageFooter"/>
    <pfooter type="even" template-name="MyEvenFooter"/>
    <pnumber format="1" />
</layout>

</body>
</html>

压缩模板

将模板文件压缩回 myPDFTemplate.pdftx 模板包。

zipTemplate('myPDFTemplate.pdftx', 'myPDFTemplate_pdftx');

使用模板

在创建文档时指定模板名称来使用模板。下面的代码使用参考模板 exampleTemplate.pdftx 来创建文档。要使用示例修改的模板,请将 exampleTemplate 替换为 myPDFTemplate

import mlreportgen.dom.*

d = Document("myDocument", "pdf", "exampleTemplate");
open(d);

append(d, "Hello world");
append(d, PageBreak());
append(d, "Hello again");
append(d, PageBreak());
append(d, "Hello again");
append(d, PageBreak());
append(d, "Hello again");
append(d, PageBreak());
append(d, "Hello again");

close(d);
rptview(d);

另请参阅

主题