import puppeteer from 'puppeteer'; import { resolve, dirname } from 'path'; import { fileURLToPath } from 'url'; import { access } from 'fs/promises'; const __dir = dirname(fileURLToPath(import.meta.url)); async function fileExists(path) { try { await access(path); return true; } catch { return false; } } const root = resolve(__dir, '..'); const inputPath = resolve(root, 'output', 'book.html'); const outputPath = resolve(root, 'output', 'kaleidoskop.pdf'); async function generate() { try { await access(inputPath); } catch { console.error('output/book.html not found — run `pnpm build` first'); process.exit(1); } // On ARM64 (e.g. WSL2 on Apple Silicon / Raspberry Pi), Puppeteer's bundled // Chrome is x86-64 and won't run. Use the system Chromium instead: // sudo apt-get install -y chromium const systemChromium = '/usr/bin/chromium'; const useSystem = await fileExists(systemChromium); console.log(`Launching Puppeteer (${useSystem ? 'system Chromium' : 'bundled Chrome'})…`); const browser = await puppeteer.launch({ headless: true, ...(useSystem ? { executablePath: systemChromium } : {}), }); const page = await browser.newPage(); await page.goto(`file://${inputPath}`, { waitUntil: 'networkidle0' }); // 8.75 × 8.75 inches = trim (8.5×8.5) + 0.125in bleed on each side await page.pdf({ path: outputPath, width: '8.75in', height: '8.75in', printBackground: true, margin: { top: 0, bottom: 0, left: 0, right: 0 }, }); await browser.close(); console.log(`PDF written to output/kaleidoskop.pdf`); } generate().catch(err => { console.error(err); process.exit(1); });