Using the 'webpage' Phantom module in node.js

筅森魡賤 提交于 2019-12-02 19:15:57

It should be something like:

var phantom=require('../node-phantom');
phantom.create(function(error,ph){
  ph.createPage(function(err,page){
    page.open(url ,function(err,status){
      // do something
    });
  });
});

Your confusion here is because you want to reuse the same concepts and metaphors from your PhantomJS script. It does not work that way. I suggest that you spend some time studying the included tests of node-phantom, see https://github.com/alexscheelmeyer/node-phantom/tree/master/test.

Using https://github.com/sgentle/phantomjs-node I have made an A4 page in nodejs using phantom with the following code:

phantom.create(function(ph){
    ph.createPage(function(page) {
        page.set("paperSize", { format: "A4", orientation: 'portrait', margin: '1cm' });
        page.open("http://www.google.com", function(status) {
            page.render("google.pdf", function(){
                console.log("page rendered");
                ph.exit();
            })
        })
    })

});

Side Note:

the page.set() function takes any variable that you would set in the rasterize.js example. See how paperSize is set above and compare it to the relevant lines in rasterize.js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!