Docx4j的Word转PDF及下载PDF

澹台啸
2023-12-01

现将html页面转为word文档在转为PDF(预览)或PDF文件下载


Word转PDF

  1.   /** 
  2.      * docx文档转换为PDF 
  3.      * 
  4.      * @param docx docx文档 
  5.      * @param pdfPath PDF文档存储路径 
  6.      * @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等 
  7.      */  
  8.     public static void convertDocxToPDF(String docxPath, String pdfPath) throws Exception {  
  9.         OutputStream os = null;  
  10.         try {  
  11.             WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new File(docxPath));
  12.             Mapper fontMapper = new IdentityPlusMapper();  
  13.             fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));  
  14.             fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));  
  15.             fontMapper.put("隶书", PhysicalFonts.get("LiSu"));  
  16.             mlPackage.setFontMapper(fontMapper);  
  17.   
  18.             os = new java.io.FileOutputStream(pdfPath);  
  19.   
  20.             FOSettings foSettings = Docx4J.createFOSettings();  
  21.             foSettings.setWmlPackage(mlPackage);  
  22.             Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);  
  23.   
  24.         }catch(Exception ex){  
  25.             ex.printStackTrace();  
  26.         }finally {  
  27.             IOUtils.closeQuietly(os);  
  28.         }  
  29.     }  

  1.   public  void pdf(WordprocessingMLPackage wordMLPackage,OutputStream os) 
  2. throws Exception {
  3.     
  4. String regex = null;

    FieldUpdater updater = new FieldUpdater(wordMLPackage);
    updater.update(true);

    // Set up font mapper (optional)
    Mapper fontMapper = new IdentityPlusMapper();
    wordMLPackage.setFontMapper(fontMapper);


    PhysicalFont font 
    = PhysicalFonts.get("Arial Unicode MS"); 


    // FO exporter setup (required)
    // .. the FOSettings object
        FOSettings foSettings = Docx4J.createFOSettings();
     
    foSettings.setWmlPackage(wordMLPackage);

    // Document format: 
    // The default implementation of the FORenderer that uses Apache Fop will output
    // a PDF document if nothing is passed via 
    // foSettings.setApacheFopMime(apacheFopMime)
    // apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
    // FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
    //foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);

    // Specify whether PDF export uses XSLT or not to create the FO
    // (XSLT takes longer, but is more complete).

    // Don't care what type of exporter you use
    Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

    // Prefer the exporter, that uses a xsl transformation
    // Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

    // Prefer the exporter, that doesn't use a xsl transformation (= uses a visitor)
    // .. faster, but not yet at feature parity
    // Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
       
    // Clean up, so any ObfuscatedFontPart temp files can be deleted 
    // if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
    // wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
    // }
    // This would also do it, via finalize() methods
    updater = null;
    foSettings = null;
    wordMLPackage = null;

        }   
 

下载PDF


  1.   public  void pdfFile(WordprocessingMLPackage wordMLPackage,OutputStream os) 
                throws Exception {
     
    // Font regex (optional)
    // Set regex if you want to restrict to some defined subset of fonts
    // Here we have to do this before calling createContent,
    // since that discovers fonts
    String regex = null;



    /* // Refresh the values of DOCPROPERTY fields 
    FieldUpdater updater = new FieldUpdater(wordMLPackage);
    updater.update(true);*/

    // Set up font mapper (optional)
    Mapper fontMapper = new IdentityPlusMapper();
    wordMLPackage.setFontMapper(fontMapper);

    // .. example of mapping font Times New Roman which doesn't have certain Arabic glyphs
    // eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
    // eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
    // to a font which does
    PhysicalFont font 
    = PhysicalFonts.get("Arial Unicode MS"); 
    String directory = "D:\\upload\\pdf";
    String fileName = "OUT_ConvertInXHTMLURL.pdf";
    File f = new File(directory,fileName);
            if(f.exists()) {
              // 文件已经存在,输出文件的相关信息
                System.out.println(f.getAbsolutePath());
                System.out.println(f.getName());
                System.out.println(f.length());
            } else {
              //  先创建文件所在的目录
                f.getParentFile().mkdirs();
            }
    File file = new File(directory+"/"+fileName);


           OutputStream os34 = new java.io.FileOutputStream(file);


           Docx4J.toPDF(wordMLPackage, os34);


           os.flush();
           os.close();
           
         /*  updater = null;*/
    wordMLPackage = null;
        }   

 类似资料: