[Linux服务器] Linux Centos系统安装LibreOffice全过程详解

181 0
Honkers 2025-6-4 04:47:04 | 显示全部楼层 |阅读模式

原文:勿意、勿必、勿固、勿我。
译文:不凭空臆测,不武断绝对,不固执拘泥,不自以为是。

LibreOffice拥有强大的数据导入和导出功能,能直接导入PDF文档、微软Word(.doc文件)、LotusWord,支持主要的OpenXML格式。软件本身并不局限于Debian和Ubuntu平台,现已支持Windows、Mac和其它Linux发行版等多个系统平台。

一、准备工作
1、下载地址:LibreOffice 官网
2、centos系统:作者使用7+版本
3、安装命令:dnf 或更换到 yum命令, 例如:dnf install 变成 yum install
4、LibreOffice文字编码下载:文字编码官网下载地址
进入地址后选择自己需要的版本进入:


选择自己需要的系统进入:https://downloadarchive.documentfoundation.org/libreoffice/old/7.5.9.2/

选择自己需要的位数进入:https://downloadarchive.documentfoundation.org/libreoffice/old/7.5.9.2/rpm/

选择自己需要的文件下载:https://downloadarchive.documentfoundation.org/libreoffice/old/7.5.9.2/rpm/x86_64/

二、安装LibreOffice
下载好的安装包,选择系统匹配的安装包
选择你对应操作系统的包,我这里是Linux的rpm包,建议选择稳定版本


也可查看适配自己系统的历史对应版本下载使用

官网安装指南:官网首页帮助与支持——>安装指南——>选择你自己系统的点击进入

上传至服务器。
centos系统根目录opt文件夹下(根据自己需求上传目录即可,更改目录注意后续命令执行中文件路径)

  1. cd /opt
复制代码

文字编码下载

  1. tar -zxvf LibreOffice_7.5.9_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
复制代码

进入文字编码目录

  1. cd LibreOffice_7.5.9.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/
复制代码

解压文字编码

  1. tar -zxvf LibreOffice_7.5.9.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
复制代码

解压LibreOffice

  1. tar -zxvf LibreOffice_7.5.9.2_Linux_x86-64_rpm.tar.gz
复制代码

进入RPMS目录

  1. cd LibreOffice_7.5.9.2_Linux_x86-64_rpm/RPMS/
复制代码

安装LibreOffice

  1. dnf localinstall *.rpm
复制代码

进入RPMS目录

  1. cd LibreOffice_7.5.9.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/
复制代码

安装字体编码

  1. dnf localinstall *.rpm
复制代码

字体编码和LibreOffice安装命令(要在对应文件目录下执行)

  1. rpm -ivh *.rpm
复制代码

验证是否安装成功

  1. libreoffice7.5 --version
复制代码

成功


卸载LibreOffice

  1. dnf remove libreoffice*
复制代码

四、报错

  1. dnf install cairo -y
  2. dnf install cups-libs -y
  3. dnf install libSM -y
复制代码

更新依赖

  1. dnf install -y libreoffice-headless
复制代码

服务器命令行测试生成PDF

  1. libreoffice7.5 --headless --invisible --convert-to pdf /opt/xxx.docx --outdir /opt
复制代码

转换成功!

解决转换后的PDF中的中文乱码问题
一般在windows下安装我们的LibreOffice成功后,转换中文都是正常的,但在linux上基本上都会出现乱码的问题。 因为linux中缺少一些中文字体导致的。这个时候我们需要把windos中的字体文件夹上传到linux上,同步一下字体信息。

1、打开windows下C:\Windows\fonts目录
2、将fonts目录压缩为zip包,然后上传到服务器上。上传路径为 /usr/share/fonts
3、在linux上解压上传的压缩包,因为是zip包,需要unzip命令。 如果没有要安装

  1. dnf -y install unzip
复制代码

4、输入fc-list 查看系统的字体,如果报错后,需要下载,下载完成后就可以看了

  1. dnf -y install cups-libs fontconfig
复制代码

5、给给解压后产生的文件夹Fonts权限

  1. chmod -R 755 /usr/share/fonts/Fonts
复制代码

6、安装ttmkfdir,然后执行对应命令

  1. dnf -y install ttmkfdir
复制代码
  1. ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
复制代码

7、用vi/vim打开/etc/fonts/fonts.conf,添加我们字体文件的位置

  1. vim /etc/fonts/fonts.conf
