Html转pdf(JAVA)

html转pdf工具类

/**
 * HTML转PDF的工具类
 */
public class ConverterHTMLToPDF {
    private static Logger logger = Logger.getLogger(ConverterHTMLToPDF.class.getName());
    public void converterHTMLToPDF(String  content,String downloadType,String myRandom)
    {
        logger.debug("HTML的内容:"+content+",是不是公文"+downloadType);
        String toPdfExeHome = "";
        com.whir.component.config.ConfigXMLReader  reader=new com.whir.component.config.ConfigXMLReader();
        toPdfExeHome = reader.getAttribute("topdftools", "topdfhome");
        String osName = System.getProperties().getProperty("os.name").toUpperCase();
        String htmlFile_temp_path = SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_temp.html";
        if(downloadType.equals("gov")){ 
            htmlFile_temp_path=SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_govtemp.html";
        }
        logger.debug("临时HTML文件"+htmlFile_temp_path);
        String fileContent = "";
        try {
            fileContent = org.apache.commons.io.FileUtils.readFileToString(new File(htmlFile_temp_path), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
             
        fileContent=fileContent.replace("[htmlcontent]",content);
        
        String  temppath=SystemPath.getRootFilePath()+File.separator+"upload"+File.separator+"tempfile";
        
        String htmlName=myRandom+".html";
        String pdfName=myRandom+".pdf";
        //pdfName="export2pdf_temp.pdf";
      
        String  htmlFile_path=temppath+File.separator+"temphtml"+File.separator+htmlName;
        String  pdfFile_path=temppath+File.separator+"temppdf"+File.separator+pdfName;
        logger.debug("HTML的路径:"+htmlFile_path+",PDF文件路径"+pdfFile_path);
        try {
            org.apache.commons.io.FileUtils.writeStringToFile(new File(htmlFile_path), fileContent, "UTF-8");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
          Runtime r = Runtime.getRuntime();
            try {
                Process pro = null; 
                
                if (osName.startsWith("WIN")){
                   pro = r.exec( toPdfExeHome+"\\wkhtmltopdf.exe "+htmlFile_path+" "+pdfFile_path);
                }else{
                    logger.debug("linux tttt:"+toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
                   pro = r.exec( toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
                }
                new DoOutput(pro.getInputStream()).start();
                new DoOutput(pro.getErrorStream()).start();
                
                try {
                    pro.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
               
            } catch (IOException eee) {
                eee.printStackTrace(); 
            }
    }

猜你喜欢

转载自www.cnblogs.com/sjzxs/p/11138921.html
今日推荐