Skip to content

Commit

Permalink
feat: pdfHeaderTemplate/pdfFooterTemplate supports value as file path (
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored Jul 10, 2024
1 parent 5c7f902 commit 156cf20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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 @@ -187,8 +187,8 @@ IResult TocPage(string url)

Task<byte[]> PrintHeaderFooter(Outline toc, int pageNumber, int totalPages, Page contentPage)
{
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 @@ -238,6 +238,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

0 comments on commit 156cf20

Please sign in to comment.