javascript - Azure Function App jsreport on HTTP 触发器不起作用

标签 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 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/

相关文章:

javascript - 如何使用nodejs使用子进程运行命令?

node.js - Sails Js 权限

node.js - Sails.js 是从商店中清除的 session

node.js - 如何使用 Node SDK 制作用于接受付款的 Paypal 按钮?

javascript - 在pdf中使用javascript获取用户名

javascript - 立即将 eventListener 添加到页面 TypeScript

javascript - Java : extract a value from a string based on delimiters

javascript - 如何在 Gatsby.js 中将时间戳附加到 markdown 前面的内容

android - Mozilla PDF 在三星设备上速度极慢

java - pdfbox - 转换为灰度后 pdf 增加大小