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.error("Didn't get an URL"); process.exit(10); } console.log(" -> URL ", url); if (fileDestination === undefined) { console.error("Didn't get a file destination"); process.exit(11); } console.log(" -> File ", fileDestination); if (fs.existsSync(fileDestination)) { console.error("[ERROR] The file \"" + fileDestination + "\" already exists"); process.exit(12); } const browser = await puppeteer.launch(); const page = await browser.newPage(); console.log("Rendering data...") await page.goto(url, {waitUntil: 'networkidle0'}); await page.emulateMediaType('screen'); // To use normal CSS instead of only print styles 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.error('An error occurred:', error); } process.exit(); })();