简体   繁体   中英

html-pdf and Javascript

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM