sources-of-light/index.mjs

40 lines
1016 B
JavaScript
Raw Normal View History

2024-11-14 05:15:53 +00:00
import fs from 'fs';
import path from 'path';
import puppeteer from 'puppeteer-core';
import {fileURLToPath} from 'url';
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const renderedDir = path.join(dirname, 'rendered');
fs.mkdirSync(renderedDir, { recursive: true });
const files = fs.readdirSync(path.join(dirname, 'pages'))
.sort()
.map(file => path.join(dirname, 'pages', file));
const browser = await puppeteer.launch({
2024-11-14 21:57:39 +00:00
browser: 'firefox',
executablePath: '/usr/bin/firefox',
2024-11-14 05:15:53 +00:00
});
const page = await browser.newPage();
for (const file of files) {
const targetFile = path.join(renderedDir, path.basename(file, '.html') + '.pdf');
console.log(`processing ${path.basename(file)}...`);
2024-11-14 21:57:39 +00:00
await page.goto(`file://${file}`);
2024-11-14 05:15:53 +00:00
await page.pdf({
width: '9.25in',
height: '12.25in',
path: targetFile,
printBackground: true,
});
console.log(` generated PDF at ${targetFile}`);
}
await browser.close();