viewportSize seems not to work with PhantomJS

后端 未结 4 984
半阙折子戏
半阙折子戏 2021-01-02 01:21

Shouldn\'t the output from this PhantomJS script be 240x320 pixels? I\'m getting a large, default-sized image. clipRect() would seem to render the correct size image, but I

4条回答
  •  执笔经年
    2021-01-02 01:44

    This works!! Found the snippet on the github page of the issue.It forces the 'body' element to the page viewportSize:

    var width = 1024;
    var height = 768;
    var webpage = require('webpage');
    
    page = webpage.create();
    page.viewportSize = {width: width, height: height};
    page.open('http://harness.io', function(status) {
        console.log(status);
        page.evaluate(function(w, h) {
          document.body.style.width = w + "px";
          document.body.style.height = h + "px";
        }, width, height);
        page.clipRect = {top: 0, left: 0, width: width, height: height};                                                                                                                           
        page.render('/tmp/test.png');
        phantom.exit();
    });
    

提交回复
热议问题