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
import { testres1 } from './getdata.js'
import { pdfrouter } from "./generatepdf.js";
import { insertdata } from "./insert.js";
import { pool } from "./dbconn.js";
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
}
}
const testres2 = await createPDF()
pdf file creation function - generatepdf.js
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 }
getdata.js - this js gets all the data needed for puppeteer
const testres1 = async (connection) => {
console.log('entered create pdf')
const query = `SELECT employeeid, uid, payout
SUM(CAST(COALESCE(NULLIF(payout, ''), '0') AS decimal)) OVER(PARTITION BY employeeid) AS sumpayout
FROM test_stage`
const queryPrommises = [];
queryPrommises.push(connection.query(query))
//console.log(result.rows)
const result = await Promise.all(queryPrommises);
let tempmap = {}
//console.log('result ' + result[0].rows[0])
const res = Object.values(result[0].rows.reduce((all, y) => {
const group = all[y.employeeid] || {
employee_name: y.employeename,
employee_id: y.employeeid,
data: []
};
group.data.push({
payout: y.payout
})
all[y.employeeid] = group;
return all;
}, {}));
//await connection.end()
//console.log('@@@@@ ' + JSON.stringify(res))
return JSON.stringify(res)
}