【问题标题】:Node.js - How to export view to pdfNode.js - 如何将视图导出为 pdf
【发布时间】:2017-06-26 13:46:13
【问题描述】:

我有一个需要导出为 pdf 的视图,但要显示此视图,我需要将参数发送到特定路由,但我不知道如何调用路由而不是 html 文件。

我的路线:

viewBicsDetails(req, res)
  {
    var ref = req.params.stamp;

    var info = request.query("query");

    var compounds = request.query("query");

    return Promise.all([info, compounds]).then(function(results)
    {
      console.log(results[0]);
      res.render('management-bic-details', {info : results[0], compounds: results[1]});
    });}

如何将此渲染导出为 pdf?我安装了html-pdf npm 库。 https://www.npmjs.com/package/html-pdf

 var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' } 
});

readFileSync 我应该把路线.. 但是如何? 谢谢

【问题讨论】:

    标签: javascript node.js express pdf html-to-pdf


    【解决方案1】:

    解决方案 1,带有 HTML 和 PDF 模板

    制作pdf模板并用javascript代码填充它。 HTML 和 PDF 模板会有所不同!

    有很多具有此功能的软件包。例如pdffiller

    解决方案 2,使用单个 HTML 模板

    在后台html-pdf 实现了这种方法。

    1. 填充 HTML 模板。
    2. 通过phantomjs 或任何无头浏览器渲染图像。
    3. 将图像编译为单个 pdf 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多