CentOS安装LibreOffice&转换为PDF格式

2020-05-12  本文已影响0人  程序员小白成长记

环境:
centos7
libreoffice6.4.3
网站:
libreoffice中文官网
libreoffice中国镜像

一、下载

libreoffice中国镜像

a. 安装包 LibreOffice_6.4.3_Linux_x86-64_rpm.tar.gz
b. 用户界面语言包 LibreOffice_6.4.3_Linux_x86-64_rpm_langpack_zh-CN.tar.gz

二、解压

tar -zxvf LibreOffice_6.4.3_Linux_x86-64_rpm.tar.gz

三、安装

libreoffice的安装

3.1 进入解压目录安装

cd RPMS/
yum localinstall *.rpm
默认安装到/opt/libreoffice6.4/program/

3.2 启动

进入安装目录
cd /opt/libreoffice6.4/program/

【注】每个参数前是--(双横岗)而不是-
执行下面命令即可启动
libreoffice6.4 --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &

image.png

3.3 使用

【问题一】转换耗时太长,30秒以上
后来发现是没有安装字体库,如果设置中文和字体库,设置后转换的就快了

【问题二】中文变成了框框
Centos7下使用LibreOffice转PDF中文乱码

解决办法一:让linux支持中文(需要改系统语言)(这个方法亲自实践可行)
[linux系统下安装的libreoffice进行文件预览时,中文变成四方框]
(https://blog.csdn.net/qq_42246254/article/details/88366939)

yum groupinstall "fonts"
# 更改内容为LANG=zh_CN.UTF-8(之前为LANG=en_US.UTF-8)
vim /etc/locale.conf
source /etc/locale.conf

libreoffice6.4 --headless --convert-to pdf sun/11.doc --outdir sun

image.png

两种方式
1)通过调用shell执行cmd命令
【libreoffice】libreoffice实现office转pdf、html、jpg等格式数据(shell cmd方式)

  1. libreoffice API
    LibreOffice 6.4 API Documentation

3)通过jodconverter库操作
DocumentConverter (JODConverter 2.2.0 API)

image.png

jobconverter文档详解
jodconverter Getting-Started(github)

Java实现LibreOffice转换PDF文档

【问题一】
officeHome not set and could not be auto-detected

// final LocalOfficeManager officeManager = LocalOfficeManager.install();
// 其中linux不设置officeHome会提示officeHome not set and could not be auto-detected
final LocalOfficeManager officeManager = LocalOfficeManager.builder().officeHome("/opt/libreoffice6.4/").install().build();

jodconverter入门(参考这个博客解决的问题)
【问题二】
word转pdf,使用java调用jodconverter,第一次转换成功,第二次转换失败,提示丢掉连接

解决办法:在finally中停掉soffice进程

try {
    // Start an office process and connect to the started instance (on port 2002).
    officeManager.start();
    // Convert
    JodConverter
            .convert(inputFile)
            .to(outputFile)
            .execute();
} catch(Exception e){
    logger.debug("转换失败", e.getMessage());
} finally {
    // Stop the soffice process
    OfficeUtils.stopQuietly(officeManager);
}

四、总结

经比较,libreoffice比openoffice转换的更好,而且安装操作更方便,但是耗时更长,建议使用libreoffice

五、参考

libreoffice安装以及命令行使用
libreoffice实现office转pdf、html、jpg等格式数据(写的比较详细)

Java 利用LibreOffice将Office文档转换成 PDF,进而转图片,实现在线预览功能(知乎)
jodconverter4.1.0版本改进解析
SpringBoot使用LibreOffice转换PDF
jodconverter Maven仓库

【注】
也可以关注一下
collaboraoffice
Linux安装Collabora Online让NextCloud支持Office在线编辑

上一篇下一篇

猜你喜欢

热点阅读