0

I have been trying to dynamically generate a PDF file from my web app based on Firebase.

I recently tried to use html-pdf node module to generate a pdf. But the implementation I wanted to do was to be able to generate the PDF file using a combination of Javascript and html-pdf.

So what I wanted to understand is , how can I call html-pdf's create functionality from my apps javascript file.

I have used browserify to create a bundle.js file for html-pdf node module.

If there is any other info that you need. Please let me know.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Ruhban Shah
  • 71
  • 1
  • 6

1 Answers1

0

You can do this by importing the html-pdf package into your file

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

In my case, I use EJS as my template engine so first, I have to render the file before sending it to the PDF creator. Then, through the pdf.create function, I send my rendered HTML with options and convert into a PDF file through the .toFile() function.

ejs.renderFile('example.ejs', {...}, (err, result) => {
        // render on success
        if (result) {
            pdf.create(result, options).toFile('./example.pdf', function(err, res) {
                if (err) return console.log(err);
            });
        }
        // render or error
        else {
            res.end('An error occurred');
        }
    });

Cheers!