如何在 express.js 中将渲染视图作为字符串获取?

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

我想将呈现的视图 res.render 作为字符串存储到变量并使用 node-html-pdf 包打印 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'));
});

您需要快速应用程序的应用程序实例。

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'));
  });

});