Ubuntu实现word转pdf
系统信息
Ubuntu / 16.04 LTS amd64 (64bit)
服务器版本
采用工具: libreoffice
安装
下载 LibreOffice_6.3.5_Linux_x86-64_deb.tar.gz。 【特别注意】 是 6.3.5 版本。 并解压
ln -s //opt/libreoffice6.3/program/soffice soffice
dpkg -i *.deb
cd /usr/bin
ln -s /opt/libreoffice6.3/program/soffice soffice
安装 unoconv 工具
apt-get update
apt-get install unoconv -y
apt-get -y install fontconfig xfonts-utils
apt-get -y install ttf-wqy-microhei
fc-list :lang=zh
Java 代码
package io.renren.modules.oss.service.impl;
import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import io.renren.modules.oss.service.WordService;
import ooo.connector.BootstrapSocketConnector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @author Felix Yan
* @data 2018/07/19
*/
@Service("unoWordService")
public class LibreOfficeUnoWordServiceImpl implements WordService {
@Value("${soffice-dir:/usr/bin}")
private String sofficeDir;
@Override
public void word2pdf(String inPath, String outPath) throws Exception {
if (!outPath.endsWith(".pdf")) {
outPath += "_result.pdf";
}
XComponentContext xContext = BootstrapSocketConnector.bootstrap(sofficeDir);
XMultiComponentFactory xMCF = xContext.getServiceManager();
Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
XDesktop xDesktop = UnoRuntime.queryInterface(XDesktop.class, oDesktop);
XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, xDesktop);
XComponent xComp = xCompLoader.loadComponentFromURL(
"file:///" + inPath, "_blank", 0, new PropertyValue[0]);
XStorable xStorable = UnoRuntime.queryInterface(XStorable.class, xComp);
PropertyValue[] propertyValue = new PropertyValue[2];
propertyValue[0] = new PropertyValue();
propertyValue[0].Name = "Overwrite";
propertyValue[0].Value = Boolean.TRUE;
propertyValue[1] = new PropertyValue();
propertyValue[1].Name = "FilterName";
propertyValue[1].Value = "writer_pdf_Export";
xStorable.storeToURL("file:///" + outPath, propertyValue);
xDesktop.terminate();
}
}
测试
命令行监测是否转换成功
unoconv -f pdf 1.doc
// 检测目录是否存在
ls /usr/share/help-langpack/en_GB/evolution/contacts-libreoffice.page
https://blog.csdn.net/only_jing1314/article/details/48008589
遇到的问题
启动失败
CE> Warning: -nologo is deprecated. Use --nologo instead.
CE> Warning: -nodefault is deprecated. Use --nodefault instead.
CE> Warning: -norestore is deprecated. Use --norestore instead.
CE> Warning: -nolockcheck is deprecated. Use --nolockcheck instead.
CE> Warning: -accept=socket,host=localhost,port=8100;urp; is deprecated. Use --accept=socket,host=localhost,port=8100;urp; instead.
2020-05-14 09:41:08.503 ERROR 1940 --- [o-10011-exec-23] i.r.c.exception.RenExceptionHandler : null
com.sun.star.lang.DisposedException: null
at com.sun.star.lib.uno.environments.remote.JobQueue.removeJob(JobQueue.java:210)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:330)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:303)
at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:87)
at
【问题原因】 安装了最新版本的 libreoffice
apt-get install libreoffice
【解决办法】 采用文章首页的 安装办法即可。
分类:
java
, 开发工具 / linux
标签:
ubuntu
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)