libreOffice word 转 pdf

简介: 在Windows环境下,使用documents4j进行DOCX到PDF的转换大约需要20秒,而Linux环境下通过Docker配置LibreOffice进行转换,时间仅为1.4秒。documents4j仅适用于Windows且需Microsoft Office支持,libreOfficeCommand则依赖于Linux环境。Spire.Doc.Free有前4页免费但有水印,不推荐。

直接上结论

1、spire.doc.free 收费不考虑、前4页无水印 30s
2、docx4j 20s
3、liboffice 处理时间1.4s
4、documents4j较快只支持windows micro office

1 Windows 环境WORD 转换

dom4j 只能在windows环境调用microsoft office

引入jar包

      <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.0.3</version>
        </dependency>

    public static void wordToPdf(MultipartFile file, String outFilePath) {
        File outputFile = new File(outFilePath);
        InputStream doc = null;
        OutputStream outputStream = null;
        try {
            doc = file.getInputStream();
            outputStream = Files.newOutputStream(outputFile.toPath(), StandardOpenOption.CREATE_NEW);
            IConverter converter = LocalConverter.builder().build();
            //转换docx=>pdf
            boolean flag = converter.convert(doc).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            if (flag) {
                converter.shutDown();
            }
            doc.close();
            outputStream.close();
            System.out.println("文件名:" + outFilePath + " 转换成功!");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件名:" + outFilePath + " 转换失败!");
        }
    }

2 linux 环境WORD 转换

Dockerfile 配置

FROM openjdk:8-jdk-alpine

MAINTAINER auther<>
COPY start.sh /home/
COPY ./target/demo.jar /home/demo.jar

# 复制字体文件到容器的字体目录
COPY simhei.ttf /usr/share/fonts/

# 设置字体文件的权限
RUN chmod 655 /usr/share/fonts/simhei.ttf

ENV TZ=Asia/Shanghai
# 安装字体库,安装liboffice 
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN echo -e 'https://mirrors.aliyun.com/alpine/v3.6/main/\nhttps://mirrors.aliyun.com/alpine/v3.6/community/' > /etc/apk/repositories \
 && apk update \
 && apk upgrade \
 && apk --no-cache add ttf-dejavu fontconfig libreoffice

# 刷新系统字体缓存
RUN fc-cache -fv


#无用处,liboffce调用的上面容器内的字体
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

EXPOSE 8080
ENTRYPOINT ["/bin/sh", "/home/start.sh"]

调用方法

执行路径
libreOfficeCommand = "/usr/lib/libreoffice/program/soffice.bin"

   public static int poiWord2PDF(String source, String targetDir, String libreOfficeCommand) {
//   libreOfficeCommand Linux可执行文件的路径  source word完整路径  targeDir 则是PDF的存放目录,不带文件名称噢,文件名称会自动取source的文件名称作为名称
        String[] cmdArray = {
                libreOfficeCommand,
                "--headless",
                "--convert-to", "pdf:writer_pdf_Export",
                source,
                "--outdir", targetDir
        };
        int exitCode = 0;
        try {
            Process process = Runtime.getRuntime().exec(cmdArray);
            // simhei.ttf
            // 获取命令执行的输出
            InputStream inputStream = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            exitCode = process.waitFor();
            System.out.println(("转换码:"+exitCode));
        } catch (Exception e) {
            System.out.println( "网络繁忙,请重试!");
        }
        return exitCode;
    }
相关文章
|
7天前
|
Linux Python Windows
Python PDF文件转Word格式,只需要3秒(附打包)
Python PDF文件转Word格式,只需要3秒(附打包)
26 3
Python PDF文件转Word格式,只需要3秒(附打包)
|
6天前
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
22 1
|
1月前
|
JavaScript Java
Java 将Markdown文件转换为Word和PDF文档
【7月更文挑战第5天】Java中使用`Spire.Doc for Java`库可方便地将Markdown转换为Word或PDF。基本步骤包括导入模块,创建`Document`对象,加载Markdown文件,然后保存为目标格式(`.docx`或`.pdf`)。若遇到`Invalid UTF-8 stream`错误,需确保Markdown文件是UTF-8无BOM编码。页面设置可通过`PageSetup`类调整。注意,实际应用会依据具体需求和环境有所调整。
|
2月前
使用LabVIEW打开默认应用程序中的文档(PDF,Word,Excel,Html)
使用LabVIEW的&quot;Open a Document on Disk.vi&quot;,存于&lt;LabVIEW&gt;\vi.lib\Platform\browser.llb,可让默认应用打开硬盘文档。此VI仅基础打开功能,高级控制推荐LabVIEW Report Generation Toolkit或ActiveX。注意:避免版本升级问题,最好将VI复制到vi.lib外的目录。
|
3月前
|
Java Apache
Java将word、excel文件转成pdf文件
【5月更文挑战第26天】Java将word、excel文件转成pdf文件
1137 1
|
3月前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
249 0
|
4天前
|
XML 缓存 JSON
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
13 0
|
2月前
|
IDE Java 编译器
使用Java分割PDF文件
使用Java分割PDF文件
42 1
|
6天前
|
Python
Python——批量将PDF文件转为图片
Python——批量将PDF文件转为图片
16 2
|
17天前
【科研技巧】Mac 系统如何给Endnote 20添加某篇文献信息和附加PDF文件
Mac系统下使用Endnote 20添加文献信息和PDF文件的详细步骤,包括下载.enw文件和在Endnote中导入这些文件的方法。
30 6

热门文章

最新文章