phantomjs rendering

http://wwwy3y3.ghost.io/pageres-phantomjs-capture-sreenshot-chinese-fonts-not-render-correctly/

在使用中文时可能出现乱码,上面的链接可能能够解决问题

var system = require( 'system' );
var args   = system.args;
var url    = args[ 1 ];
var page   = require( 'webpage' ).create();

/**
 * Proper error handling
 */
phantom.onError = function( msg, trace ) {
  var msgStack = [ 'PHANTOM ERROR: ' + msg ];
  if ( trace && trace.length ) {
    msgStack.push( 'TRACE:' );
    trace.forEach( function( t ) {
      msgStack.push( ' -> ' + ( t.file || t.sourceURL ) + ': ' + t.line + ( t.function ? ' (in function ' + t.function + ')' : '' ) );
    } );
  }
  console.error( msgStack.join('
') );
  phantom.exit( 1 );
};

if ( args.length === 1 ) {
  throw new Error( 'URL as system argument missing.' );
}

page.viewportSize = {
  height : 1000,
  width  : 1200
};

page.open( url, function( status ) {
  console.log( '********************************************' );
  console.log( 'Opened ' + url + ' with status: ' + status );


  page.render( 'image-1200.png' );
  console.log( 'Rendered ' + url + ' with width of 1200px' );

  page.viewportSize = {
    height : 1000,
    width  : 800
  };

  page.render( 'image-800.png' );
  console.log( 'Rendered ' + url + ' with width of 800px' );

  page.viewportSize = {
    height : 1000,
    width  : 500
  };


  page.render( 'image-500.png' );
  console.log( 'Rendered ' + url + ' with width of 500px' );

  page.clipRect = {
    top: 0, left: 0,  500, height: 300
  };

  page.render( 'image-header-500.png' );
  console.log( 'Rendered header of ' + url + ' with width of 500px' );

  phantom.exit();
} );
原文地址:https://www.cnblogs.com/kidsitcn/p/4803028.html