0

I am trying to generate pdf using 'html-pdf' npm module. After generating pdf, in response I get pdf file path which I want to return with response of graphql call (earlier was planning to return pdf file which I realized is not a standard way). But the below code always return null though I get pdf file path in console.log

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

const resolvers = {
    Query: {        
        to_pdf(parent, args, context, info){
            console.log('Creating pdf file ', args.html);
            var options = { format: 'A4' }

            pdfLib.create(args.html, options).toFile('./myfile.pdf', callback)
        },

    },

};

async function callback(err, res) {
    if (err) return console.log(err);
    console.log(res.filename); // prints filename in console
    return res;
};            

module.exports = {
    resolvers,
}
  • 1
    A resolver should return a value or a Promise that resolves to some value. Your resolver does neither. If you have to utilize a callback, you should wrap it with a Promise and return the promise. See Common Scenario #6 [here](https://stackoverflow.com/a/56319138/6024220). – Daniel Rearden Sep 30 '19 at 16:19
  • Thanks @DanielRearden I got my answer in that thread at the end. html-pdf lib was having callback functions only so this helped "If you absolutely have to use a callback, you can also wrap the callback in a Promise:". – Rahul Kulshreshtha Sep 30 '19 at 16:49

0 Answers0