40 lines
1016 B
JavaScript
40 lines
1016 B
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({
|
|
browser: 'firefox',
|
|
executablePath: '/usr/bin/firefox',
|
|
});
|
|
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}`);
|
|
|
|
await page.pdf({
|
|
width: '9.25in',
|
|
height: '12.25in',
|
|
path: targetFile,
|
|
printBackground: true,
|
|
});
|
|
|
|
console.log(` generated PDF at ${targetFile}`);
|
|
}
|
|
|
|
await browser.close();
|