java工具word转pdf

需求:将文件转为pdf

         方案1:使用poi工具

        参考:使用poi实现Word转pdf - 极墨笔记

         方案2:使用apose工具

        参考:Aspose word 转换为pdf 排版错位和数据丢失_blues199的博客-CSDN博客

        注意:apose工具使用,在Linux系统部署时,需要添加文件涉及的字体文件,因为linux不支持window的字体文件,会出现pdf乱码问题如图。

 如何解决

        步骤1:断点找到文件用到了哪些字体(也可以自己设置好文档字体)

         步骤2:添加字体文件依赖

        情况1:项目是war包,可将字体文件直接添加到项目目录下,打包部署即可

         情况2:项目是jar包,则需要将字体添加到linux的路径下

        项目配置文件

 

        字体文件目录

至此转换文件成功展示

工具类测试

import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.SaveFormat;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;

import java.io.*;

@Slf4j
@Component
public class PdfUtil {


    /**
     * 要展示的文件流
     *
     * @param wordInputStream
     * @param pdfPath
     * @return
     */
    public boolean doc2pdf(InputStream wordInputStream, String pdfPath) {
        long start = System.currentTimeMillis();
        FileOutputStream os = null;
        try {
            File file = new File(pdfPath); // 新建一个空白pdf文档
            // linux字体设置
            OsInfo osInfo = SystemUtil.getOsInfo();
            if(osInfo.isLinux()){
                FontSettings.getDefaultInstance().setFontsFolder(this.getClass().getClassLoader().getResource("").getPath()+"font", true);
            }
            os = new FileOutputStream(file);
            Document doc = new Document(wordInputStream); // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互转换
            long end = System.currentTimeMillis();
            log.info("pdf转换成功,共耗时:" + ((end - start) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            log.error("Word转pdf失败,原因:", e);
            return false;
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    log.error("文件流关闭失败,原因:", e);
                }
            }
        }
        return true;
    }

    /**
     * 删除PDF
     *
     * @param filePath 文件路径
     */
    public static void deletePDF(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            log.info("PDF临时文件删除:" + file.delete());
        }
    }

    public static   void main(String[] args) throws Exception {
        /**
         * aspose转pdf
         */
        long start = System.currentTimeMillis();
        // 原Word文件
        FileInputStream fileInputStream = new FileInputStream("F:\\承诺书已签字.docx");
        // 要生成的pdf文件
        FileOutputStream os = new FileOutputStream("F:\\承诺书已签字-apos.pdf");
        Document doc = new Document(fileInputStream);
        // 执行转换
        doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
        System.out.println("apose转pdf耗时:" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        /**
         * poi转pdf
         */
        // 原Word文件
        fileInputStream = new FileInputStream("F:\\承诺书已签字.docx");
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        // 要生成的pdf文件
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\承诺书已签字-pdf.pdf");
        PdfOptions pdfOptions = PdfOptions.create();
        // 执行转换
        PdfConverter.getInstance().convert(xwpfDocument, fileOutputStream, pdfOptions);
        System.out.println("poi转pdf耗时:" + (System.currentTimeMillis() - start));
    }

}

        从耗时时间上看,转换一个28K的docx文件耗时,apose耗时更短。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值