freemarker生成的word转pdf解决方案

freemarker生成的word转pdf解决方案

	freemarker生成的word本体是xml格式的文件,所有直接将文件后缀改为xml。
	利用商业版软件spire将xml转为真正的word文件(docx),当然我使用的事免费功能,免费功能转为word后文档中会出现警告提示。(如果money到位可以直接购买商业spire,可直接转pdf,即可剩下后边步骤。免费版的转pdf文件只能是前几页,所以不适用)
	利用XWPFDocument的功能将警告提示信息去除。然后利用poi里的功能将word转换为pdf文件。
	废话不多说了,见代码。

pom文件,需要的依赖

<dependency>
    <groupId>com.lowagiegroupId>
    <artifactId>itextartifactId>
    <version>2.1.7version>
dependency>
<dependency>
    <groupId>org.freemarkergroupId>
    <artifactId>freemarkerartifactId>
    <version>2.3.31version>
dependency>
<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>ooxml-schemasartifactId>
    <version>1.3version>
dependency>
<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poi-ooxmlartifactId>
    <version>3.15version>
dependency>
<dependency>
    <groupId>fr.opensagres.xdocreportgroupId>
    <artifactId>org.apache.poi.xwpf.converter.pdfartifactId>
    <version>1.0.6version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
        exclusion>
        <exclusion>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxml-schemasartifactId>
        exclusion>
        <exclusion>
            <groupId>org.apache.poigroupId>
            <artifactId>ooxml-schemasartifactId>
        exclusion>
    exclusions>
dependency>
    <dependency>
		<groupId>e-icebluegroupId>
		<artifactId>spire.officeartifactId>
		<version>7.5.4version>
    dependency>

<repositories>
        <repository>
            <id>com.e-iceblueid>
            <url>https://repo.e-iceblue.cn/repository/maven-public/url>
        repository>
    repositories>
    

springboot 启动类,为简便直接测试将代码写到启动类中

package cn.com.wy.test;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;


/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args ) throws Exception
    {
    	SpringApplication.run(App.class, args);
    	Document doc = new Document();
        doc.loadFromFile("E:/test/123.xml");
      //保存为Docx格式
        doc.saveToFile("E:/test/123toDocx.docx",FileFormat.Docx);
    	
    	 XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("E:/test/123toDocx.docx"));
    	    List<XWPFParagraph> list=document.getParagraphs() ;
    	    //删除警告信息
    	    for(int i=list.size()-1;i>=0;i--){
    	    	if("Evaluation Warning: The document was created with Spire.Doc for JAVA.".equals(list.get(i).getText())){
    	    		list.get(i).removeRun(i);
    	    	}
    	    }
          PdfOptions options = PdfOptions.create();
          /*docx生成pdf设置字体,windows生成pdf时可以不需要以下内容,linux需要进行字体设置*/
//          options.fontProvider(new IFontProvider() {
//              @Override
//              public Font getFont(String familyName, String encoding, float size, int style, java.awt.Color color) {
//                  try {
//                      /*iTextAsian.jar*/
//                      /*BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);*/
//                      String prefixFont = null;
//                      String os = System.getProperties().getProperty("os.name");
//                      if (os.startsWith("win") || os.startsWith("Win")) {
//                          /*windows字体*/
//                          prefixFont = "C:/Windows/Fonts";
//                      } else {
//                          /*linux字体*/
                          prefixFont = linuxFont;
//                      }
//                      BaseFont bfChinese = BaseFont.createFont(prefixFont, "corbel",BaseFont.NOT_EMBEDDED);
//                      Font fontChinese = new Font(bfChinese, size, style, color);
//                      if (familyName != null)
//                          fontChinese.setFamily(familyName);
//                      return fontChinese;
//                  } catch (Exception e) {
//                      e.printStackTrace();
//                      return null;
//                  }
//              }
//          });
          /*生成pdf*/
          try (OutputStream out = Files.newOutputStream(Paths.get("E:/test/1234.pdf"))) {
              PdfConverter.getInstance().convert(document, out, options);
              out.close();
          }
    }
}

你可能感兴趣的:(代码,freemarker,poi和spire组合使用,word,pdf,java)