【问题标题】:PDF generation with PhantomJS in node-Webkit在 node-Webkit 中使用 PhantomJS 生成 PDF
【发布时间】:2014-09-29 12:42:00
【问题描述】:

我在这个主题node wkhtmltopdf create corrupted PDF in node webkit 中发现了一条评论,这表明可以使用 PhantomJS 尤其是使用此脚本从 node-webkit 中的 html 生成 pdf:https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js

但是我不明白如何在没有命令行调用的情况下使用这个脚本......

【问题讨论】:

    标签: node.js pdf phantomjs node-webkit


    【解决方案1】:

    无法直接在 node.js 中按原样使用脚本。您可以使用 child_process 模块调用 phantomjs,本质上是作为带有 rasterize.js 脚本和选项的命令行脚本。

    另一种可能性是使用node.js 的幻像包装器直接包含rasterize.js 的代码。您只需要进行一些小的调整,例如 page 参数是从包装器传递的,不需要创建。可能的包装器是node-phantomphantomjs-node。如果你用 node-webkit 打包你的应用程序,那么你可能会遇到 phantomjs 可执行文件的路径问题。

    【讨论】:

      【解决方案2】:

      Phantomjs 的 Rasterize.js 可以很好地生成干净的多页可编辑 pdf,并保留所有棘手的 CSS。尝试在 nodejs 环境中使用它时我有点困惑,但它非常简单。

      根据NPM Phantomjs readme:(我已经更明确地说明了一点)

      var path = require('path')
      var childProcess = require('child_process')
      var phantomjs = require('phantomjs')
      var binPath = phantomjs.path
      
      //Args for rasterize.js: [ rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]') ]
      
      var childArgs = [ path.join(__dirname, 'rasterize.js'),'url','docname.pdf','A4',1.00]
      
      childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
        // handle results
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        • 2013-10-30
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        相关资源
        最近更新 更多