And often I get this error in the messages of Switch.
Code: Select all
The script execution has ended while an async function or callback in the script might not have finished yet. Please check the script code for unresolved promises or missing await statements.
In this case from the code below.
Code: Select all
// Create PDF using puppeteer (https://pptr.dev) from HTML-file as input
// Jimmy Hartington, Skabertrang, 2024-02-09
import * as fs from "fs";
import * as tmp from "tmp";
import puppeteer from "puppeteer";
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
// puppeteer
async function generatePDFfromHTML(document: string, outputPath: string) {
const browser = await puppeteer.launch({
args: [
"--disable-web-security",
"--disable-features=SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure",
],
});
const page = await browser.newPage();
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
);
await page.setContent(document);
await page.evaluateHandle("document.fonts.ready");
await page.pdf({ path: outputPath, format: "A4" });
await browser.close();
}
// Usage
let jobName: string = (await job.getName(false)) as string;
let jobPath: string = (await job.get(AccessLevel.ReadOnly)) as string;
let htmlContent = await fs.readFileSync(jobPath, "utf-8");
const tmpFile: string = await tmp.fileSync({
prefix: "newfile",
postfix: ".pdf",
}).name;
// puppeteer
await generatePDFfromHTML(htmlContent, tmpFile)
.then(() => job.log(LogLevel.Info, "PDF created"))
.catch((err) => job.log(LogLevel.Error, "Error in creating pdf: " + err));
let newJob = await job.createChild(tmpFile);
await newJob.sendToSingle(jobName + ".pdf");
// delete tmp files
await fs.unlinkSync(tmpFile);
// send original to null
await job.sendToNull();
}