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
6 changes: 4 additions & 2 deletions src/Docfx.App/PdfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ static async Task CreatePdf(IBrowser browser, ProgressTask task, Uri outlineUrl,
var pageNumber = 1;
var nextPageNumber = 1;

var page = await browser.NewPageAsync(new() { UserAgent = "docfx/pdf" });

// Make progress at 99% before merge PDF
task.MaxValue = pages.Length + (pages.Length / 99.0);
foreach (var (url, node) in pages)
Expand All @@ -158,14 +160,14 @@ static async Task CreatePdf(IBrowser browser, ProgressTask task, Uri outlineUrl,

async Task<byte[]> CapturePdf(Uri url, int startPageNumber)
{
var page = await browser.NewPageAsync();
var response = await page.GotoAsync(url.ToString());
if (response is null || !response.Ok)
throw new InvalidOperationException($"Failed to build PDF page [{response?.Status}]: {url}");

await page.AddScriptTagAsync(new() { Content = EnsureHeadingAnchorScript });
await page.AddScriptTagAsync(new() { Content = InsertHiddenPageScript(startPageNumber - 1) });
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
await page.WaitForFunctionAsync("!window.docfx || window.docfx.ready");
var bytes = await page.PdfAsync(new()
{
HeaderTemplate = "<span></span>",
Expand Down
25 changes: 16 additions & 9 deletions templates/modern/src/docfx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ declare global {
async function init() {
window.docfx = window.docfx || {}

initTheme()

await Promise.all([
enableSearch(),
renderInThisArticle(),
renderMarkdown(),
renderNav(),
highlight()
])
const pdfmode = navigator.userAgent.indexOf('docfx/pdf') >= 0
if (pdfmode) {
await Promise.all([
renderMarkdown(),
highlight()
])
} else {
await Promise.all([
initTheme(),
enableSearch(),
renderInThisArticle(),
renderMarkdown(),
renderNav(),
highlight()
])
}

window.docfx.ready = true

Expand Down