phantomjs rendering

世有因果知因求果發表於2015-09-12

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('\n') );
  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, width: 500, height: 300
  };

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

  phantom.exit();
} );

 

相關文章