0

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

4

1 回答 1

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();
}
于 2017-07-17T20:15:48.867 回答