sources-of-light/index.mjs

41 lines
1.0 KiB
JavaScript

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({
executablePath: '/usr/bin/google-chrome',
});
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)}...`);
await page.goto(`file://${file}`, {
waitUntil: 'networkidle0',
});
await page.pdf({
width: '9.25in',
height: '12.25in',
path: targetFile,
printBackground: true,
});
console.log(` generated PDF at ${targetFile}`);
}
await browser.close();