import {PdfDocument, AffixFonts} from "@ironsoftware/ironpdf";
(async () => {
// Configure render options
const options = {
firstPageNumber: 1, // Use 2 if a cover page will be appended
// Add a header to every page
textHeader: {
dividerLine: true,
centerText: "{html-title}",
font: AffixFonts.Helvetica, // Use the font name or font file path
fontSize: 12,
},
// Add a footer
textFooter: {
dividerLine: true,
leftText: "{date} {time}",
rightText: "{page} of {total-pages}",
font: AffixFonts.Arial, // Use the font name or font file path
fontSize: 10,
},
// Mergeable fields are: {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
margin: {
top: 25, // Create 25mm space for the header
bottom: 25, // Create 25mm space for the footer
},
};
// Define HTML content
const htmlContent = "<h1>Hello IronPDF</h1>";
// Render HTML content to a PDF with headers and footers
const pdf = await PdfDocument.fromHtml(htmlContent, {renderOptions: options });
// Save the PDF with headers and footers
await pdf.saveAs("header_footer.pdf");
})();
添加經典的文本頁首和頁尾
了解如何在渲染HTML內容時向PDF文件添加文字頁首和頁腳。
要包括文本頁首和頁尾,您需要在渲染選項對象中指定它們。 使用分隔線定義標題內容,居中文本顯示 HTML 標題、字體設置和字體大小。同樣,使用textFooter屬性定義頁腳。 您可以使用像是{page}、{total-pages}、{url}、{date}、{time}、{html-title}和{pdf-title}這樣的欄位來個性化內容。