java html转pdf 使用wkhtmltox 组件

java  html转pdf 

下载 wkhtmltox 组件  安装

下载地址

wkhtmltox-0.12.2.4_mingw-w64-cross-win64  

http://download.csdn.net/detail/lvsucceed/9278867

(Window32位)wkhtmltopdf-0.9.9-installer  

http://download.csdn.net/detail/lvsucceed/9278891







package HTML2PDF;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


import javax.annotation.Resource;


import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;


import freemarker.template.Template;


/** 
 * 生成PDF,通过freemarker模板
 * @author zf
 */
@Component("PDFTool")
public class PDFTool {


@Resource(name="webfreemarkerconfiguration")
private FreeMarkerConfigurer freemarkerconfiguration; 




private final String dzorderftl = "pdftemplete/dzorder.ftl"; 


private static String tmpdir = PDFTool.class.getResource("/").getPath() + "tmpdir";
{
tmpdir = tmpdir.replaceAll("^/", "");
}


public InputStream generationPdfDzOrder(Map<String ,Object> params) throws Exception{
final Template template = freemarkerconfiguration.getConfiguration().getTemplate(dzorderftl);
String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, params); 
String tmpFileName = UUID.randomUUID().toString(); //生成随机文件名
File dir = new File(tmpdir);
if(!dir.exists())
dir.mkdirs();
String htmlFileName =  tmpdir + "/" + tmpFileName + ".html" ; 
String pdfFileName = tmpdir + "/" + tmpFileName + ".pdf" ;
File htmlFile = new File(htmlFileName);   //html文件
File pdfFile = new  File(pdfFileName); //pdf文件
IOUtils.write(htmlText, new FileOutputStream(htmlFile)); //将内容写入html文件
String command = getCommand(htmlFileName , pdfFileName);
Runtime.getRuntime().exec(command);
TimeUnit.SECONDS.sleep(3); 
return new FileInputStream(pdfFile);
}

public String  gethtmltest(){
String html="";
html+="<!DOCTYPE html>";
html+="<html>";
html+="<head>";
html+="<title>test.html</title>";
html+="<meta name=\"content-type\" content=\"text/html; charset=UTF-8\">";
html+="</head>";
html+="<body>";
html+="This is my HTML pa     ge1. <br>";
html+="This is my HTML page2. <br>";
html+="This is my HTML page     3. <br>";
html+="This is my HTML page4. <br>";
html+="This is my HTML p      age5. <br>";
html+=" </body>";
html+="</html>";



return html;

}
public InputStream generationPdfDzOrder1() throws Exception{
String htmlText = gethtmltest();
String tmpFileName ="test"; //生成随机文件名
File dir = new File(tmpdir);
if(!dir.exists())
dir.mkdirs();
String htmlFileName =  tmpdir + "/" + tmpFileName + ".html" ; 
String pdfFileName = tmpdir + "/" + tmpFileName + ".pdf" ;
System.out.println("htmlFileName=========="+htmlFileName);
System.out.println("pdfFileName=========="+pdfFileName);
File htmlFile = new File(htmlFileName);   //html文件
File pdfFile = new  File(pdfFileName); //pdf文件
IOUtils.write(htmlText, new FileOutputStream(htmlFile)); //将内容写入html文件
//String command = getCommand(htmlFileName , pdfFileName);


//command   wkhtmltopdf.exe运行地址  空格 加html地址 加空格 加pdf存储地址

String command ="D:/wkhtmltopdf/bin/wkhtmltopdf.exe " + htmlFileName + " " + pdfFileName;

System.out.println("command]============"+command);

Runtime.getRuntime().exec(command);
TimeUnit.SECONDS.sleep(3); 
return new FileInputStream(pdfFile);
}

public String getCommand(String htmlName , String pdfName){
String system = System.getProperty("os.name");
if("Windows XP".equalsIgnoreCase(system))//xp系统
return "D:/wkhtmltopdf/bin/wkhtmltopdf.exe " + htmlName + " " + pdfName;
else  if("Linux".equalsIgnoreCase(system))//linux 系统
return "wkhtmltopdf-amd64 " + htmlName + " " + pdfName;
return "" ;
}

public static void main(String[] args) {
PDFTool pdfTool=new PDFTool();
try {
pdfTool.generationPdfDzOrder1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
  

}






阅读更多
文章标签: HTML转pdf
个人分类: java
上一篇MySQL 5.6 for Windows 解压缩版配置安装
下一篇cordova 安装配置 及遇到的问题解决
想对作者说点什么? 我来说一句

wkhtmltox html生成pdf

2017年12月14日 79.38MB 下载

wkhtmltox-0.12.3_linux-generic-amd64.tar.xz

2016年12月19日 13.86MB 下载

java完美实现html转pdf(wkhtmltopdf-0.9.9)

2017年12月16日 14.37MB 下载

没有更多推荐了,返回首页

关闭
关闭