Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 页脚的内容似乎不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在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不暴露回调方法错误

解决方法

您的脚本中存在两个问题:

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

打印自定义页眉/页脚还需要调用phantom.callBACk.此方法没有记录,因此不会被node-phantom暴露(也不可能).我们需要找到一种方法在这个包中应用这个方法.

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

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

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

phantom.create(function (err,header: {
                            height: "1cm",contents: 'function(pageNum,numPages) { return pageNum + "/" + numPages; }'
                        },footer: {
                            height: "1cm",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;

正如你所看到的那样有效! (谷歌法语)

大佬总结

以上是大佬教程为你收集整理的node.js – 页脚的内容似乎不起作用全部内容,希望文章能够帮你解决node.js – 页脚的内容似乎不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。