我正在尝试使 jsreport 在 Azure Function 应用程序中工作。我已经安装了所需的所有软件包,它们是 jsreport-core jsreport-render jsreport-phantom-js ,它们似乎都工作得很好。我的代码:
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.content || (req.body && req.body.content)) {
var pdf = renderPdf(req.query.content || req.body.content, {})
context.res = {
status: 200,
body: { data: pdf }
};
}
else {
context.res = {
status: 400,
body: "Please pass the content on the query string or in the request body"
};
}
context.done();};
function renderPdf(content, data){
var jsreport = require('jsreport-core')();
var promise = jsreport.init().then(function () {
return jsreport.render({
template: {
content: content,
engine: 'jsrender',
recipe: 'phantom-pdf'
},
data: data
});
});
return Promise.resolve(promise);}
我使用这篇文章作为示例:Export html to pdf in ASP.NET Core
我的最终目标是从asp.net core调用这个函数。谢谢您的帮助。
最佳答案
您的 renderPdf 函数返回一个 promise ,您没有正确使用它。您不能只将 Promise 分配给结果主体,而是在 then
中分配主体:
if (req.query.content || (req.body && req.body.content)) {
renderPdf(...).then(pdf => {
context.res = {
status: 200,
body: { data: pdf }
};
context.done();
});
}
else {
context.res = {
status: 400,
body: "Please pass the content on the query string or in the request body"
};
context.done();
}
关于javascript - Azure Function App jsreport on HTTP 触发器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45152520/