Convert and download html to pdf without saving file

node js
convert html to pdf
pdf
download pdf
without saving

Below is the module for convert html to pdf file "html-pdf", 

Install the html-pdf

$ npm install -g html-pdf

example for convert html to pdf 

var pdf = require('html-pdf');
var options = { format: 'Letter' };
 
pdf.create('<h1>Welcome</h1>', options).toFile('./sample.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res);
});

Below is the api code for download file without saving,

router.get('/downloadfile', function(req, res, next) {
  pdf.create("<h1 style='text-align:center'>Hi Welcome</h1><br><img src='https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'>").toStream(function (err, stream) {
    if (err) {
        console.log(err);
    }
    res.writeHead(200, {
        'Content-Type': 'application/force-download',
        'Content-disposition': 'attachment; filename=file.pdf'
    });
    stream.pipe(res);
  });
});

Basically, it will not work in ajax calls, it will work only when page load, from the client side we need to call the API,  like 

window.open('https://www.domain.com//downloadfile','download');

 

You might also like:

Simple Array Sum | Javascript | hackerrank.com

07-05-2020 hackerrank.com javascript arrays array sum

File Upload Using Angular Js and Node Js

28-05-2018 AngularJs Node js file upload multer module

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 replace text in Angular Js

05-03-2017 filter replace angular js