如何解决接收错误:html-pdf:PDF生成超时 Phantom.js脚本未退出在Firebase Cloud Functions中
我正在构建一个使用html-pdf包(使用PhantomJS)的firebase函数。该功能在我的本地计算机上运行良好,但是每当我在Firebase上部署该功能时,都会出现以下错误:
我更改了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()放在云函数的返回位置来修改代码来解决此问题。
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 秒内生成,而直接从我的应用程序。 所以这就是我所做的:将 html-pdf 放在我部署的单独函数中,然后调用它:
https = require('https');
https.get(https://us-central1-your-project-name.cloudfunctions.net/your-function-using-html-pdf)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。