简体   繁体   中英

Puppeteer 504 Gateway Time-out error in production

I have implemented pdf generation using puppeteer

const puppeteer = require("puppeteer");  
module.exports = {
   async generate(req, res) {
   let url = req.query.url;
   let name = req.query.name ? req.query.name : "file";
   const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
   const page = await browser.newPage();
   await page.goto(url, { waitUntil: "networkidle0" });
   const pdf = await page.pdf({ format: "A4" });
            
   await browser.close();
            
   res.set({
      "Content-Type": "application/pdf",
       "Content-Length": pdf.length,
     });
   res.setHeader("Content-disposition", `attachment; filename=${name}.pdf`);
       res.send(pdf);
   },
};

    

The above code is running fine when I try this in localhost and it generates the PDF but when I am moving this to production then it keeps loading and gives 504 Gateway error ex: https://api.socialtools.me/core/html-to-pdf?url=https://apps.socialtools.me/preview/discount-coupon/613e0c6a9115bb003681958d/print/0&name=discount-coupon .

Can anyone, please help me to solve this issue.

For me on Linux, the only way to resolve is by installing some dependencies:

sudo apt-get update
sudo apt-get install -y gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
sudo apt-get install -y libgbm-dev

reference:

https://github.com/puppeteer/puppeteer/issues/5662?fbclid=IwAR17HxYebtc14UvptAAUUk7qd4oMTysm-Uasv7KBm1LN7aN2H7GAkj6IxIU#issuecomment-732076246

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM