欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > Windows环境安装LibreOffice实现word转pdf

Windows环境安装LibreOffice实现word转pdf

2025/5/20 19:25:51 来源:https://blog.csdn.net/weixin_52466601/article/details/148059478  浏览:    关键词:Windows环境安装LibreOffice实现word转pdf

前言:最近在工作中遇到了一个需求要实现word转pdf,本来我在上一个公司使用aspose.words工具使用的得心应手,都已经把功能点实现了,两句代码轻轻松松,但是被告知不能用商业版的东西,公司要求只能用开源的,那么只有另想办法,想过通过word转图片再转pdf但是实现出来的效果没有想象中那么好,果断放弃,最终斟酌了一下用了libreoffice,那么下面为大家分享一下;

一丶安装Libreoffice

  • 确保在服务器或运行环境中已安装 LibreOffice。
  • 可以从 LibreOffice 官方网站 下载并安装。

使用国内镜像下载更快; 主页 | LibreOffice 简体中文官方网站 - 自由免费的办公套件

二丶​​配置 LibreOffice 为服务模式

  • LibreOffice 可以以无头模式运行,处理转换任务。
  • 通常,您可以通过命令行启动 LibreOffice 服务:
#这个将在后台运行Libreoffice,并监听端口2022
soffice --headless --accept="socket,host=127.0.0.1,port=2002;urp;" --nofirststartwizard &

三丶​​添加maven配置

#添加这两个就行
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.4.2</version>
</dependency>
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-spring-boot-starter</artifactId><version>4.4.2</version>
</dependency>

五丶​​实现代码

package cn.it.resource.util;import org.jodconverter.core.office.OfficeException;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;import java.io.File;public class JODConverterExample {public static void main(String[] args) {// 启动 LibreOffice 管理器LocalOfficeManager officeManager = null;try {officeManager = LocalOfficeManager.builder().portNumbers(2002) // 与启动 LibreOffice 时的端口一致.install().build();officeManager.start();File inputFile = new File("D:/home/server/886bdac8cf.docx"); // 替换为实际路径File outputFile = new File("D:/home/server/wordTopdf2.pdf"); // 替换为实际路径LocalConverter.builder().officeManager(officeManager).build().convert(inputFile).to(outputFile).execute();System.out.println("文件转换成功!");} catch (OfficeException e) {e.printStackTrace();} finally {if (officeManager != null) {try {officeManager.stop();} catch (OfficeException e) {e.printStackTrace();}}}}
}

六丶​​出现报错异常,并解决

Exception in thread "main" java.lang.NullPointerException: officeHome must not be nullat org.jodconverter.core.util.AssertUtils.notNull(AssertUtils.java:131)at org.jodconverter.local.office.LocalOfficeUtils.validateOfficeHome(LocalOfficeUtils.java:352)at org.jodconverter.local.office.LocalOfficeManager$Builder.build(LocalOfficeManager.java:189)at cn.it.resource.util.JODConverterExample.main(JODConverterExample.java:19)

启动第五步代码的时候出现了上述错误,通过检查后发现没有配置libreoffice的安装路径,我尝试了在环境变量中直接配置路径,没有用,只有自己定义实现路径; 

        // 必须指定安装路径 否则会报:officeHome must not be nullString officeHome = "D:/libreoffice";// 启动 LibreOffice 管理器LocalOfficeManager officeManager = null;try {officeManager = LocalOfficeManager.builder().portNumbers(2002) // 与启动 LibreOffice 时的端口一致.officeHome(officeHome).install().build();

好了我的分享就在这里了,如果需要上linux服务器操作,在网上找下命令安装一下就行了,然后将代码中的安装路径修改,然后就可以使用了;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词