How to convert html to pdf in Node js

pdf
convert
html
node js

In node js there is an node module like html-to-pdf. Now install html-to-pdf module, command for install this module is 

npm install html-to-pdf --save

after installing this module, we need to include this module into our js file. below command for include this module is

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

Syntax for converting html file to pdf is,

htmlToPdf.convertHTMLFile('path/sample.html', 'path/sample.pdf',
    function (error, success) {
       if (error) {
            console.log('Something wrong.');
            console.log(error);
        } else {
            console.log('Successfully converted');
            console.log(success);
        }
    }
);

Now convert dynamic html content variable to pdf, 

var html = '<h1>Example for HTML to PDF</h1>';  
htmlToPdf.convertHTMLString(html, 'path/sample.pdf',
    function (error, success) {
        if (error) {
            console.log('Something Wrong');
            console.log(error);
        } else {
            console.log('Successfully converted!');
            console.log(success);
        }
    }
);

 

 

You might also like:

Functions in javascript

25-05-2018 functions javascript es5 es6 arrow functions named functions Closures Nested Functions Callbacks Arrow functions.

Get next date from given date in javascript

06-03-2017 date javascript next date

Filter for change Date format (MM-dd-yyyy) in Angular Js

05-03-2017 mm-dd-yyyy date formats angular js

How to set or update or push the values into Object in javacript

27-02-2017 javascript set update push object

How to remove last / value from url in javascript

27-02-2017 array javascript last value