I was using puppeteer for generating pdf from html. It worked as expected in local machine. But on deploying to AWS lambda, we found that it will exceed the size limit of 250MB set by AWS for lambda functions. Then we got to know about chrome-aws-lambda which can be used for deploying in AWS. The code to generate the pdf was almost exactly the same across multiple docs available online. But when deployed we are always getting an empty pdf. But the pdf file has a size too. How can we resolve this blank pdf issue?
Steps that were followed for the pdf generation process.
npm install chrome-aws-lambda
Code used to generate the pdf:
module.exports.generateReport = async (req,res,next)=>{
const browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
const loaded = page.waitForNavigation({waitUntil:'load'});
await page.setContent('<h1>Hello world!</h1>');
await loaded;
const pdfFile = await page.pdf({ format: "Letter"});
res.set({ 'Content-Type': 'application/pdf', 'Content-Length': pdfFile.length })
return res.send(pdfFile)
}
Another set of code that was tried:
module.exports.generateReport = async (req,res,next)=>{
const browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.setContent('<h1>Hello world!</h1>',{ waitUntil: ['load','domcontentloaded', 'networkidle0'] });
const pdfFile = await page.pdf({ format: "Letter"});
res.set({ 'Content-Type': 'application/pdf', 'Content-Length': pdfFile.length })
return res.send(pdfFile)
}