diff --git a/docs/docs/pdf.md b/docs/docs/pdf.md
index 75ad0f5339f..52934054753 100644
--- a/docs/docs/pdf.md
+++ b/docs/docs/pdf.md
@@ -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:
- ``: current page number.
- ``: total pages in the document.
@@ -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
diff --git a/src/Docfx.App/PdfBuilder.cs b/src/Docfx.App/PdfBuilder.cs
index 2ca53a239ea..d2b84c90093 100644
--- a/src/Docfx.App/PdfBuilder.cs
+++ b/src/Docfx.App/PdfBuilder.cs
@@ -187,8 +187,8 @@ IResult TocPage(string url)
Task 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());
@@ -238,6 +238,22 @@ static string ExpandTemplate(string? pdfTemplate, int pageNumber, int totalPages
.Replace("", $"{totalPages}")
.Replace("", $"{totalPages}");
}
+
+ 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;
+ }
+ }
}
}