How to convert html to pdf in Node js

12-03-2017 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:

Use java script file client side and server side at a time in node js
30-07-2017 node js include call javascript
Sanitization in java sript
14-03-2017 sanitization java script
How to make an http request in node js
14-03-2017 http module node js
Convert Time 24 hrs to 12 hrs format In javascript
12-03-2017 date javascript 12hrs convert
How to find Angular Errors
06-03-2017 errors angularjs exceptionHandler