关闭

java html转pdf 使用wkhtmltox 组件

标签: HTML转pdf
854人阅读 评论(0) 收藏 举报
分类:

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();
}
}
  

}






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13278次
    • 积分:245
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条
    文章分类