簡體   English   中英

生產中的 Puppeteer 504 網關超時錯誤

[英]Puppeteer 504 Gateway Time-out error in production

我已經使用 puppeteer 實現了 pdf 生成

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);
   },
};

    

當我在 localhost 中嘗試此操作並生成 PDF 時,上面的代碼運行良好,但是當我將其移至生產環境時,它會繼續加載並給出 504 網關錯誤,例如: https ://api.socialtools.me/core/html- to-pdf?url=https://apps.socialtools.me/preview/discount-coupon/613e0c6a9115bb003681958d/print/0&name=discount-coupon

誰能幫我解決這個問題。

對於 Linux 上的我來說,解決的唯一方法是安裝一些依賴項:

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

參考:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM