Skip to main content
Post improved.
Source Link
Reporter
  • 3.9k
  • 5
  • 36
  • 50

I just made some changes to your code.In this code I create a route.Whenever Whenever you made a request with this route it converts HTML file to PDF and creating a pdf file into your directory from where you are executing.And And at the sametime it displays the html file in browser with downloading option also.Hope Hope this helps for you.And And here is my code.

var express=require('express'); var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('C:/Users/nodejs/tasks/file.html', 'utf8'); var options = { format: 'Letter' }; var app=express(); app.get('/file',function(request,response) {  pdf.create(html, options).toFile('./businesscaw.pdf', function(err, res) {  if (err) return console.log(err);  console.log(res);  var file= 'C:/Users/nodejs/tasks/businesscaw.pdf';  fs.readFile(file,function(err,data){   response.contentType("application/pdf");   response.send(data);  });  }); }); app.listen(3000,function(){  console.log("Server listening on port http://loalhost:3000"); }); OUTPUT: 

SeeOutput: See the output in browser like : localhost:3000/file

I just made some changes to your code.In this code I create a route.Whenever you made a request with this route it converts HTML file to PDF and creating a pdf file into your directory from where you are executing.And at the sametime it displays the html file in browser with downloading option also.Hope this helps for you.And here is my code.

var express=require('express'); var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('C:/Users/nodejs/tasks/file.html', 'utf8'); var options = { format: 'Letter' }; var app=express(); app.get('/file',function(request,response) { pdf.create(html, options).toFile('./businesscaw.pdf', function(err, res) { if (err) return console.log(err); console.log(res); var file= 'C:/Users/nodejs/tasks/businesscaw.pdf'; fs.readFile(file,function(err,data){ response.contentType("application/pdf"); response.send(data); }); }); }); app.listen(3000,function(){ console.log("Server listening on port http://loalhost:3000"); }); OUTPUT: 

See the output in browser like : localhost:3000/file

I just made some changes to your code.In this code I create a route. Whenever you made a request with this route it converts HTML file to PDF and creating a pdf file into your directory from where you are executing. And at the sametime it displays the html file in browser with downloading option also. Hope this helps for you. And here is my code.

var express=require('express'); var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('C:/Users/nodejs/tasks/file.html', 'utf8'); var options = { format: 'Letter' }; var app=express(); app.get('/file',function(request,response) {  pdf.create(html, options).toFile('./businesscaw.pdf', function(err, res) {  if (err) return console.log(err);  console.log(res);  var file= 'C:/Users/nodejs/tasks/businesscaw.pdf';  fs.readFile(file,function(err,data){   response.contentType("application/pdf");   response.send(data);  });  }); }); app.listen(3000,function(){  console.log("Server listening on port http://loalhost:3000"); }); 

Output: See the output in browser like : localhost:3000/file

deleted 31 characters in body
Source Link
Syed Ayesha Bebe
  • 1.5k
  • 1
  • 17
  • 26

I just made some changes to your code.In this code I create a route.Whenever you made a request with this route it converts HTML file to PDF and creating a pdf file into your directory from where you are executing.And at the sametime it displays the html file in browser with downloading option also.Hope this helps for you.And here is my code.

var express=require('express'); var path=require('path'); var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('C:/Users/nodejs/tasks/file.html', 'utf8'); var options = { format: 'Letter' }; var app=express(); app.get('/file',function(request,response) { pdf.create(html, options).toFile('./businesscaw.pdf', function(err, res) { if (err) return console.log(err); console.log(res); var file= 'C:/Users/nodejs/tasks/businesscaw.pdf'; fs.readFile(file,function(err,data){ response.contentType("application/pdf"); response.send(data); }); }); }); app.listen(3000,function(){ console.log("Server listening on port http://loalhost:3000"); }); OUTPUT: 

See the output in browser like : localhost:3000/file

I just made some changes to your code.In this code I create a route.Whenever you made a request with this route it converts HTML file to PDF and creating a pdf file into your directory from where you are executing.And at the sametime it displays the html file in browser with downloading option also.Hope this helps for you.And here is my code.

var express=require('express'); var path=require('path'); var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('C:/Users/nodejs/tasks/file.html', 'utf8'); var options = { format: 'Letter' }; var app=express(); app.get('/file',function(request,response) { pdf.create(html, options).toFile('./businesscaw.pdf', function(err, res) { if (err) return console.log(err); console.log(res); var file= 'C:/Users/nodejs/tasks/businesscaw.pdf'; fs.readFile(file,function(err,data){ response.contentType("application/pdf"); response.send(data); }); }); }); app.listen(3000,function(){ console.log("Server listening on port http://loalhost:3000"); }); OUTPUT: 

See the output in browser like : localhost:3000/file

I just made some changes to your code.In this code I create a route.Whenever you made a request with this route it converts HTML file to PDF and creating a pdf file into your directory from where you are executing.And at the sametime it displays the html file in browser with downloading option also.Hope this helps for you.And here is my code.

var express=require('express'); var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('C:/Users/nodejs/tasks/file.html', 'utf8'); var options = { format: 'Letter' }; var app=express(); app.get('/file',function(request,response) { pdf.create(html, options).toFile('./businesscaw.pdf', function(err, res) { if (err) return console.log(err); console.log(res); var file= 'C:/Users/nodejs/tasks/businesscaw.pdf'; fs.readFile(file,function(err,data){ response.contentType("application/pdf"); response.send(data); }); }); }); app.listen(3000,function(){ console.log("Server listening on port http://loalhost:3000"); }); OUTPUT: 

See the output in browser like : localhost:3000/file

Source Link
Syed Ayesha Bebe
  • 1.5k
  • 1
  • 17
  • 26

I just made some changes to your code.In this code I create a route.Whenever you made a request with this route it converts HTML file to PDF and creating a pdf file into your directory from where you are executing.And at the sametime it displays the html file in browser with downloading option also.Hope this helps for you.And here is my code.

var express=require('express'); var path=require('path'); var fs = require('fs'); var pdf = require('html-pdf'); var html = fs.readFileSync('C:/Users/nodejs/tasks/file.html', 'utf8'); var options = { format: 'Letter' }; var app=express(); app.get('/file',function(request,response) { pdf.create(html, options).toFile('./businesscaw.pdf', function(err, res) { if (err) return console.log(err); console.log(res); var file= 'C:/Users/nodejs/tasks/businesscaw.pdf'; fs.readFile(file,function(err,data){ response.contentType("application/pdf"); response.send(data); }); }); }); app.listen(3000,function(){ console.log("Server listening on port http://loalhost:3000"); }); OUTPUT: 

See the output in browser like : localhost:3000/file