How to render part of a page with PhantomJS?

后端 未结 5 1183
生来不讨喜
生来不讨喜 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: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();
    });
    

提交回复
热议问题