From 156cf201002e268d49c286cda841edea0c5d060c Mon Sep 17 00:00:00 2001 From: Yufei Huang Date: Wed, 10 Jul 2024 10:24:58 +0800 Subject: [PATCH] feat: pdfHeaderTemplate/pdfFooterTemplate supports value as file path (#10099) --- docs/docs/pdf.md | 4 ++-- src/Docfx.App/PdfBuilder.cs | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) 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; + } + } } }