0

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

Same question was posted on stackoverflow but no answer : Puppeteer Protocol error (Runtime.callFunctionOn): Target closed

  • I am using t2.micro instance type and storage volume is 8 Gib. From the docs it seems the memory (ram) of t2.micro instance type is 1 GB – Prashansa Shukla Nov 30 '22 at 15:12
  • Try with a larger instance. A t2.micro has 1 GB RAM. Operating system/etc. will take a good chunk of that, and even though the output PDF is only 7 MB, between input cacheing, manipulation, etc. could easily require a lot more during processing. – manassehkatz-Moving 2 Codidact Nov 30 '22 at 15:15
  • Look at the memory required locally when it runs to get an idea of what you need. – Kevin Brown Nov 30 '22 at 16:58
  • 1
    For someone who is facing similar issue and looking for an answer, I managed to solve the issue by using aws-lambda. Aws lambda allows up to 10 GB (3GB in most of the regions) of memory. My memory usage was around 2-2.5 GB. That is the reason I was facing the issue in EC2 1 GB instance. Thank you @manassehkatz-Moving2Codidact and Kevin Brown – Prashansa Shukla Dec 03 '22 at 04:43

0 Answers0