html转存pdf

http://blog.csdn.net/is_zhoufeng/article/details/8244454

最近有个需求转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 

[java]  view plain copy
  1.   
[java]  view plain copy
  1. package javapdf;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.InputStream;  
  5. import javax.annotation.Resource;  
  6. import org.springframework.stereotype.Component;  
  7. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;  
  8. /**  
  9.  * 生成PDF,通过freemarker模板 
  10.  * @author zf 
  11.  */  
  12. @Component("PDFTool")  
  13. public class PDFTool {  
  14.   
  15.     @Resource(name="webfreemarkerconfiguration")  
  16.     private FreeMarkerConfigurer freemarkerconfiguration = new FreeMarkerConfigurer();   
  17.       
  18.     public static void main(String[] args) throws Exception {  
  19. //      Map<String,Object> params = new HashMap<String,Object>();  
  20. //      params.put("http://www.guidechem.com", "C:/pdftool.pdf");  
  21.         long start = System.currentTimeMillis();  
  22.         new PDFTool().generationPdfDzOrder("http://21food.cn""C:/pdftool.pdf");  
  23.         System.out.println(System.currentTimeMillis() - start);  
  24.     }  
  25.   
  26. //  private final String dzorderftl = "pdftemplete/dzorder.ftl";   
  27.   
  28. //  private static String tmpdir = PDFTool.class.getResource("/").getPath() + "tmpdir";  
  29. //  {  
  30. //      tmpdir = tmpdir.replaceAll("^/", "");  
  31. //  }  
  32.   
  33.     public InputStream generationPdfDzOrder(String htmlFileName,String pdfFileName) throws Exception{  
  34. //      final Template template = freemarkerconfiguration.getConfiguration().getTemplate(dzorderftl);  
  35. //      String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);   
  36. //      String tmpFileName = UUID.randomUUID().toString(); //生成随机文件名  
  37. //      File dir = new File(tmpdir);  
  38. //      if(!dir.exists())  
  39. //          dir.mkdirs();  
  40. //      String htmlFileName =  tmpdir + "/" + tmpFileName + ".html" ;   
  41. //      File htmlFile = new File(htmlFileName);   //html文件  
  42. //      String pdfFileName = tmpdir + "/" + tmpFileName + ".pdf" ;  
  43. //      IOUtils.write(htmlText, new FileOutputStream(htmlFile)); //将内容写入html文件  
  44.           
  45.         String command = getCommand(htmlFileName , pdfFileName);  
  46.         System.out.println(command);  
  47.         File pdfFile = new  File(pdfFileName); //pdf文件  
  48.         if(!pdfFile.exists()){pdfFile.createNewFile();}  
  49.         Runtime.getRuntime().exec(command);  
  50. //      TimeUnit.SECONDS.sleep(3);   
  51.         return new FileInputStream(pdfFile);  
  52.     }  
  53.       
  54.     public String getCommand(String htmlName , String pdfName){  
  55.         String system = System.getProperty("os.name");  
  56. //      if("Windows XP".equalsIgnoreCase(system))   //xp系统  
  57.             return "D:/Program Files/wkhtmltopdf/wkhtmltopdf.exe " + htmlName + " " + pdfName;  
  58. //      else  if("Linux".equalsIgnoreCase(system))  //linux 系统  
  59. //          return "wkhtmltopdf-amd64 " + htmlName + " " + pdfName;  
  60. //      return ""   ;  
  61.     }  
  62.     
  63. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值