0

I'm trying to make jsreport work in a Azure Function app. I've already installed all the packages needed, which are jsreport-core jsreport-render jsreport-phantom-js and they all seem to be working just fine. My code:

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);}

I'm using this post as an example: Export html to pdf in ASP.NET Core

My final goal is to call this function from asp.net core. Thanks for the help.

Andoni Zubizarreta
  • 1,275
  • 1
  • 15
  • 23

1 Answers1

2

Your renderPdf function returns a promise, which you are not consuming correctly. You can't just assign the promise to result body, instead assign the body in 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();
}
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107