java实现word转pdf

前两天写了一篇博客,讲的是word、ppt、pptx等文件转pdf,我研究文件转化也是因为工作需要,后来发现写的功能在我本地(window 10)没有问题,但是发到现场(Linux)word转pdf转化出来时0k,没办法只能再换一种方式,今天就给大家介绍Apache OpenOffice。OpenOffice是开源的办公软件套件,包可以在多个操作系统上运行,包括Windows、Mac OS X和Linux等。
一、下载OpenOffice
OpenOffice下载连接

二、解压依赖包

powershell 复制代码
tar -zxvf 压缩包名称

三、安装依赖

powershell 复制代码
# 移动到指定目录下
cd zh-CN/RPMS
# 安装依赖
yum localinstall *.rpm

四、启动OpenOffice程序

powershell 复制代码
# 这里127.0.0.1只能是本机访问,占用端口设置为8100
nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

五、测试

用openoffice需要集成相关依赖

测试的时候我们需要用到jodconverter-2.2.2.jar

需要注意的是如果服务器的字体不全,转出来的文件可能乱码或者空白或者都是框

powershell 复制代码
# 解压压缩包
unzip jodconverter-2.2.2.zip
# 进入解压后的文件夹
cd /jodconverter-2.2.2/lib
# 运行测试
java -jar jodconverter-cli-2.2.2.jar /root/1.doc /usr/1.pdf

六、项目集成

java 复制代码
  <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>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.17</version>
        </dependency>
		<dependency>
		    <groupId>com.artofsolving</groupId>
		    <artifactId>jodconverter</artifactId>
		    <version>2.2.2</version>
		</dependency>
		<dependency>
		    <groupId>org.openoffice</groupId>
		    <artifactId>juh</artifactId>
		    <version>3.0.1</version>
		</dependency>
		<dependency>
		    <groupId>org.openoffice</groupId>
		    <artifactId>ridl</artifactId>
		    <version>3.0.1</version>
		</dependency>
		<dependency>
		    <groupId>org.openoffice</groupId>
		    <artifactId>unoil</artifactId>
		    <version>3.0.1</version>
		</dependency>
java 复制代码
import java.io.*;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public static void docToPdf(String sourcePath, String targetPath){
        File sourceFile = new File(sourcePath);
            File pdfFile = new File(targetPath);
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            try {
                connection.connect();
                DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                converter.convert(sourceFile, pdfFile);
                connection.disconnect();
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
    }
相关推荐
一只会写代码的猫2 小时前
面向高性能计算与网络服务的C++微内核架构设计与多线程优化实践探索与经验分享
java·开发语言·jvm
萤丰信息3 小时前
智慧园区能源革命:从“耗电黑洞”到零碳样本的蜕变
java·大数据·人工智能·科技·安全·能源·智慧园区
曹牧3 小时前
Eclipse为方法添加注释
java·ide·eclipse
是小胡嘛3 小时前
C++之Any类的模拟实现
linux·开发语言·c++
口袋物联4 小时前
设计模式之工厂模式在 C 语言中的应用(含 Linux 内核实例)
linux·c语言·设计模式·简单工厂模式
我叫张小白。4 小时前
Spring Boot拦截器详解:实现统一的JWT认证
java·spring boot·web·jwt·拦截器·interceptor
qq_479875435 小时前
X-Macros(1)
linux·服务器·windows
笨笨聊运维6 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
Gerardisite6 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
闲人编程7 小时前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器