Fix PDF generation: wait for includes to load and improve error handling
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
1ba37bbd75
commit
73c4533926
|
|
@ -122,8 +122,25 @@ async function generatePdf(browser, htmlFile) {
|
||||||
timeout: 30000
|
timeout: 30000
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait a bit for any JavaScript to finish
|
// Wait for includes to load - check if header-include exists and has content
|
||||||
await page.waitForTimeout(1000);
|
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
|
// Generate PDF
|
||||||
await page.pdf({
|
await page.pdf({
|
||||||
|
|
@ -141,8 +158,13 @@ async function generatePdf(browser, htmlFile) {
|
||||||
console.log(`✓ Generated: ${pdfRelativePath}`);
|
console.log(`✓ Generated: ${pdfRelativePath}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`✗ Failed: ${htmlFile} - ${error.message}`);
|
console.error(`✗ Failed: ${htmlFile} - ${error.message}`);
|
||||||
|
// Don't re-throw - let the caller handle it
|
||||||
} finally {
|
} finally {
|
||||||
await page.close();
|
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 {
|
try {
|
||||||
// Generate PDFs for each HTML file
|
// Generate PDFs for each HTML file
|
||||||
for (const htmlFile of htmlFiles) {
|
for (const htmlFile of htmlFiles) {
|
||||||
await generatePdf(browser, htmlFile);
|
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 {
|
} finally {
|
||||||
await browser.close();
|
await browser.close();
|
||||||
server.close();
|
server.close();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue