phantomjs rendering

本文介绍如何使用PhantomJS解决中文乱码问题,并提供了实例代码来展示如何正确地进行中文字符的截图操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
} );

 

转载于:https://www.cnblogs.com/kidsitcn/p/4803028.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值