Azure函数应用程序jsreport上的HTTP触发器无法正常工作

时间:2017-07-17 19:50:56

标签: javascript node.js pdf azure-functions jsreport

我正在尝试让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核心调用此函数。谢谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您的renderPdf函数会返回一个您没有正确使用的承诺。您不能将保证分配给结果正文,而是在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();
}