zoukankan      html  css  js  c++  java
  • java 转html为pdf

    最近有个需求转html为pdf 。

    用过itext 、 pd4ml  ,都不理想,不是样式有问题,就是页面大小有问题。 或字体有问题。

    解决办法是通过wkhtmltopdf工具 , 下载地址为:http://code.google.com/p/wkhtmltopdf/

    (有windowx版本,和linux版本。) 解压后是一个可执行文件。用法为  dir/wkhtmltopdf  htmlpath pdfpath

    在linux上面,可以将该可执行文件 软连接到path文件夹下面入: sudo ln -s /home/zhoufeng/wkhtmltopdf  /usr/bin/wkhtmltopdf

    package etds.report.util;
    
    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 getCommand(String htmlName , String pdfName){
    		String system = System.getProperty("os.name");
    		if("Windows XP".equalsIgnoreCase(system))	//xp系统
    			return "D:/Program Files/wkhtmltopdf/wkhtmltopdf.exe " + htmlName + " " + pdfName;
    		else  if("Linux".equalsIgnoreCase(system))	//linux 系统
    			return "wkhtmltopdf-amd64 " + htmlName + " " + pdfName;
    		return ""	;
    	}
      
    }
    
  • 相关阅读:
    利用javabean完成注册效果
    DAO设计模式例子
    SmartUpload使用和简介
    js和jquery通过this获取html标签中的属性值
    细说引用类型string StringBuilder class的特点
    WinForm控件自动提示你定义相似的项值
    利用Assembly动态加载程序集
    C# 数据结构与算法系列(五) 队列
    ASP.NET最简单的用户权限管理
    C# 数据结构与算法系列(三) 线性表之链表
  • 原文地址:https://www.cnblogs.com/myfreefield/p/efgerg.html
Copyright © 2011-2022 走看看