|
|
|
@ -3,6 +3,7 @@ import fs from 'fs';
|
|
|
|
|
import moment from 'moment';
|
|
|
|
|
import PDFMerger from 'pdf-merger-js';
|
|
|
|
|
|
|
|
|
|
// get params from console
|
|
|
|
|
let [
|
|
|
|
|
url,
|
|
|
|
|
title = '',
|
|
|
|
@ -14,8 +15,10 @@ const path = './pdf';
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(path)) fs.mkdirSync(path);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let browser;
|
|
|
|
|
try{
|
|
|
|
|
// start puppeteer
|
|
|
|
|
browser = await puppeteer.launch({
|
|
|
|
|
headless: true,
|
|
|
|
|
args: [
|
|
|
|
@ -26,18 +29,23 @@ try{
|
|
|
|
|
]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// open page
|
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
page.setDefaultNavigationTimeout(0);
|
|
|
|
|
await page.goto(url);
|
|
|
|
|
await Promise.any([
|
|
|
|
|
page.mainFrame().waitForNavigation({waitUntil: 'networkidle0'}),
|
|
|
|
|
new Promise(resolve => setTimeout(resolve,1000))
|
|
|
|
|
new Promise(resolve => setTimeout(resolve,2000))
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// 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: `
|
|
|
|
|
.hidden-print {
|
|
|
|
@ -48,8 +56,9 @@ try{
|
|
|
|
|
}
|
|
|
|
|
`});
|
|
|
|
|
|
|
|
|
|
// create pdf for pages
|
|
|
|
|
const pages = await page.pdf({
|
|
|
|
|
//path: `${path}/${date}_${title}_pages.pdf`,
|
|
|
|
|
//path: `${path}/${filename}_pages.pdf`,
|
|
|
|
|
format: 'A4',
|
|
|
|
|
margin: {
|
|
|
|
|
top: '20mm',
|
|
|
|
@ -82,6 +91,7 @@ try{
|
|
|
|
|
printBackground: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// load cover template and replace values
|
|
|
|
|
let coverHTML = fs.readFileSync('./cover.html', {encoding: 'utf-8', flag: 'r'});
|
|
|
|
|
|
|
|
|
|
Object.entries({
|
|
|
|
@ -90,11 +100,13 @@ try{
|
|
|
|
|
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}/${date}_${title}_cover.pdf`,
|
|
|
|
|
//path: `${path}/${filename}_cover.pdf`,
|
|
|
|
|
format: 'A4',
|
|
|
|
|
margin: {
|
|
|
|
|
top: '20mm',
|
|
|
|
@ -104,16 +116,20 @@ try{
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// close puppeteer
|
|
|
|
|
await browser.close();
|
|
|
|
|
|
|
|
|
|
// merge both pdfs and save
|
|
|
|
|
const merger = new PDFMerger();
|
|
|
|
|
merger.add(cover);
|
|
|
|
|
merger.add(pages);
|
|
|
|
|
await merger.save(`${path}/${date}_${title.trim().replaceAll(' ', '_')}.pdf`)
|
|
|
|
|
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();
|
|
|
|
|
throw e;
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('finished');
|
|
|
|
|