Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the rendered view as a string in express.js?

I want to store the rendered view res.render as string to a variable and use node-html-pdf package to print pdf:

var pdf = require('html-pdf');
var html = res.render('my_view', res.view_data); // not work

pdf.create(html).toStream(function(err, stream){
    stream.pipe(fs.createWriteStream('./foo.pdf'));
});

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’ll need the app instance of express application.

var appInstance = express();
appInstance.set('views', path.join(__dirname, 'views')); // Set this relative to your file
appInstance.set('view engine', 'pug'); // Or whatever your view engine is

appInstance.render("my_view", view_data, (err, html) => {

  pdf.create(html).toStream(function(err, stream){
    stream.pipe(fs.createWriteStream('./foo.pdf'));
  });

});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading