java实现网页转图片或者pdf的需求实现
通过工具wkhtmltopdf 实现
命令: E:\wkhtmltopdf\bin\wkhtmltopdf.exe http://www.jyb.cn/ d:/out.pdf
示例java代码
public class Test4 {
private static String toImageTool = "E:\\wkhtmltopdf\\bin\\wkhtmltoimage.exe";
private static String toImageUrl = "http://www.jyb.com/";
private static String outImageFile = "d:/out.jpg";
public static void main(String[] args) throws Exception{
String commond = toImageTool +" " + toImageUrl + " " + outImageFile;
System.out.println(commond);
Runtime.getRuntime().exec(commond);
Process proc = Runtime.getRuntime().exec(commond.toString());
HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
error.start();
output.start();
proc.waitFor();
}
}
public class HtmlToPdfInterceptor extends Thread {
private InputStream is;
public HtmlToPdfInterceptor(InputStream is){
this.is = is;
}
public void run(){
try{
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line.toString()); //输出内容
}
}catch (IOException e){
e.printStackTrace();
}
}
}