24 lines
1.3 KiB
JavaScript
24 lines
1.3 KiB
JavaScript
// build-pdf.py runs this in the playwright-for-claude image: it prints /work/page.html to /work/out.pdf with Chromium.
|
|
const { chromium } = require('/app/node_modules/playwright');
|
|
|
|
(async () => {
|
|
const browser = await chromium.launch({ args: ['--no-sandbox', '--disable-dev-shm-usage'] });
|
|
const page = await browser.newPage({ viewport: { width: 1100, height: 1400 } });
|
|
// A font that fails to load falls back to another, and the pages no longer fit their sheets, so any failed request fails the build.
|
|
const failed = [];
|
|
page.on('requestfailed', (request) => failed.push(request.url()));
|
|
page.on('response', (response) => { if (response.status() >= 400) failed.push(`${response.status()} ${response.url()}`); });
|
|
// networkidle waits for the Google Fonts the page loads; fonts.ready waits for them to apply.
|
|
await page.goto('file:///work/page.html', { waitUntil: 'networkidle' });
|
|
await page.evaluate(() => document.fonts.ready);
|
|
if (failed.length) {
|
|
console.error(`Not printed, since these failed to load: ${failed.join(', ')}`);
|
|
await browser.close();
|
|
process.exit(1);
|
|
}
|
|
await page.emulateMedia({ media: 'print' });
|
|
// preferCSSPageSize takes the page's own @page rule: A4 landscape.
|
|
await page.pdf({ path: '/work/out.pdf', format: 'A4', printBackground: true, preferCSSPageSize: true });
|
|
await browser.close();
|
|
})();
|