-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (63 loc) · 2.16 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const puppeteer = require('puppeteer');
const fs = require('fs');
const sharp = require('sharp');
const exec = require('@actions/exec');
(async () => {
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
await page.setRequestInterception(true);
// convert images to WEBP
page.on('request', async (req) => {
if (req.resourceType() !== 'image') {
req.continue();
return;
}
try {
const response = await fetch(req.url(), {
method: req.method(),
headers: req.headers(),
});
const buffer = await response.arrayBuffer();
const image = await sharp(buffer)
.webp({ quality: 100, lossless: true })
.rotate()
.toBuffer();
req.respond({ body: image });
} catch {
req.continue();
}
});
// set page content (HTML and CSS)
const html = fs.readFileSync('index.html', 'utf-8');
await page.setContent(html, { waitUntil: 'networkidle0' });
await page.addStyleTag({ path: 'style.css' });
// add custom elements
await page.addScriptTag({ path: 'components/infoItem.js' });
await page.addScriptTag({ path: 'components/resumeItem.js' });
await page.addScriptTag({ path: 'components/resumeAchievementItem.js' });
await page.addScriptTag({ path: 'components/skillProgress.js' });
// to reflect CSS used for screens instead of print
await page.emulateMediaType('screen');
// wait for the fonts to be loaded
await page.evaluateHandle('document.fonts.ready');
// get page dimensions
const elem = await page.$('body');
const { height, width } = await elem.boundingBox();
// get page title
const title = await page.title();
const fileTitle = title.replace(/\s/g, '-').toLowerCase();
const tmpFileTitle = fileTitle + '.tmp.pdf';
const finalFileTitle = fileTitle + '.pdf';
await page.pdf({
path: tmpFileTitle,
width,
height,
printBackground: true
});
await browser.close();
// shrink PDF
await exec.exec(`./shrinkpdf.sh -r 300 -o ${finalFileTitle} ${tmpFileTitle}`);
// remove temporary file
await exec.exec(`rm ${tmpFileTitle}`);
console.log(`PDF generated: ${finalFileTitle}`);
})();