You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.2 KiB
JavaScript

3 years ago
import puppeteer from 'puppeteer';
import fs from 'fs';
import moment from 'moment';
import PDFMerger from 'pdf-merger-js';
3 years ago
// get params from console
let [
url,
title = '',
subtitle = '',
date = moment().format('YYY-MM-DD')
] = process.argv.slice(2);
3 years ago
const path = './pdf';
if (!fs.existsSync(path)) fs.mkdirSync(path);
3 years ago
let browser;
try{
// start puppeteer
3 years ago
browser = await puppeteer.launch({
headless: true,
args: [
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-sandbox",
]
});
// open page
3 years ago
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' });
console.log('page loaded');
3 years ago
// search for header and subheader in document
if (!title) title = await (await page.waitForSelector('h1')).evaluate(el => el.textContent);
if (!subtitle) subtitle = await (await page.waitForSelector('h2')).evaluate(el => el.textContent);
console.log(title,subtitle);
const filename = `${date}_${title.trim().replaceAll(' ', '_')}`;
// deactivate @print css and add custom css (for pretty code)
await page.emulateMediaType('screen');
await page.addStyleTag({content: `
body {
2 years ago
text-shadow: none !important;
}
.hidden-print {
display: none !important;
}
*:not(code, .code > *){
color: #000 !important;
}
`});
// create pdf for pages
const pages = await page.pdf({
//path: `${path}/${filename}_pages.pdf`,
3 years ago
format: 'A4',
margin: {
top: '20mm',
bottom: '20mm',
left: '12mm',
right: '12mm'
},
displayHeaderFooter: true,
headerTemplate: `
<div style="
text-align: center;
color: #000;
font-family: sans-serif;
font-size: 10px;
width: 100%;
padding: 2mm;
border-bottom: 1px #222 solid;
">${[title, subtitle].map(el=>el.trim()).filter(el=>!!el).join(' | ')}</div>
3 years ago
`,
footerTemplate: `
<div style="
text-align: center;
color: #000;
font-family: sans-serif;
font-size: 10px;
width: 100%;
padding: 2mm;
">Page <span class="pageNumber"></span></div>
`,
printBackground: true
});
// load cover template and replace values
let coverHTML = fs.readFileSync('./cover.html', {encoding: 'utf-8', flag: 'r'});
Object.entries({
title, subtitle, date
}).forEach(([key, value]) => {
coverHTML = coverHTML.replaceAll(`{{${key}}}`, value);
});
// open new page and set template
const page2 = await browser.newPage();
await page2.setContent(coverHTML);
// create pdf for cover
const cover = await page2.pdf({
//path: `${path}/${filename}_cover.pdf`,
format: 'A4',
margin: {
top: '20mm',
bottom: '20mm',
left: '12mm',
right: '12mm'
}
});
// close puppeteer
3 years ago
await browser.close();
// merge both pdfs and save
const merger = new PDFMerger();
merger.add(cover);
merger.add(pages);
await merger.save(`${path}/${filename}.pdf`)
console.log('finished');
console.log(`saved at ${path}/${filename}.pdf`);
3 years ago
} catch (e) {
// try to close browser on exception
3 years ago
if (browser) await browser.close();
console.error(e);
3 years ago
}