I am using handlebars and puppeteer lib for generating pdfs . For handlebar input i am creating a array of data. Now i am looping through that data array and passing each record to puppeteer lib to generate pdf. I have large set of data . When loop is running entire application is hanging and it gives me the below error
How to loop data with puppeeteer so tht i can generate multiple pdf file base64 string
timed out after 30000 ms while waiting for the WS endpoint URL to appear in stdout! at ChromeLauncher.launch
below is the code
async function createPDF() {
const insertquery = ('INSERT INTO base64staging(filename, base64str,testid) VALUES($1, $2, $3)');
const queryPrommises1 = [];
// const client = await pool.connect()
const result = await testres1(pool) // This returns large set of data (10k to 20k records)
console.log('result ' + result)
var promiseresult = []
if (result !== null && result !== undefined) {
JSON.parse(result).forEach(async res => {
const res1 = await pdfrouter(res)
queryPrommises1.push(pool.query(insertquery, [res.employeeid.toString(), res1, res.testid]))
const result1 = await Promise.allSettled(queryPrommises1);
console.log(result1.length)
result1.forEach(res => {
if (res.status === 'fulfilled') {
promiseresult.push(res.value)
}
})
})
return promiseresult
}
}
pdf file creation function
const pdfrouter = async (data) => {
process.setMaxListeners(Infinity)
try {
const templateHtml = fs.readFileSync(
join(process.cwd(), "./index.html"),
"utf8"
);
var template = Handlebars.compile(templateHtml);
var html = encodeURIComponent(template(data));
var milis = new Date();
milis = milis.getTime();
const options = {
width: '1230px',
headerTemplate: "<p></p>",
footerTemplate: "<p></p>",
displayHeaderFooter: false,
margin: {
top: "10px",
bottom: "30px"
},
printBackground: true,
};
const browser = await launch({
args: ['--no-sandbox'],
headless: "new"
});
var page = await browser.newPage();
console.log('Page opened')
await page.goto(`data:text/html;charset=UTF-8,${html}`, {
waitUntil: 'networkidle0'
});
let pdfBuffer = await page.pdf(options);
await browser.close();
console.log("Done: invoice.pdf is created!");
return pdfBuffer.toString('base64')
} catch (err) {
console.log("ERROR:", err);
return err
}
}
export { pdfrouter }



