Html转pdf(JAVA)

前端开发   发布日期:2025年05月18日   浏览次数:166

html转pdf工具类

  1. /**
  2. * HTML转PDF的工具类
  3. */

    SystemPath(获取路径,需要自己处理下)
  4. public class ConverterHTMLToPDF {
  5. private static Logger logger = Logger.getLogger(ConverterHTMLToPDF.class.getName());
  6. public void converterHTMLToPDF(String content, String myRandom)
  7. {
  8. String toPdfExeHome = "";
  9. com.whir.component.config.ConfigXMLReader reader=new com.whir.component.config.ConfigXMLReader();
  10. toPdfExeHome = reader.getAttribute("topdftools", "topdfhome");
  11. String osName = System.getProperties().getProperty("os.name").toUpperCase();
  12. String htmlFile_temp_path = SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_temp.html";
  13. htmlFile_temp_path=SystemPath.getRootFilePath()+File.separator+"platform"+File.separator+"custom"+File.separator+"custom_form"+File.separator+"run"+File.separator+"export2html_govtemp.html";
  14. logger.debug("临时HTML文件"+htmlFile_temp_path);
  15. String fileContent = "";
  16. try {
  17. fileContent = org.apache.commons.io.FileUtils.readFileToString(new File(htmlFile_temp_path), "UTF-8");
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. fileContent=fileContent.replace("[htmlcontent]",content);
  22. String temppath=SystemPath.getRootFilePath()+File.separator+"upload"+File.separator+"tempfile";
  23. String htmlName=myRandom+".html";
  24. String pdfName=myRandom+".pdf";
  25. //pdfName="export2pdf_temp.pdf";
  26. String htmlFile_path=temppath+File.separator+"temphtml"+File.separator+htmlName;
  27. String pdfFile_path=temppath+File.separator+"temppdf"+File.separator+pdfName;
  28. logger.debug("HTML的路径:"+htmlFile_path+",PDF文件路径"+pdfFile_path);
  29. try {
  30. org.apache.commons.io.FileUtils.writeStringToFile(new File(htmlFile_path), fileContent, "UTF-8");
  31. } catch (IOException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. Runtime r = Runtime.getRuntime();
  36. try {
  37. Process pro = null;
  38. if (osName.startsWith("WIN")){
  39. pro = r.exec( toPdfExeHome+"\\wkhtmltopdf.exe "+htmlFile_path+" "+pdfFile_path);
  40. }else{
  41. logger.debug("linux tttt:"+toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
  42. pro = r.exec( toPdfExeHome+File.separator+"wkhtmltopdf "+htmlFile_path+" "+pdfFile_path);
  43. }
  44. new DoOutput(pro.getInputStream()).start();
  45. new DoOutput(pro.getErrorStream()).start();
  46. try {
  47. pro.waitFor();
  48. } catch (InterruptedException e) {
  49. e.printStackTrace();
  50. }
  51. } catch (IOException eee) {
  52. eee.printStackTrace();
  53. }
  54. }


    /**
         * 把自定义流程的html转换成pdf
         * @param htmlFile html文件
         * @param pdfFile  pdf文件
         */
        public void converterArchivesFileHTMLToPDF(File htmlFile,File pdfFile)
        {
          
            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_path= htmlFile.getAbsolutePath();
            String  pdfFile_path=  pdfFile.getAbsolutePath();
            logger.debug("HTML的路径:"+htmlFile_path+",PDF文件路径"+pdfFile_path);
              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();
                }
        }

 

以上就是Html转pdf(JAVA)的详细内容,更多关于Html转pdf(JAVA)的资料请关注九品源码其它相关文章!