-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.js
41 lines (35 loc) · 1.03 KB
/
print.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const fs = require('fs')
const puppeteer = require('puppeteer')
const baseUrl = process.env.PRINT_URL || 'http://localhost:4002'
async function writePDF() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(`${baseUrl}/print`, {
waitUntil: 'networkidle2',
})
const today = new Date()
const formattedDate = `${String(today.getMonth() + 1).padStart(
2,
'0'
)}-${String(today.getDate()).padStart(2, '0')}-${today.getFullYear()}`
await page.pdf({
path: `pdfs/CarbonPlan-Datasets-${formattedDate}.pdf`,
printBackground: true,
format: 'letter',
})
await browser.close()
}
try {
console.log('removing old PDFs...')
fs.rmSync('pdfs/', { recursive: true })
console.log('removed.')
} catch (e) {
console.log('no existing PDFs.')
}
console.log('creating new PDFs folder...')
const result = fs.mkdirSync('pdfs/')
console.log('created.')
console.log('writing new files...')
writePDF()
.then(() => console.log('done!'))
.catch((e) => console.log('errored', e))