系统信息
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
【解决办法】 采用文章首页的 安装办法即可。