|
|
|
import puppeteer from 'puppeteer';
|
|
|
|
import fs from 'fs';
|
|
|
|
import moment from 'moment';
|
|
|
|
import PDFMerger from 'pdf-merger-js';
|
|
|
|
|
|
|
|
// get params from console
|
|
|
|
let [
|
|
|
|
url,
|
|
|
|
title = '',
|
|
|
|
subtitle = '',
|
|
|
|
date = moment().format('YYY-MM-DD')
|
|
|
|
] = process.argv.slice(2);
|
|
|
|
|
|
|
|
const path = './pdf';
|
|
|
|
|
|
|
|
if (!fs.existsSync(path)) fs.mkdirSync(path);
|
|
|
|
|
|
|
|
|
|
|
|
let browser;
|
|
|
|
try{
|
|
|
|
// start puppeteer
|
|
|
|
browser = await puppeteer.launch({
|
|
|
|
headless: true,
|
|
|
|
args: [
|
|
|
|
"--disable-gpu",
|
|
|
|
"--disable-dev-shm-usage",
|
|
|
|
"--disable-setuid-sandbox",
|
|
|
|
"--no-sandbox",
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
// open page
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(url, { waitUntil: 'networkidle0' });
|
|
|
|
console.log('page loaded');
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
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`,
|
|
|
|
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>
|
|
|
|
`,
|
|
|
|
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
|
|
|
|
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`);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
// try to close browser on exception
|
|
|
|
if (browser) await browser.close();
|
|
|
|
console.error(e);
|
|
|
|
}
|