添加HTML页眉和页脚
配置渲染选项以在使用IronPDF进行PDF文档渲染时包含HTML页眉和页脚。IronPDF是Iron Software推出的库,允许进行高级PDF生成和操作。
使用 IronPdf 灵活的 PDF 渲染工具,用分隔线、HTML 片段定义页眉内容,并指定页眉的最大高度。 同样地,使用IronPDF提供的 htmlHeader 属性定义页脚内容。
请注意,页眉和页脚的高度不会自动检测,这意味着它们可能会与主要HTML内容重叠。 自定义边距以确保页眉和页脚适当定位。
const {PdfDocument} = require("@ironsoftware/ironpdf");
(async () => {
// Configure render options
const renderOptions = {
firstPageNumber: 1, // Use 2 if a cover page will be appended
// Build a footer using html to style the text
// mergeable fields are:
// {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
htmlFooter: {
maxHeight: 15, //millimeters
htmlFragment: "<center><i>{page} of {total-pages}<i></center>",
dividerLine: true,
},
// Build a header using an image asset
htmlHeader: {
maxHeight: 15, //millimeters
htmlFragment: "<img src='logo.png'>",
},
// Use sufficient MarginBottom to ensure that the HtmlFooter does not overlap with the main PDF page content.
margin: {
top: 25, // Create 25mm space for the header
bottom: 25, // Create 25mm space for the footer
},
};
})();