html 转 pdf java_Html转pdf(JAVA)

/*** HTML转PDF的工具类*/

SystemPath(获取路径,需要自己处理下)

public classConverterHTMLToPDF {private static Logger logger = Logger.getLogger(ConverterHTMLToPDF.class.getName());public voidconverterHTMLToPDF(String content, String myRandom)

{String toPdfExeHome= "";

com.whir.component.config.ConfigXMLReader reader=newcom.whir.component.config.ConfigXMLReader();

toPdfExeHome= reader.getAttribute("topdftools", "topdfhome");

String osName= System.getProperties().getProperty("os.name").toUpperCase();

String htmlFile_temp_path= SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_temp.html";htmlFile_temp_path=SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_govtemp.html";

logger.debug("临时HTML文件"+htmlFile_temp_path);

String fileContent= "";try{

fileContent= org.apache.commons.io.FileUtils.readFileToString(new File(htmlFile_temp_path), "UTF-8");

}catch(IOException e) {

e.printStackTrace();

}

fileContent=fileContent.replace("[htmlcontent]",content);

String temppath=SystemPath.getRootFilePath()+File.separator+"upload"+File.separator+"tempfile";

String htmlName=myRandom+".html";

String pdfName=myRandom+".pdf";//pdfName="export2pdf_temp.pdf";

String htmlFile_path=temppath+File.separator+"temphtml"+File.separator+htmlName;

String pdfFile_path=temppath+File.separator+"temppdf"+File.separator+pdfName;

logger.debug("HTML的路径:"+htmlFile_path+",PDF文件路径"+pdfFile_path);try{

org.apache.commons.io.FileUtils.writeStringToFile(new File(htmlFile_path), fileContent, "UTF-8");

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

Runtime r=Runtime.getRuntime();try{

Process pro= null;if (osName.startsWith("WIN")){

pro= r.exec( toPdfExeHome+"\\wkhtmltopdf.exe "+htmlFile_path+" "+pdfFile_path);

}else{

logger.debug("linux tttt:"+toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);

pro= r.exec( toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);

}newDoOutput(pro.getInputStream()).start();newDoOutput(pro.getErrorStream()).start();try{

pro.waitFor();

}catch(InterruptedException e) {

e.printStackTrace();

}

}catch(IOException eee) {

eee.printStackTrace();

}

}

/**

* 把自定义流程的html转换成pdf

* @param htmlFile html文件

* @param pdfFile  pdf文件

*/

public void converterArchivesFileHTMLToPDF(File htmlFile,File pdfFile)

{

String toPdfExeHome = "";

com.whir.component.config.ConfigXMLReader  reader=new com.whir.component.config.ConfigXMLReader();

toPdfExeHome = reader.getAttribute("topdftools", "topdfhome");

String osName = System.getProperties().getProperty("os.name").toUpperCase();

String  htmlFile_path= htmlFile.getAbsolutePath();

String  pdfFile_path=  pdfFile.getAbsolutePath();

logger.debug("HTML的路径:"+htmlFile_path+",PDF文件路径"+pdfFile_path);

Runtime r = Runtime.getRuntime();

try {

Process pro = null;

if (osName.startsWith("WIN")){

pro = r.exec( toPdfExeHome+"\\wkhtmltopdf.exe "+htmlFile_path+" "+pdfFile_path);

}else{

logger.debug("linux tttt:"+toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);

pro = r.exec( toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);

}

new DoOutput(pro.getInputStream()).start();

new DoOutput(pro.getErrorStream()).start();

try {

pro.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

}

} catch (IOException eee) {

eee.printStackTrace();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值