| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- const puppeteer = require('puppeteer');
- const fs = require('fs');
- (async function ()
- {
- try
- {
- console.log("StageRack HTML to PDF conversion tool");
- var args = process.argv.slice(2);
- let url = args[0];
- let fileDestination = args[1];
-
- if (url === undefined)
- {
- console.log("Didn't get an URL");
- process.exit(10);
- }
- console.log(" -> URL ", url);
-
- if (fileDestination === undefined)
- {
- console.log("Didn't get a file destination");
- process.exit(11);
- }
- console.log(" -> File ", fileDestination);
-
- if (fs.existsSync(fileDestination))
- {
- console.log("[ERROR] The file \"" + fileDestination + "\" already exists");
- process.exit(12);
- }
-
- const browser = await puppeteer.launch({headless: true, dumpio: true, args: ['--no-sandbox']});
- const page = await browser.newPage();
- page.on('requestfailed', function(request)
- {
- console.log("Request failed", request);
- console.log("Failure Error Text", request.failure().errorText);
- process.exit(13);
- });
- page.on('pageerror', function(message)
- {
- console.log("Page Error", message);
- process.exit(13);
- });
- console.log("Rendering data...");
- let response = await page.goto(url, {waitUntil: 'networkidle0'});
- let httpStatus = response.status();
- console.log(" -> Got HTTP code " + httpStatus);
- if (httpStatus !== 200)
- {
- console.log("Error: got HTTP response " + httpStatus + ". Aborting");
- process.exit(20);
- }
- await page.emulateMediaType('print');
-
-
- console.log("Creating the PDF document...");
- // Documentation for this function: https://pptr.dev/api/puppeteer.page
- await page.pdf(
- {
- path: fileDestination,
- format: 'A4',
- omitBackground: false,
- printBackground: true,
- preferCSSPageSize: true,
- margin:
- {
- left: "1.27cm",
- right: "1.27cm",
- top: "1.27cm",
- bottom: "1.27cm",
- }
- });
- console.log('Conversion complete. PDF file generated successfully.');
-
- await browser.close();
- } catch (error)
- {
- console.log('An error occurred:', error);
- process.exit(1);
- }
- process.exit();
- })();
|