add cover and some tweaks

master
adb 3 years ago
parent 42485da43b
commit 40adfb49c9

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cover</title>
<style>
html, body{
height: 100%;
width: 100%;
margin: 0;
font-family: Helvetica, Arial, sans-serif;
}
body{
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
}
.subtitle{
font-weight: bold;
font-size: 1.2rem ;
}
</style>
</head>
<body>
<h1>{{title}}</h1>
<p class="subtitle">{{subtitle}}</p>
<p class="date">{{date}}</p>
</body>
</html>

@ -1,9 +1,14 @@
import puppeteer from 'puppeteer';
import fs from 'fs';
import moment from 'moment';
import PDFMerger from 'pdf-merger-js';
const [url, title = '', subtitle = ''] = process.argv.slice(2);
const date = moment().format('YYYY-MM-DD');
let [
url,
title = '',
subtitle = '',
date = moment().format('YYYY-MM-DD')
] = process.argv.slice(2);
const path = './pdf';
@ -29,8 +34,12 @@ try{
new Promise(resolve => setTimeout(resolve,1000))
]);
await page.pdf({
path: `${path}/${date}_${title}.pdf`,
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 pages = await page.pdf({
//path: `${path}/${date}_${title}_pages.pdf`,
format: 'A4',
margin: {
top: '20mm',
@ -48,7 +57,7 @@ try{
width: 100%;
padding: 2mm;
border-bottom: 1px #222 solid;
">${[title, subtitle].join(' - ')}</div>
">${[title, subtitle].map(el=>el.trim()).filter(el=>!!el).join(' - ')}</div>
`,
footerTemplate: `
<div style="
@ -63,7 +72,35 @@ try{
printBackground: true
});
let coverHTML = fs.readFileSync('./cover.html', {encoding: 'utf-8', flag: 'r'});
Object.entries({
title, subtitle, date
}).forEach(([key, value]) => {
coverHTML = coverHTML.replaceAll(`{{${key}}}`, value);
});
const page2 = await browser.newPage();
await page2.setContent(coverHTML);
const cover = await page2.pdf({
//path: `${path}/${date}_${title}_cover.pdf`,
format: 'A4',
margin: {
top: '20mm',
bottom: '20mm',
left: '12mm',
right: '12mm'
}
});
await browser.close();
const merger = new PDFMerger();
merger.add(cover);
merger.add(pages);
await merger.save(`${path}/${date}_${title.trim().replaceAll(' ', '_')}.pdf`)
} catch (e) {
if (browser) await browser.close();
throw e;

@ -12,6 +12,7 @@
"license": "",
"dependencies": {
"moment": "^2.29.2",
"pdf-merger-js": "^3.4.0",
"puppeteer": "^13.5.2"
}
}

Loading…
Cancel
Save