html转图片,原来用了html2image.jar,但是做不到复杂的css js渲染效果。
在网友推荐下,可以用wkhtmltoimage插件,这是用谷歌浏览器的内核webkit,网上搜一下可以下载,同时也是可以wkhtmltopdf,在同一个安装包内。
如果需要参数设置,在cmd中打一下 wkhtmltoimage /?
以下的代码我是用作手机版本的
/**
* 生成图片
* @param nickname 微信昵称
* @param headurl 微信
*/
public static void CreateImage(String nickname,String headurl,String openId){
try {
String plugpath="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage";
String montherUrl="http://localhost/mother/index.html";
montherUrl=montherUrl+"?nickname="+nickname+"&headurl="+headurl+"";
System.out.println(montherUrl);
String imgpath="e:\\"+openId+".jpg";
ProcessBuilder pb = new ProcessBuilder(plugpath,"--crop-h","1136","--crop-w","640",montherUrl,imgpath );
Process process;
process = pb.start();
//注意,调用process.getErrorStream()而不是process.getInputStream()
BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
System.out.println("read errstreamreader");
String line = null;
line = errStreamReader.readLine();
while(line != null) {
//System.out.println(line);
line = errStreamReader.readLine();
}
process.destroy();
System.out.println("destroyed process");
} catch (IOException e) {
e.printStackTrace();
}
}