Node.js 接收错误:html pdf:pdf生成超时。Phantom.js脚本未退出。在Firebase云函数中

Node.js 接收错误:html pdf:pdf生成超时。Phantom.js脚本未退出。在Firebase云函数中,node.js,firebase,google-cloud-functions,phantomjs,node-html-pdf,Node.js,Firebase,Google Cloud Functions,Phantomjs,Node Html Pdf,我正在构建一个使用html pdf包(使用PhantomJS)的firebase函数。该函数在本地计算机上运行良好,但每当我在Firebase上部署该函数时,都会出现以下错误: 错误:html pdf:pdf生成超时。Phantom.js脚本未退出 我已经更改了pdf.create()的超时参数,并继续得到相同的结果。你知道是什么原因造成了这个问题吗?只有当一个团队将此部署到Firebase时,这个问题才是唯一的?代码如下 const pdf=require('html-pdf'); 常量运行时

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

错误:html pdf:pdf生成超时。Phantom.js脚本未退出

我已经更改了
pdf.create()
的超时参数,并继续得到相同的结果。你知道是什么原因造成了这个问题吗?只有当一个团队将此部署到Firebase时,这个问题才是唯一的?代码如下

const pdf=require('html-pdf');
常量运行时选项={
timeoutSeconds:540,//以秒为单位
内存:“2GB”
}
exports.sendToKindle=functions.runWith(runtimeOpts).https.onRequest(async(req,res)=>{
//为简单起见,删除了一段代码,但如果需要,可以重新插入//
变量选项={
格式:'字母',
目录:“/tmp”,
超时:540000,//以毫秒为单位
};
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');
等待fs.promises.appendFile(htmlFilePath,bookHTML);
const fd=fs.openSync(tempFilePath,'w');
var html=fs.readFileSync(htmlFilePath,'utf8');
让mailgunObject=null;
create(html,options).toFile(tempFilePath,async(err,res)=>{
if(err)返回控制台。error(err);
mailgunObject=等待发送电子邮件(tempFilePath,kindleEmail);
返回控制台日志(res);
});
fs.closeSync(fd);
fs.closeSync(htmlFs);
返回cors(请求、回复,()=>{
res.status(200).type('application/json').send({'response':'Success'})
})

我通过将pdf.create().toFile()放在云函数的返回中来修改代码,从而解决了这个问题

const pdf=require('html-pdf');
常量运行时选项={
timeoutSeconds:300,//以秒为单位
内存:“1GB”
}
exports.sendToKindle=functions.runWith(runtimeOpts).https.onRequest(async(req,res)=>{
//为简单起见,删除了一段代码,但如果需要,可以重新插入//
变量选项={
格式:'字母',
目录:“/tmp”,
超时:540000,//以毫秒为单位
};
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');
等待fs.promises.appendFile(htmlFilePath,bookHTML);
const fd=fs.openSync(tempFilePath,'w');
var html=fs.readFileSync(htmlFilePath,'utf8');
返回cors(请求、回复,()=>{
create(html,options).toFile(tempFilePath,async(err,res)=>{
if(err)返回控制台。error(err);
让mailgunObject=等待发送电子邮件(tempFilePath,kindleEmail);
fs.closeSync(fd);
fs.closeSync(htmlFs);
返回控制台日志(res);
});
res.status(200).type('application/json').send({'response':'Success'})
})

我通过将pdf.create().toFile()放在云函数的返回中来修改代码,从而解决了这个问题

const pdf=require('html-pdf');
常量运行时选项={
timeoutSeconds:300,//以秒为单位
内存:“1GB”
}
exports.sendToKindle=functions.runWith(runtimeOpts).https.onRequest(async(req,res)=>{
//为简单起见,删除了一段代码,但如果需要,可以重新插入//
变量选项={
格式:'字母',
目录:“/tmp”,
超时:540000,//以毫秒为单位
};
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');
等待fs.promises.appendFile(htmlFilePath,bookHTML);
const fd=fs.openSync(tempFilePath,'w');
var html=fs.readFileSync(htmlFilePath,'utf8');
返回cors(请求、回复,()=>{
create(html,options).toFile(tempFilePath,async(err,res)=>{
if(err)返回控制台。error(err);
让mailgunObject=等待发送电子邮件(tempFilePath,kindleEmail);
fs.closeSync(fd);
fs.closeSync(htmlFs);
返回控制台日志(res);
});
res.status(200).type('application/json').send({'response':'Success'})
})

我也遇到了同样的问题。事实上,我意识到,当我通过邮递员使用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)

我也遇到了同样的问题。事实上,我意识到,当我通过邮递员使用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)

您的代码没有等待
create().toFile())
完成。它几乎肯定是异步的,因为它接受回调。@DougStevenson,这个问题不也会反映在本地firebase emulator上吗?这只是我将其部署到firebase时的问题。不,emulator不会在返回请求时关闭它。这不是100%准确的模拟@DougStevenson,好吧,这很有道理。解决这个问题的最好方法是什么?我不能让
返回cors(