-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdf.js
101 lines (89 loc) · 2.89 KB
/
pdf.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const puppeteer = require('puppeteer');
const { spawn } = require("child_process");
async function hasNextSlide(page) {
return page.evaluate(_ => !Reveal.isLastSlide() || Reveal.availableFragments().next);
}
async function nextSlide(page) {
return page.evaluate(_ => Reveal.next());
}
async function currentSlide(page) {
return page.evaluate(_ => {
const indices = Reveal.getIndices();
const id = Reveal.getCurrentSlide().getAttribute('id');
return typeof id === 'string' && id.length
? '/' + id
: '/' + indices.h + (indices.v > 0 ? '/' + indices.v : '');
});
}
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
async function screenshot(page, index) {
await page.screenshot({
omitBackground: false,
fullPage: true,
path: `slide-${index}.png`,
});
}
function run(command, args) {
}
async function convertToPdf(index, output) {
console.log(` ✅ ${index + 1} slides generated`);
console.log(`Exporting ${output}`);
let args = [ '-monitor' ];
for (let current = 0; current <= index; current++) {
args.push(`slide-${current}.png`);
}
args.push(output);
spawn('convert', args, { stdio: 'inherit' });
}
async function printToPdf(page, output, timeout) {
await page.waitForSelector('.print-pdf', {visible: true, timeout: timeout });
await page.pdf({ path: output, format: 'A4' });
}
async function exportToPdf(page, output) {
const spinner = [ '❤️ ', '🧡', '💛', '💚', '💙', '💜', '🤎', '🖤', '🤍' ];
let index = 0;
await screenshot(page, index);
while (await hasNextSlide(page)) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(" " + spinner[index % spinner.length] + " Generating slide #" + index + ": " + await currentSlide(page));
index++;
await nextSlide(page);
await delay(1000);
await screenshot(page, index);
}
process.stdout.clearLine();
process.stdout.cursorTo(0);
await convertToPdf(index, output);
}
(async() => {
const type = process.argv[2]
const url = process.argv[3];
const output = process.argv[4];
const timeout = process.argv[5];
console.log(`Processing ${url} to ${output} with timeout set to ${timeout} ms`);
// '--no-sandbox' is set to allow running on Docker without '--cap-add SYS_ADMIN'
const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--window-size=1920,1080',
],
});
const [page] = await browser.pages();
await page.setViewport({ width: 1920, height: 1080 });
page.setDefaultNavigationTimeout(0);
await page.setCacheEnabled(false);
await page.goto(url, { waitUntil: 'networkidle0', timeout: timeout });
if (type === 'exportToPdf') {
await exportToPdf(page, output);
} else if (type === 'printToPdf') {
await printToPdf(page, output, timeout);
}
await browser.close();
})();