How to render part of a page with PhantomJS?

后端 未结 5 1170
生来不讨喜
生来不讨喜 2020-11-29 00:58

I would like to render individual HTML elements into PNGs using Phantom.JS. Does anyone know if this is possible? Also, how would I use Phantom.js to render a page that the

相关标签:
5条回答
  • 2020-11-29 01:39

    The solution below works for me.

        var clipRect = document.querySelector(selector).getBoundingClientRect();
         page.clipRect = {
                  top:    clipRect.top,
                  left:   clipRect.left,
                  width:  clipRect.width,
                  height: clipRect.height
          };
       page.render('capture.png');
    

    But I notice that this on works only if we are rendering an image not a pdf. To wokaround this for pdf, try this

    page.evaluate(function (element){
        $(element).appendTo('body');
        $('body > :not(' + element + ')').hide(); 
       }, element);       
      });
    
    window.setTimeout(function(){
        page.render("page.pdf");
      },1000);
    

    This links may help: How to hide all elements except one using jquery?

    https://github.com/ariya/phantomjs/issues/10465

    0 讨论(0)
  • 2020-11-29 01:42

    You can use the CasperJS, another project based on PhantomJS.

    casper.start('http://www.weather.com/', function() {
        this.captureSelector('weather.png', '#wx-main');
    });
    
    casper.run();
    
    0 讨论(0)
  • 2020-11-29 01:49

    Working example.

    var page = require('webpage').create();
    
    page.open('http://google.com', function() {
      // being the actual size of the headless browser
      page.viewportSize = { width: 1440, height: 900 };
    
      var clipRect = page.evaluate(function(){
        return document.querySelector('#hplogo').getBoundingClientRect();
      });
    
      page.clipRect = {
        top:    clipRect.top,
        left:   clipRect.left,
        width:  clipRect.width,
        height: clipRect.height
      };
    
      page.render('google.png');
      phantom.exit();
    });
    
    0 讨论(0)
  • 2020-11-29 02:01

    I had the same need, I tried this and it worked fine for me :

    don't forget the http://www in the URL

    var page = require('webpage').create();
    page.open('YourPageURL', function (status) {
    
    if (status !== 'success') {
        console.log('Network Problem');
    } else {
        var p = page.evaluate(function () {
            return document.getElementById('yourDivID').innerHTML
        });
        console.log(p);
    }
    phantom.exit();
    });
    
    0 讨论(0)
  • 2020-11-29 02:03

    To only render part of a page you need to set the clipRect attribute for the page and then render it.

    var clipRect = document.querySelector(selector).getBoundingClientRect();
    page.clipRect = {
        top:    clipRect.top,
        left:   clipRect.left,
        width:  clipRect.width,
        height: clipRect.height
    };
    page.render('capture.png');
    

    I don't understand the second part of your question. Phantom.js is headless meaning that there is no actual display that a user is looking at.

    0 讨论(0)
提交回复
热议问题