add README and comments

master
adb 3 years ago
parent 78ef6faac6
commit 688901ae07

@ -0,0 +1,13 @@
# docs2pdf
===
simple tool to create pdfs from online documents (hedgedoc)
## install
```
npm i
```
## run script
```
node index.js "https://foo.bar"
```

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

Loading…
Cancel
Save