复制代码
  1. <dir>/usr/share/fonts/Fonts</dir>
复制代码


8、刷新内存中的字体缓存,输入:

  1. fc-cache
复制代码

9、fc-list查看一下linux系统中的字体, 有我们刚刚添加进去的

  1. fc-list
复制代码

10、再试下转换,发现中文转换成功

Java应用代码

documents4j方式集成LibreOffice
1、引入依赖

  1. <dependency>
  2. <groupId>com.documents4j</groupId>
  3. <artifactId>documents4j-local</artifactId>
  4. <version>1.0.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.documents4j</groupId>
  8. <artifactId>documents4j-transformer-msoffice-word</artifactId>
  9. <version>1.0.3</version>
  10. </dependency>
复制代码

2、实现word转pdf的工具类代码,适用于window系统和Linux系统

  1. import com.documents4j.api.DocumentType;
  2. import com.documents4j.api.IConverter;
  3. import com.documents4j.job.LocalConverter;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.lang3.ObjectUtils;
  6. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  7. import org.jeecg.common.util.DateUtils;
  8. import javax.servlet.http.HttpServletRequest;
  9. import java.io.*;
  10. import java.net.URL;
  11. import java.nio.file.Files;
  12. import java.nio.file.Path;
  13. import java.nio.file.Paths;
  14. import java.nio.file.StandardCopyOption;
  15. @Slf4j
  16. public class PdfUtil {
  17. /**
  18. * 通过documents4j 实现word转pdf
  19. */
  20. public static String documents4jWordToPdf(HttpServletRequest request, String wordPath, String pdfDownloadPath) {
  21. //获取系统类型
  22. String os = System.getProperty("os.name").toLowerCase();
  23. String ym = DateUtils.getDate("yyyyMM");
  24. String filePath = pdfDownloadPath + "/" + ym;
  25. File path = new File(filePath);
  26. // 检查目录是否存在
  27. if (!path.exists()) {
  28. // 创建目录
  29. boolean created = path.mkdirs();
  30. if (created) {
  31. log.info("目录已创建");
  32. } else {
  33. log.info("目录创建失败");
  34. }
  35. } else {
  36. log.info("目录已经存在或文件不在有效目录中");
  37. }
  38. String fileName = wordPath.substring(wordPath.lastIndexOf("/") + 1, wordPath.lastIndexOf("."));
  39. log.info("fileName:{}", fileName);
  40. log.info("当前系统:{}", os);
  41. if (os.contains("win")) {
  42. String pdfPath = pdfDownloadPath + ym + "/" + fileName + ".pdf";
  43. File wordFile = null;
  44. if (ObjectUtils.isNotEmpty(wordPath) && wordPath.indexOf("http") == -1) {
  45. wordFile = new File(wordPath);
  46. } else {
  47. wordFile = getFileByHttpURL(wordPath);
  48. }
  49. // Windows操作系统
  50. winDocuments4jWordToPdf(wordFile, pdfPath);
  51. return org.jeecg.common.util.http.HttpUtil.getServerUrl(request, 10) + "/pdf/" + ym + "/" + fileName + ".pdf";
  52. } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
  53. String pdfPath = pdfDownloadPath + ym + "/";
  54. String wordWritePath = pdfDownloadPath + ym + "/" + fileName + ".docx";
  55. try {
  56. if (ObjectUtils.isNotEmpty(wordPath) && wordPath.indexOf("http") == -1) {
  57. // 创建源文件和目标文件的路径对象
  58. Path sourcePath = Paths.get(wordPath);
  59. Path destinationPath = Paths.get(filePath, sourcePath.getFileName().toString());
  60. Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
  61. } else {
  62. InputStream inputStream = new URL(wordPath).openStream();
  63. // 创建XWPFDocument对象
  64. XWPFDocument document = new XWPFDocument(inputStream);
  65. // 创建输出流,将Word文件写入到本地指定文件夹
  66. FileOutputStream outputStream = new FileOutputStream(wordWritePath);
  67. document.write(outputStream);
  68. outputStream.close();
  69. log.info("Word文件已成功保存到本地!");
  70. }
  71. } catch (IOException e) {
  72. log.info("发生错误: {}" + e.getMessage());
  73. }
  74. File linuxWordPath = new File(wordWritePath);
  75. // Unix/Linux/Mac操作系统
  76. linuxDocuments4jWordToPdf(linuxWordPath, pdfPath);
  77. return org.jeecg.common.util.http.HttpUtil.getServerUrl(request, 10) + "/pdf/" + ym + "/" + fileName + ".pdf";
  78. } else {
  79. // 未知操作系统
  80. throw new RuntimeException("不支持当前操作系统转换文档");
  81. }
  82. }
  83. public static String winDocuments4jWordToPdf(File wordFile, String pdfPath) {
  84. InputStream docxInputStream = null;
  85. OutputStream outputStream = null;
  86. try {
  87. // 原word地址
  88. docxInputStream = new FileInputStream(wordFile);
  89. // 转换后pdf生成地址
  90. outputStream = new FileOutputStream(pdfPath);
  91. IConverter converter = LocalConverter.builder().build();
  92. converter.convert(docxInputStream)
  93. .as(DocumentType.DOCX)
  94. .to(outputStream)
  95. .as(DocumentType.PDF).execute();
  96. // 关闭
  97. converter.shutDown();
  98. // 关闭
  99. outputStream.close();
  100. // 关闭
  101. docxInputStream.close();
  102. return pdfPath;
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. log.info("[documents4J] word转pdf失败:" + e.toString());
  106. } finally {
  107. try {
  108. if (outputStream != null) {
  109. outputStream.close();
  110. }
  111. if (docxInputStream != null) {
  112. docxInputStream.close();
  113. }
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. log.info("[documents4J] word转pdf失败:" + e.toString());
  117. }
  118. }
  119. return "";
  120. }
  121. /**
  122. * 根据URL地址获取文件
  123. *
  124. * @param path URL网络地址
  125. * @return File
  126. */
  127. private static File getFileByHttpURL(String path) {
  128. String newUrl = path.split("[?]")[0];
  129. String[] suffix = newUrl.split("/");
  130. //得到最后一个分隔符后的名字
  131. String fileName = suffix[suffix.length - 1];
  132. File file = null;
  133. InputStream inputStream = null;
  134. OutputStream outputStream = null;
  135. try {
  136. file = File.createTempFile("report", fileName);//创建临时文件
  137. URL urlFile = new URL(newUrl);
  138. inputStream = urlFile.openStream();
  139. outputStream = new FileOutputStream(file);
  140. int bytesRead = 0;
  141. byte[] buffer = new byte[8192];
  142. while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
  143. outputStream.write(buffer, 0, bytesRead);
  144. }
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. } finally {
  148. try {
  149. if (null != outputStream) {
  150. outputStream.close();
  151. }
  152. if (null != inputStream) {
  153. inputStream.close();
  154. }
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. }
  158. }
  159. return file;
  160. }
  161. /**
  162. * 通过documents4j 实现word转pdf -- linux 环境 需要有 libreoffice 服务
  163. *
  164. * @param wordFile 源文件
  165. */
  166. public static String linuxDocuments4jWordToPdf(File wordFile, String pdfPath) {
  167. // 获取文件的绝对路径和目录路径
  168. String absolutePath = wordFile.getAbsolutePath();
  169. String parentPath = wordFile.getParent();
  170. // 构建LibreOffice的命令行工具命令
  171. String commands = "libreoffice7.5 --headless --invisible --convert-to pdf "
  172. + absolutePath + " --outdir " + parentPath;
  173. // 执行转换命令
  174. try {
  175. boolean result = executeLinuxCmd(commands);
  176. if (result) {
  177. // 转换成功,返回转换后的PDF文件
  178. String pdfFilePath = parentPath + File.separator + wordFile.getName().replaceAll("\\.(docx?|\\w+)$", "") + ".pdf";
  179. log.info(pdfFilePath);
  180. return pdfPath;
  181. } else {
  182. return null;
  183. }
  184. } catch (Exception e) {
  185. // 转换失败
  186. log.error("Word文档转换为PDF失败,原因:执行命令时出现异常。", e);
  187. return null;
  188. }
  189. }
  190. /**
  191. * 执行命令行
  192. *
  193. * @param cmd 命令行
  194. * @return
  195. * @throws IOException
  196. */
  197. private static boolean executeLinuxCmd(String cmd) throws IOException {
  198. // 执行命令行工具命令
  199. Process process = Runtime.getRuntime().exec(cmd);
  200. try {
  201. process.waitFor();
  202. } catch (InterruptedException e) {
  203. log.error("执行 Linux 命令异常:", e);
  204. return false;
  205. }
  206. return true;
  207. }
  208. }
复制代码

如有错误,评论指出,作者改正

欢迎大家点赞、转发、评论

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行