java 转pdf 源码

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.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;  

 
 
/**  
 * 生成PDF
 *
 */  

public class PDFTool {  
 

 
    
    public static InputStream generationPdf(HttpServletRequest request,String htmlText ) throws Exception{  
    
        
        String  path = request.getSession().getServletContext().getRealPath("pdf");  
        System.out.println("原path: " + path);  
        //把路径中的反斜杠转成正斜杠  
        path = path.replaceAll("\\\\", "/"); //
        String tmpFileName = UUID.randomUUID().toString(); //生成随机文件名  
       /* File dir = new File(tmpdir);  
        if(!dir.exists())  
            dir.mkdirs();  */
        System.out.println(path +";tmpdirtmpdirtmpdirtmpdirtmpdirtmpdirtmpdirtmpdir;"+tmpFileName);
        String htmlFileName =  path +"/" + tmpFileName + ".html" ;   
        String pdfFileName = path + "/" + tmpFileName + ".pdf" ;  
        File htmlFile = new File(htmlFileName);   //html文件  
        File pdfFile = new  File(pdfFileName); //pdf文件  
        
        deleteFile(htmlFile, 1000 * 60 * 10);
        deleteFile(pdfFile, 1000 * 60 * 10);
        IOUtils.write(htmlText, new FileOutputStream(htmlFile)); //将内容写入html文件  
        String command = getCommand(htmlFileName , pdfFileName);  
        System.out.println(command);
        Runtime.getRuntime().exec(command);  
        TimeUnit.SECONDS.sleep(3);   
        return new FileInputStream(pdfFile);  
    }  
      
    public static String getCommand(String htmlName , String pdfName){  
        
        String system = System.getProperty("os.name");  
        if("Windows 7".equalsIgnoreCase(system)){//xp系统
            System.out.println("xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  xp系统  ");
            return "\"C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe\"" +"         "+ htmlName + " " + pdfName;  
        }else  if("Linux".equalsIgnoreCase(system))  //linux 系统  
            return "wkhtmltopdf    --encoding  utf-8  " + htmlName + " " + pdfName;  
        return ""   ;  
    }  
    public static void deleteFile(final File file, final long time) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                file.delete();
            }
        }).start();
    }
   
    


开始一直用的是itext 样式一直存在问题,最好在网上找到一个wkhtmltopdf   工具,感觉还不错吧

安装wkhtmltopdf   

window 版 我的系统是64位的    链接:http://pan.baidu.com/s/1bLcuHg 密码:xqei


linux 版本  安装命令:yum install  wkhtmltopdf

(linux 可能有个中文问题   把window 的字体文件上传到linux 的/usr/share/fonts目录)






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值