3

我正在尝试在 phantomjs 示例中创建自定义页脚:https ://github.com/ariya/phantomjs/blob/master/examples/printheaderfooter.js

这是我的代码:

var phantom = require('node-phantom');

phantom.create(function (err, ph) {
    ph.createPage(function (err, page) {
         page.set('paperSize', {
              format: 'A4',
              orientation: 'portrait',
              footer: {
                contents: ph.callback(function (pageNum, numPages) {
                  if (pageNum == 1) {
                    return "";
                  }
                  return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
                })
              }
         }, function () {
             page.open('http://www.google.com', function () {
              })
         })
    })
});

但不幸的是,我收到以下错误:

TypeError: Object #<Object> has no method 'callback';

ph 不公开回调方法是错误吗?

4

4 回答 4

6

您的脚本中有两个问题:

  • ph不是经典的幻象对象,而是代理对象。node-phantom使用网络套接字调用 phantomjs。当然,使用此实现会丢失一些功能。
  • 调用时函数未序列化page.set

打印自定义页眉/页脚也需要调用 phantom.callback。此方法未记录在案,因此未公开node-phantom(也不能公开)。我们需要找到一种方法在这个包中应用这个方法。

有很多解决方案。这是我可能的解决方案:

在脚本中将函数序列化为字符串

var phantom = require('node-phantom');

phantom.create(function (err, ph) {
    ph.createPage(function (err, page) {
         page.set('paperSize', {
              format: 'A4',
              orientation: 'portrait',
              header: {
                            height: "1cm",
                            contents: 'function(pageNum, numPages) { return pageNum + "/" + numPages; }'
                        },
                        footer: {
                            height: "1cm",
                            contents: 'function(pageNum, numPages) { return pageNum + "/" + numPages; }'
                        }
         }, function () {   
             page.open('http://www.google.fr', function () {        
             page.render('google.pdf');
             ph.exit();
              })
         })
    })
});

编辑 bridge.js 并添加 phantom.callback + eval。这允许我们重新插入页眉/页脚 .contents。

case 'pageSet':
            eval('request[4].header.contents = phantom.callback('+request[4].header.contents+')');
            eval('request[4].footer.contents = phantom.callback('+request[4].footer.contents+')');
            page[request[3]]=request[4];
            respond([id,cmdId,'pageSetDone']);
            break;

如您所见,这很有效!(谷歌法语)

在此处输入图像描述

于 2013-06-17T19:45:59.157 回答
1

不幸的是,node-phantom似乎不支持phantom.callback. 由于该项目一年多没有活动,我认为它不太可能在不久的将来更新。

另一方面,phantomjs-node从0.6.6phantom.callback()版本开始支持。你可以像这样使用它:

var phantom = require('phantom');

phantom.create(function (ph) {
    ph.createPage(function (page) {
        page.open("http://www.google.com", function (status) {

            var paperConfig = {
                format: 'A4',
                orientation: 'portrait',
                border: '1cm',
                header: {
                    height: '1cm',
                    contents: ph.callback(function(pageNum, numPages) {
                        return '<h1>My Custom Header</h1>';
                    })
                },
                footer: {
                    height: '1cm',
                    contents: ph.callback(function(pageNum, numPages) {
                        return '<p>Page ' + pageNum + ' / ' + numPages + '</p>';
                    })
                }
            };

            page.set('paperSize', paperConfig, function() {
                // render to pdf
                page.render('path/to/file.pdf', function() {
                    page.close();
                    ph.exit();
                });
            });
        });
    });
});

正如你也可以在这个要点上看到的。

于 2014-11-19T23:59:27.603 回答
0

节点幻象似乎通过 create 函数公开了这个代理对象(这应该是你的 ph 对象):

var proxy={
                createPage:function(callback){
                    request(socket,[0,'createPage'],callbackOrDummy(callback));
                },
                injectJs:function(filename,callback){
                    request(socket,[0,'injectJs',filename],callbackOrDummy(callback));
                },
                addCookie: function(cookie, callback){
                    request(socket,[0,'addCookie', cookie],callbackOrDummy(callback));
                },
                exit:function(callback){
                    request(socket,[0,'exit'],callbackOrDummy(callback));
                },
                on: function(){
                    phantom.on.apply(phantom, arguments);
                },
                _phantom: phantom
            };

这意味着,您可能可以像这样访问幻影回调:

ph._phantom.callback
于 2013-06-14T08:28:56.853 回答
0

这是我为访问 phantom.callback 所做的操作:

将此添加到 node-phantom.js 第 202 行:

callback: function(callback){
  request(socket,[0,'callback'],callbackOrDummy(callback));
},

就在之前_phantom: phantom

并将其添加到 bridge.js 第 45 行:

case 'callback':
    phantom.callback(request[3]);
break;

希望能帮助到你!

于 2013-06-17T14:02:00.247 回答