I have implemented a PDF download feature in my current project using Puppeteer. I'm hosting the project on AWS with Node 18.
Locally, it works perfectly fine, but in production when I hit the PDF download API, it returns the following error: ReferenceError: ReadableStream is not defined
.
Puppeteer version is 22.3.0
Here is the Puppeteer code:
public getReportPdf = async(req: Request, res: Response) = >{
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
const {
reportId
} = req.params;
const reportData = await this.reportRepo.getJobAndTemplateWithReport(reportId);
const htmlData = await ejs.renderFile('src/helper/templates/report.ejs', {
record: reportData[0],
reportOverview: REPORT_OVERVIEW
});
const image = await getBase64Image('https://maxscaffold-front-stg.taskgrids.com/assets/images/Max-Builders-Default.png') // Need to pass here companyLogo url
await page.setContent(htmlData);
const pdfBuffer = await page.pdf({
displayHeaderFooter: true,
headerTemplate: ` < div style = "font-size: 10px; display: flex; justify-content: center; align-items: center; margin-left: 300px" > <div > Powered by < /div>
<img style="height: 50px; width: auto; margin-left: 5px" src="data:image/png;
base64,
$ {
image
}
"/>
</div>`,
format: 'A4',
printBackground: true,
preferCSSPageSize: true,
scale: 0.8,
margin: {
top: '100px',
bottom:'50px'
}
});
await page.close();
await browser.close();
const savepath = `./${STORAGE_BUCKET.DEFAULT}/${reportId}_report.pdf`;
fs.writeFile(savepath, pdfBuffer, (err) => {
if (err) {
console.log('err', err);
}
});
setTimeout(() => {
fs.promises.unlink(savepath);
}, 25000);
const data = process.env.NODE_URL + `api/images/${reportId}_report.pdf`;
return generalResponse(req, res,data, successMessage.REPORT_DISPLAY_SUCCESS, true, responseFlag.SUCCESS);
} catch (error) {
await page.close();
await browser.close();
return generalResponse(req, res, error, errorMessage.SOMETHING_WRONG, false, responseFlag.ERROR);
}
};"
Can anyone please help me to resolve this issue?
I have tried Node version 18 locally, then it works perfectly, but I don't know why in production it gives an error.
This is error message which I get from the server
Error message (I'm not sure which line is causing the error):
error: ReferenceError: ReadableStream is not defined
at getReadableFromProtocolStream (/var/www/max-scaffolds/backend/node_modules/puppeteer-core/lib/cjs/puppeteer/common/util.js:241:5)
at CdpPage.createPDFStream (/var/www/max-scaffolds/backend/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Page.js:845:66)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async CdpPage.pdf (/var/www/max-scaffolds/backend/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Page.js:849:26)
You have to check 3 main paramter in Local and Live
Above three parameter should match than and only than above issue is resolve In my case i have done
Puppeteer version :- 22.4.1
Node version :- 22.11.0
Chromium version :- set according to your live and local