I am trying to create a PDF from HTML (created from Handlebar). I am using NodeJs and Puppeteer. The resulting PDF has a size of 7MB around 500 pages. The code is working fine in my local but I am facing issues on cloud servers. I have tried with heroku free dyno (no longer available after 29th November 2022) and AWS EC2 server.
In heroku I was getting below error
Error R15 (Memory quota vastly exceeded)
In AWS EC2 server:
if I use the flag --single-process
while trying to launch the puppeteer browser I am getting the below error:
ProtocolError: Protocol error (Runtime.callFunctionOn): Target closed.
at /home/ec2-user/revisor_template_engine/node_modules/puppeteer/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:329:24
at new Promise (<anonymous>)
at CDPSessionImpl.send (/home/ec2-user/revisor_template_engine/node_modules/puppeteer/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:325:16)
at ExecutionContext._ExecutionContext_evaluate (/home/ec2-user/revisor_template_engine/node_modules/puppeteer/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:211:46)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async ExecutionContext.evaluate (/home/ec2-user/revisor_template_engine/node_modules/puppeteer/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)
at async IsolatedWorld.setContent (/home/ec2-user/revisor_template_engine/node_modules/puppeteer/node_modules/puppeteer-core/lib/cjs/puppeteer/common/IsolatedWorld.js:217:9)
at async CDPPage.setContent (/home/ec2-user/revisor_template_engine/node_modules/puppeteer/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Page.js:996:9)
and if I don't use the --single-process
flag the code get stuck after page.setContent()
function.
Here is the code snippet :
Note: The HTML is generated from handlebars
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--no-first-run',
'--no-zygote',
'--single-process',
'--shm-size=2gb',
'--disable-features=site-per-process',
'--disable-features=IsolateOrigins',
'--disable-site-isolation-trials',
'--unlimited-storage',
'--force-gpu-mem-available-mb',
'--full-memory-crash-report'
],
headless: true,
timeout: 300000,
devtools: true
});
const page = await browser.newPage();
await page.setContent(html, {timeout: 300000, waitUntil: ['domcontentloaded', 'load', "networkidle0"]}); // This line throws error
let buffer = await page.pdf({...options, timeout: 300000});
The above code is working fine in my local but throwing error in cloud servers.
Puppeteer version used : 19.2.2
Node Version in AWS EC2 : v14.21.1
IF I TRY WITH SMALL SMALL HTML STRING (~300 kb) THE SAME CODE IS WORKING FINE IN AWS EC2
I have followed this link https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-on-aws-ec2-instance-running-amazon-linux
I have also tried
- https://github.com/puppeteer/puppeteer/issues/1175
- Puppeteer - Protocol error (Page.navigate): Target closed
- https://github.com/puppeteer/puppeteer/issues/3683
Same question was posted on stackoverflow but no answer : Puppeteer Protocol error (Runtime.callFunctionOn): Target closed