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.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.3</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
    <version>1.0.6</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
        </exclusion>
    </exclusions>
</dependency>
    <dependency>
		<groupId>e-iceblue</groupId>
		<artifactId>spire.office</artifactId>
		<version>7.5.4</version>
    </dependency>

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <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();
          }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值