javascriptnode.jsazure-functionshtml-pdf

Node return byte array after turning html to pdf


I am using the 'html-pdf' npm package to create a byte array in a nodeJs azure function. I can log and see that the byte array is being created but when i call my function i cannot get the buffer to return. My response is a 200 and again i can see the buffer if i log it to string. For context the package takes an html string and returns a pdf .How do i set the response to return the byte array?

var pdf = require('html-pdf');

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    if ((req.body && req.body.data)) {
        var html = req.body.data;
        context.log(html);
        console.log(html);
        var data = []
         pdf.create(html).toBuffer(function(err, buffer){
            data.push(buffer);
            context.res = {
                setEncoding: 'binary',
                // status: 200, /* Defaults to 200 */
                body: Buffer.concat(data)
            };
          });
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

Solution

  • If anyone needs to figure this out in the future you have to create a new promise from pdf.create. My code now looks like this. This doesnt work in an azure function apparently due to GDI support for phantom js

    UPDATE this now works if deployed to an app service plan. Will not work in consumption plan in azure.

    var pdf = require('html-pdf');
    
    
    module.exports = async function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
        const html=req.body.data;
        var data = await returnHtmlAsPdf(html);
        var data2 = []
        data2.push(data);
        context.res = {
            setEncoding: 'binary',
            // status: 200, /* Defaults to 200 */
            body: Buffer.concat(data2)
        };
        context.done();
    };
    
    async function returnHtmlAsPdf(html) {
        return new Promise((resolve, reject) => {
            pdf.create(html).toBuffer(function(err, buffer){
                if(err){
                    reject(err);
                }
                resolve(buffer);
            })
        });
    
    }