Skip to content

Commit 87e4560

Browse files
committed
fix: Quick pubstash settings tweak
1 parent 3b0c43e commit 87e4560

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

pubstash/server.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ async function getBrowser(): Promise<Browser> {
105105
async function convertHtmlToPdf(html: string): Promise<Buffer> {
106106
const activeBrowser = await getBrowser();
107107
const page = await activeBrowser.newPage();
108+
page.setDefaultTimeout(PAGE_TIMEOUT_MS);
109+
page.setDefaultNavigationTimeout(PAGE_TIMEOUT_MS);
108110
try {
109111
// Accumulate per-page box data from paged.js events
110112
const collectedPages: PageBoxes[] = [];
@@ -179,6 +181,24 @@ async function convertHtmlToPdf(html: string): Promise<Buffer> {
179181
// Wait for paged.js DOM to land
180182
await page.waitForSelector('.pagedjs_pages', { timeout: PAGE_TIMEOUT_MS });
181183

184+
// Give the browser time to finish layout/paint for long documents.
185+
// The paged.js 'rendered' event fires when JS work is done, but
186+
// Chromium may still be laying out pages for large docs.
187+
await page.waitForFunction(
188+
() => {
189+
const pages = document.querySelectorAll('.pagedjs_page');
190+
// Ensure at least one page exists and all pages have content
191+
if (pages.length === 0) return false;
192+
const last = pages[pages.length - 1];
193+
return last.getBoundingClientRect().height > 0;
194+
},
195+
{ timeout: PAGE_TIMEOUT_MS },
196+
);
197+
// Additional idle wait to let paint/compositing finish
198+
await page.evaluate(
199+
() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))),
200+
);
201+
182202
// Extract <meta> tags for PDF metadata
183203
const meta: PdfMeta = await page.evaluate(() => {
184204
const m: Record<string, string> = {};
@@ -237,6 +257,9 @@ async function convertHtmlToPdf(html: string): Promise<Buffer> {
237257
}, OUTLINE_TAGS);
238258

239259
// Generate the raw PDF
260+
console.info(
261+
`[pubstash] paged.js produced ${collectedPages.length} pages, generating PDF…`,
262+
);
240263
const rawPdf = await page.pdf({
241264
format: 'Letter',
242265
printBackground: true,

0 commit comments

Comments
 (0)