Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/docs/pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Indicates whether to include background graphics when rendering the pdf.

### `pdfHeaderTemplate`

HTML template for the print header. Should be valid HTML markup with following HTML elements used to inject printing values into them:
HTML template for the print header, or a path to an HTML page relative to the root of the output directory. Should be valid HTML markup with following HTML elements used to inject printing values into them:

- `<span class='pageNumber'></span>`: current page number.
- `<span class='totalPages'></span>`: total pages in the document.
Expand All @@ -95,7 +95,7 @@ HTML template for the print header. Should be valid HTML markup with following H

### `pdfFooterTemplate`

HTML template for the print footer. Should use the same format as the [header template](#pdfheadertemplate). Uses the following default footer template if unspecified:
HTML template for the print footer, or a path to an HTML page relative to the root of the output directory. Should use the same format as the [header template](#pdfheadertemplate). Uses the following default footer template if unspecified:

```html
<div style="width: 100%; font-size: 12px;">
Expand Down
20 changes: 18 additions & 2 deletions src/Docfx.App/PdfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ IResult TocPage(string url)

Task<byte[]> PrintHeaderFooter(Outline toc, int pageNumber, int totalPages)
{
var headerTemplate = ExpandTemplate(toc.pdfHeaderTemplate, pageNumber, totalPages);
var footerTemplate = ExpandTemplate(toc.pdfFooterTemplate ?? DefaultFooterTemplate, pageNumber, totalPages);
var headerTemplate = ExpandTemplate(GetHeaderFooter(toc.pdfHeaderTemplate), pageNumber, totalPages);
var footerTemplate = ExpandTemplate(GetHeaderFooter(toc.pdfFooterTemplate) ?? DefaultFooterTemplate, pageNumber, totalPages);

return headerFooterCache.GetOrAdd((headerTemplate, footerTemplate), _ => PrintHeaderFooterCore());

Expand Down Expand Up @@ -222,6 +222,22 @@ static string ExpandTemplate(string? pdfTemplate, int pageNumber, int totalPages
.Replace("<span class='totalPages'></span>", $"<span>{totalPages}</span>")
.Replace("<span class=\"totalPages\"></span>", $"<span>{totalPages}</span>");
}

string? GetHeaderFooter(string? template)
{
if (string.IsNullOrEmpty(template))
return template;

try
{
var path = Path.Combine(outputFolder, template);
return File.Exists(path) ? File.ReadAllText(path) : template;
}
catch
{
return template;
}
}
}
}

Expand Down