Receiving error: html-pdf: PDF generation timeout. Phantom.js script did not exit. within Firebase Cloud Functions

Receiving error: html-pdf: PDF generation timeout. Phantom.js script did not exit. within Firebase Cloud Functions

我正在构建一个使用 html-pdf 包(使用 PhantomJS)的 firebase 函数。该函数在我的本地机器上运行良好,但每当我在 Firebase 上部署该函数时,我都会收到以下错误:

Error: html-pdf: PDF generation timeout. Phantom.js script did not exit.

我已经更改了 pdf.create() 的超时参数并继续得到相同的结果。关于什么可能导致此问题的任何想法,只有在将其部署到 Firebase 时才会出现?代码如下。

const pdf = require('html-pdf');
const runtimeOpts = {
    timeoutSeconds: 540,  // in seconds
    memory: '2GB'
  }

exports.sendToKindle = functions.runWith(runtimeOpts).https.onRequest(async (req, res) => {
   // REMOVED A BLOCK OF CODE FOR SIMPLICITY, BUT CAN PUT BACK IN IF NEEDED//
   var options = { 
       format: 'Letter',
       directory: "/tmp", 
       timeout: 540000,  // in milliseconds
   };
    
   const blookFileName = createFileName(blookData.title) + '.pdf';
    
   const tempFilePath = path.join(os.tmpdir(), `${blookFileName}`);

   const htmlFilePath = path.join(os.tmpdir(), 'book.html');

   const htmlFs = fs.openSync(htmlFilePath, 'w');

   await fs.promises.appendFile(htmlFilePath, bookHTML);

   const fd = fs.openSync(tempFilePath, 'w');

   var html = fs.readFileSync(htmlFilePath, 'utf8');

   let mailgunObject = null;

   pdf.create(html, options).toFile(tempFilePath, async (err, res) => {
           if (err) return console.error(err);
           mailgunObject = await sendEmail(tempFilePath, kindleEmail);
           return console.log(res);
       });

   fs.closeSync(fd);
   fs.closeSync(htmlFs);

   return cors(req, res, () => {
        res.status(200).type('application/json').send({'response': 'Success'})
    })

我能够通过将 pdf.create().toFile() 放置在云函数的 return 中来修改代码来解决这个问题。

const pdf = require('html-pdf');
const runtimeOpts = {
    timeoutSeconds: 300,  // in seconds
    memory: '1GB'
  }

exports.sendToKindle = functions.runWith(runtimeOpts).https.onRequest(async (req, res) => {
   // REMOVED A BLOCK OF CODE FOR SIMPLICITY, BUT CAN PUT BACK IN IF NEEDED//
   var options = { 
       format: 'Letter',
       directory: "/tmp", 
       timeout: 540000,  // in milliseconds
   };
    
   const blookFileName = createFileName(blookData.title) + '.pdf';
    
   const tempFilePath = path.join(os.tmpdir(), `${blookFileName}`);

   const htmlFilePath = path.join(os.tmpdir(), 'book.html');

   const htmlFs = fs.openSync(htmlFilePath, 'w');

   await fs.promises.appendFile(htmlFilePath, bookHTML);

   const fd = fs.openSync(tempFilePath, 'w');

   var html = fs.readFileSync(htmlFilePath, 'utf8');

   return cors(req, res, () => {
        pdf.create(html, options).toFile(tempFilePath, async (err, res) => {
           if (err) return console.error(err);
           let mailgunObject = await sendEmail(tempFilePath, kindleEmail);
           fs.closeSync(fd);
           fs.closeSync(htmlFs);
           return console.log(res);
        });

        res.status(200).type('application/json').send({'response': 'Success'})
    })

我遇到了同样的问题。实际上我意识到,当我通过 Postman 使用 html-pdf 调用函数时,或者只是通过 Google Chrome 的请求调用函数时,pdf 用于在 2 或 3 秒内生成,而直接从我的应用程序调用它需要 2 或 3 分钟。 所以这就是我所做的:将 html-pdf 放入我部署的单独函数中,然后调用它:

https = require('https'); 
https.get(https://us-central1-your-project-name.cloudfunctions.net/your-function-using-html-pdf)