Fix PDF generation: wait for includes to load and improve error handling
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Colin 2025-12-01 14:49:58 -05:00
parent 1ba37bbd75
commit 73c4533926
Signed by: colin
SSH Key Fingerprint: SHA256:nRPCQTeMFLdGytxRQmPVK9VXY3/ePKQ5lGRyJhT5DY8
1 changed files with 39 additions and 5 deletions

View File

@ -122,8 +122,25 @@ async function generatePdf(browser, htmlFile) {
timeout: 30000
});
// Wait a bit for any JavaScript to finish
await page.waitForTimeout(1000);
// Wait for includes to load - check if header-include exists and has content
try {
await page.waitForFunction(() => {
const headerInclude = document.getElementById('header-include');
// If header-include exists, wait for it to have content (includes loaded)
// If it doesn't exist, that's fine too (page doesn't use includes)
if (headerInclude) {
return headerInclude.innerHTML.trim().length > 0 ||
document.querySelector('.main-nav') !== null;
}
return true; // No header-include, page is ready
}, { timeout: 10000 });
} catch (waitError) {
// If waiting for includes times out, continue anyway
console.warn(`Warning: Includes may not have loaded for ${htmlFile}, continuing...`);
}
// Wait a bit more for any remaining JavaScript to finish
await page.waitForTimeout(500);
// Generate PDF
await page.pdf({
@ -141,8 +158,13 @@ async function generatePdf(browser, htmlFile) {
console.log(`✓ Generated: ${pdfRelativePath}`);
} catch (error) {
console.error(`✗ Failed: ${htmlFile} - ${error.message}`);
// Don't re-throw - let the caller handle it
} finally {
try {
await page.close();
} catch (closeError) {
// Page might already be closed, ignore
}
}
}
@ -190,13 +212,25 @@ async function main() {
]
});
let successCount = 0;
let failCount = 0;
try {
// Generate PDFs for each HTML file
for (const htmlFile of htmlFiles) {
try {
await generatePdf(browser, htmlFile);
successCount++;
} catch (error) {
failCount++;
console.error(`Failed to generate PDF for ${htmlFile}:`, error.message);
// Continue with next file instead of stopping
}
}
console.log(`\n✓ PDF generation complete! Files saved to: ${PDF_DIR}`);
console.log(`\n✓ PDF generation complete!`);
console.log(` Success: ${successCount}, Failed: ${failCount}`);
console.log(` Files saved to: ${PDF_DIR}`);
} finally {
await browser.close();
server.close();