【问题标题】:How can I render html content as pdf file using phantomJs in node.JS如何在 node.JS 中使用 phantomJs 将 html 内容呈现为 pdf 文件
【发布时间】:2016-09-10 11:02:29
【问题描述】:

如何生成包含一些 html 内容的 pdf 文件,并使用 node.js 中的 phantom.js 将文件和内容呈现给浏览器?

【问题讨论】:

    标签: html node.js pdf phantomjs


    【解决方案1】:

    为什么不使用使用 phantomJS 的 html-pdf npm 模块?

    代码示例

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

    【讨论】:

    • 我可以通过var html = fs.readFileSync('http://localhost:3000/product/generate-invoice', 'utf8');这样的网址生成pdf报告吗?
    • 每个 pdf 结尾处的字符都被剪掉了。这是一个动态的pdf。热来解决它。
    猜你喜欢
    • 2013-03-09
    • 2013-06-06
    • 2018-05-21
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多