PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Use CHtmlHeaderFooterOptions with setHeaderFooterOptions() to render separate HTML templates in the top and bottom page-margin areas. Set displayHeaderFooter to true, and reserve enough top/bottom margin for the templates. A template that exceeds its available width or height causes conversion to fail; increase the margin or reduce the template size.
The following example treats the first physical page as a cover page: it hides that page's templates and starts numbering the second physical page at 1.
import CPDFGeneration.CConverterProperties;
import CPDFGeneration.CHtmlConverter;
import CPDFGeneration.CHtmlHeaderFooterOptions;
CConverterProperties properties = new CConverterProperties();
try {
properties.setPageMargins(48, 40, 48, 40);
CHtmlHeaderFooterOptions options = new CHtmlHeaderFooterOptions();
options.displayHeaderFooter = true;
options.headerTemplate = "<div style='font-size:10px'>"
+ "<span class='title'></span></div>";
options.footerTemplate = "<div style='font-size:10px;text-align:right'>"
+ "<span class='date'></span> | "
+ "<span class='pageNumber'></span>/<span class='totalPages'></span></div>";
options.title = "Report";
options.skipLeadingTemplatePages = 1;
options.skipLeadingNumberedPages = 1;
properties.setHeaderFooterOptions(options);
CHtmlHeaderFooterOptions saved = properties.getHeaderFooterOptions();
System.out.println("Headers/footers enabled: " + saved.displayHeaderFooter);
CHtmlConverter.CErrorCode status = CHtmlConverter.ConvertToPdf(
"input.html", "header-footer.pdf", properties);
if (status != CHtmlConverter.CErrorCode.HTML_CONVERTER_SUCCESS) {
throw new IllegalStateException("HTML conversion failed: " + status);
}
} finally {
properties.release();
}| Field | Default | Description |
|---|---|---|
displayHeaderFooter | false | Enable or disable header/footer rendering. |
headerTemplate | Empty string | Header HTML; an empty string selects the default header. |
footerTemplate | Empty string | Footer HTML; an empty string selects the default footer. |
baseUri | Empty string | Base URI for template resources; empty uses the body resource directory. |
title | Empty string | Displayed title; empty uses the body's HTML title. |
url | Empty string | Displayed URL; supply it when converting HTML without a source URL. |
date | Empty string | Displayed date; empty uses the local date/time of the conversion. |
skipLeadingTemplatePages | 0 | Hide templates on the first N physical pages without changing numbering. |
skipLeadingNumberedPages | 0 | Exclude the first N physical pages from numbering and template rendering; subsequent pages start at 1. |
Both skip counts must be nonnegative. When both are set, templates are hidden on the first max(skipLeadingTemplatePages, skipLeadingNumberedPages) physical pages, and numbering is offset only by skipLeadingNumberedPages.
Templates support elements with the CSS classes date, title, url, pageNumber and totalPages, for example <span class='pageNumber'></span>. Page counters also support the text placeholders and . totalPages excludes the pages skipped by skipLeadingNumberedPages.
An empty template selects the built-in template. To leave a header or footer explicitly blank, use <div></div>. Templates use the renderer's supported HTML/CSS and do not execute JavaScript.
setHeaderFooterOptions() copies the configuration; changing the supplied object later does not change the properties. getHeaderFooterOptions() returns an independent copy that must be applied again to update the configuration. These settings can also be supplied to email PDF conversion through CConverterProperties.