init
commit
42485da43b
@ -0,0 +1,72 @@
|
||||
import puppeteer from 'puppeteer';
|
||||
import fs from 'fs';
|
||||
import moment from 'moment';
|
||||
|
||||
const [url, title = '', subtitle = ''] = process.argv.slice(2);
|
||||
const date = moment().format('YYYY-MM-DD');
|
||||
|
||||
const path = './pdf';
|
||||
|
||||
if (!fs.existsSync(path)) fs.mkdirSync(path);
|
||||
|
||||
let browser;
|
||||
try{
|
||||
browser = await puppeteer.launch({
|
||||
headless: true,
|
||||
args: [
|
||||
"--disable-gpu",
|
||||
"--disable-dev-shm-usage",
|
||||
"--disable-setuid-sandbox",
|
||||
"--no-sandbox",
|
||||
]
|
||||
});
|
||||
|
||||
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))
|
||||
]);
|
||||
|
||||
await page.pdf({
|
||||
path: `${path}/${date}_${title}.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].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
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
} catch (e) {
|
||||
if (browser) await browser.close();
|
||||
throw e;
|
||||
}
|
||||
|
||||
console.log('finished');
|
@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "docs2pdf",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "//ADB",
|
||||
"license": "",
|
||||
"dependencies": {
|
||||
"moment": "^2.29.2",
|
||||
"puppeteer": "^13.5.2"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue