简体   繁体   中英

Footer's contents don't seem to work

I'm trying create custom footers such in phantomjs examples: https://github.com/ariya/phantomjs/blob/master/examples/printheaderfooter.js

Here is my code:

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 () {
              })
         })
    })
});

But unfortunately I get the following error:

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

Is it bug that ph does not expose callback method?

There are two problems in your script :

  • ph is not the classic phantom object, but a proxy object. node-phantom use web sockets to invoke phantomjs. Of course, some features are lost using this implementation.
  • functions are not serialized when calling page.set

Printing custom header/footer also requires to call phantom.callback. This method is not documented and so not exposed by node-phantom (and can't be). We need to find a way to apply this method in this package.

There are many solutions. Here is my possible solution :

Serialize your functions in a string in your script

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

edit bridge.js and add phantom.callback + eval. This allow us to re-plug the header/footer .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;

As you can see this works ! (Google in French)

在此输入图像描述

Unfortunately, node-phantom doesn't appear to support phantom.callback . Since the project is inactive for more than a year, I think it's unlikely to be updated in the near future.

On the other hand, phantomjs-node supports phantom.callback() since version 0.6.6 . You can use it like this:

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

As you can also see on this gist .

node phantom seems to expose this proxy-object via the create function (this should be your ph-object):

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

that means, that you can probably acces the phantoms callback like this:

ph._phantom.callback

Here what I did to access phantom.callback:

add this to node-phantom.js line 202:

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

just before _phantom: phantom

and add this to bridge.js line 45:

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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM