java把jsp转pdf_将jsp页面转化为图片或pdf升级版(二)(qq:1324981084)

packagecom.jit.platform.basics.util.pdf;importjava.io.File;importjava.io.IOException;public classHtmlToImageOrPdf {

/*** html转pdf

*@paramsrcPath html路径,可以是硬盘上的路径,也可以是网络路径

*@paramdestPath pdf保存路径

*@return转换成功返回true

*@throwsInterruptedException

*@throwsIOException*/

public static void convert(String srcPath, String destPath,String width_height) throwsInterruptedException, IOException{

String toPdfTool=getCommand();

File file= newFile(destPath);

File parent=file.getParentFile();//如果pdf保存路径不存在,则创建路径

if(!parent.exists()){

parent.mkdirs();

}

//将软件路径、html路径、pdf生成路径、和一些形成pdf的参数加到字符串容器中。

StringBuilder cmd= newStringBuilder();

cmd.append(toPdfTool);

cmd.append(" ");

//cmd.append("--margin-top 0mm --margin-bottom 0mm --margin-right 0mm --margin-left 0mm ");

//cmd.append("--page-width "+width_px+" ");

//cmd.append("--page-height "+height_px+" ");

cmd.append(srcPath);

cmd.append(" ");

cmd.append(destPath);long begin =System.currentTimeMillis();

//利用cmd执行刚才添加的参数。

Process proc=Runtime.getRuntime().exec(cmd.toString());long end =System.currentTimeMillis();

System.out.println("形成html执行耗时:" + (end - begin) + " 豪秒");

HtmlToPdfInterceptor error= newHtmlToPdfInterceptor(proc.getErrorStream());

HtmlToPdfInterceptor output= newHtmlToPdfInterceptor(proc.getInputStream());

error.start();

output.start();

proc.waitFor();

}

//为满足window和linux系统,所以我们在这里进行判断public staticString getCommand(){

String system= System.getProperty("os.name");if(system.toLowerCase().indexOf("windows")>=0) //xp系统

return "D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe"; //这里返回你wkhtmltoimage或wkhtmltopdf的exe文件的路径else if(system.toLowerCase().indexOf("linux")>=0) //linux 系统

return "wkhtmltopdf-amd64 ";return "";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值