generated from actions/container-toolkit-action
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaction.js
54 lines (51 loc) · 1.93 KB
/
action.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
"use strict";
const core = require('@actions/core');
const puppeteer = require('puppeteer');
(async () => {
try {
const googlePath = '/opt/google/chrome/chrome';
const webPageURL = core.getInput('webPageURL');
const outputFile = core.getInput('outputFile');
const usePuppeteer = core.getInput('usePuppeteer');
const useScreen = core.getInput('useScreen');
console.log(`Starting PDF generation for ${webPageURL}`);
if (usePuppeteer) {
const pdfDefaults = {
'displayHeaderFooter': false,
'path': outputFile
};
const pdfOpts = Object.assign({}, JSON.parse(core.getInput('pdfOptions')), pdfDefaults);
const pptrOpts = {
executablePath: googlePath,
args: ['--no-sandbox', '--headless', '--disable-gpu']
};
const browser = await puppeteer.launch(pptrOpts);
const daPage = await browser.newPage();
await daPage.goto(webPageURL, {
waitUntil: "networkidle0",
})
if (useScreen) {
await daPage.emulateMediaType('screen');
}
await daPage.pdf(pdfOpts)
await browser.close()
} else {
const { exec } = require("child_process");
exec(`${googlePath} --no-sandbox --headless --disable-gpu --print-to-pdf="${outputFile}" ${webPageURL}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
}
console.log(`DONE. Generated PDf file is at ${outputFile}`)
}
catch (error) {
core.setFailed(error.message);
}
})();