SpringBoot使用LibreOffice转换PDF

2023-05-16

用LibreOffice将Office文档转换PDF

本例使用 LibreOffice-6.0.4jodconverter-4.2.0spring-boot-1.5.9.RELEASE
CentOS7 + openJDK8Windows7 + jdk1.8 环境下测试通过。

LibreOffice转换命令

# 源文件放在 e:\tmp\123.docx 或者 /tmp/123.docx

# windows
soffice.exe --headless --invisible --convert-to pdf e:\tmp\123.docx --outdir e:\tmp

# linux
/usr/bin/libreoffice6.0 --headless --invisible --convert-to pdf /tmp/123.docx --outdir /tmp

jodconverter封装了一组转换命令,通过java调用LibreOffice相关服务。

pom依赖

<dependency>
  <groupId>org.jodconverter</groupId>
  <artifactId>jodconverter-core</artifactId>
  <version>4.2.0</version>
</dependency>
<dependency>
  <groupId>org.jodconverter</groupId>
  <artifactId>jodconverter-local</artifactId>
  <version>4.2.0</version>
</dependency>
<dependency>
  <groupId>org.jodconverter</groupId>
  <artifactId>jodconverter-spring-boot-starter</artifactId>
  <version>4.2.0</version>
</dependency>
<dependency>
  <groupId>org.libreoffice</groupId>
  <artifactId>ridl</artifactId>
  <version>5.4.2</version>
</dependency>

注意: 在这里说明特别一下,jodconverter4.2开始,对LibreOffice相关功能从jodconverter-core中分离出来,封装到为jodconverter-local,另外新增了jodconverter-online,支持LibreOffice online server的远程调用。

配置 application.properties

jodconverter.local.enabled=true
# 设置LibreOffice主目录
jodconverter.local.office-home=${pom.office.home}
# 开启多个LibreOffice进程,每个端口对应一个进程
jodconverter.local.portNumbers=8100,8101,8102
# LibreOffice进程重启前的最大进程数
jodconverter.local.maxTasksPerProcess=100

使用Maven的多环境配置

<profiles>
  <profile>
    <!-- windows环境 -->
    <id>win</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <pom.office.home>C:/Program Files/LibreOffice</pom.office.home>
    </properties>
  </profile>
  <profile>
    <!-- linux环境 -->
    <id>linux</id>
    <properties>
      <pom.office.home>/opt/libreoffice6.0</pom.office.home>
    </properties>
  </profile>
</profiles>

调用方法

import org.jodconverter.DocumentConverter;

@Resource
private DocumentConverter documentConverter;

// 具体转换方法,参数是java.io.File
documentConverter.convert(sourceFile).to(targetFile).execute();
  • convert方法 接受参数 java.io.Filejava.io.InputStream
  • to方法 接受参数 java.io.Filejava.io.OutputStream

在线预览

使用开源项目 https://github.com/mozilla/pdf.js
下载最新的release包(pdfjs-x.y.z-dist.zip)

pdfjs\web\viewer.html传入参数file(示例:http://localhost:8080/pdfjs/web/viewer.html?file=xxxxxxx.pdf

CentOS7安装LibreOffice

官网
https://zh-cn.libreoffice.org/

科大镜像
http://mirrors.ustc.edu.cn/td...

中文语言包
http://mirrors.ustc.edu.cn/td...

下载安装包

wget -P /tmp/office http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.4/rpm/x86_64/LibreOffice_6.0.4_Linux_x86-64_rpm.tar.gz

wget -P /tmp/office http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.4/rpm/x86_64/LibreOffice_6.0.4_Linux_x86-64_rpm_langpack_zh-CN.tar.gz

解压缩

tar zxvf /tmp/office/LibreOffice_6.0.4_Linux_x86-64_rpm.tar.gz -C /tmp/office

tar zxvf /tmp/office/LibreOffice_6.0.4_Linux_x86-64_rpm_langpack_zh-CN.tar.gz -C /tmp/office

检查安装包

ll /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm/RPMS/*.rpm

ll /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm

用yum安装,不要执行install

yum install /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm/RPMS/*.rpm

yum install /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm

安装libcairo.so.2依赖库

yum install ibus

查找服务目录

安装路径:/opt/libreoffice6.0
快捷方式:/usr/bin/libreoffice6.0

启动服务

/usr/bin/libreoffice6.0 --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard

CentOS7安装字体库

在CentOS7服务器上,利用LibreOffice将word等格式转换为PDF,发现不支持汉字。需要安装字体库。

安装fontconfig

yum -y install fontconfig

安装完成后,/usr/share目录就可以看到fonts和fontconfig两个目录。

安装ttmkfdir

yum -y install ttmkfdir

检查已有字体库

fc-list

复制字体

#新建文件夹
mkdir /usr/share/fonts/chinese

把Windows系统的字体C:\Windows\Fonts复制进去。

  • simsun.ttc 宋体
  • simhei.ttf 黑体
  • msyh.ttf 微软雅黑
  • msyhbd.ttf 微软雅黑
# 修改字体权限
chmod -R 644 /usr/share/fonts/chinese

汇总生成fonts.scale文件

ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

修改字体配置文件

vim /etc/fonts/fonts.conf

修改内容

<fontconfig>
  ....
  <dir>....
  <dir>/usr/share/fonts/chinese</dir>
  ....
</fontconfig>

更新字体缓存

fc-cache -fv
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot使用LibreOffice转换PDF 的相关文章

随机推荐

  • windows下nvm安装node之后npm命令找不到问题解决办法

    主要关键解解决办法 xff1a 61 61 61 适 用于所有东西的安装 安装有关环境配置类的软件及其他 xff0c 一般情况下切记不要安装到c盘programfiles下 xff0c 否则会出现各种问题的报错 xff01 xff01 xf
  • [问题2014S14] 复旦高等代数II(13级)每周一题(第十四教学周)

    问题2014S14 设 V 为酉空间 证明 不存在 V 上的非零线性变换 varphi 使得对 V 中任一向量 v 均有 varphi v v 61 0 注 本题是复旦高代教材 P326 习题 9 1 5 的推广
  • 8B10B编解码及FPGA实现

    概述 在使用ALTERA的高速串行接口时 xff0c GXB模块里硬件实现了8B10B编码 xff0c 用户只是 傻瓜 式的使用 xff0c 笔者也一直没有弄清楚 网上搜索了一些学习资料 xff0c 结合参考文献希望能够对其进行消化 另外
  • 零碎记录- spring security oauth2 资源服务器中设置放行路径

    在资源服务器配置类中重写configure方法 xff0c 添加放行信息 使用了 64 EnableResouceServer xff0c 且继承了ResourceServerConfigurerAdapter的类作为资源服务器配置信息类
  • Golang 新手可能会踩的 50 个坑

    译文 xff1a Golang 新手可能会踩的 50 个坑 原文 xff1a 50 Shades of Go Traps Gotchas and Common Mistakes 翻译已获作者授权 xff0c 转载请注明来源 不久前发现在知乎
  • 手把手教你使用TF服务将TensorFlow模型部署到生产环境 ...

    介 绍 将机器学习 xff08 ML xff09 模型应用于生产环境已成为一个火热的的话题 许多框架提供了旨在解决此问题的不同解决方案 为解决这一问题 xff0c 谷歌发布了TensorFlow xff08 TF xff09 服务 xff0
  • Centos/Linux下如何查看网关地址/Gateway地址

    Centos Linux下如何查看网关地址 Gateway地址 xff1f Linux下查看网关的命令还是很多的 xff0c 不过如果IP是DHCP获取 xff0c 那么有些命令是不适用的 xff0c 当然也有通用的查询网关命令 1 ifc
  • 教你Java生成文件时怎么设置编码格式

    OutputStreamWriter允许用户指定编码方式 xff0c 代码为 xff1a FileInputStream fis 61 new FileInputStream 34 文件路径 34 xff1b OutputStreamWri
  • One input and More output use 'tee'

    2008 05 22 In preparation for my Luma media player I wanted to create a simple audio player with visualization Based upo
  • 简单的任务调度(自整理)

    为什么80 的码农都做不了架构师 xff1f gt gt gt 任务调度 使用 Quartz 框架实现 1 8 6 的版本 开源框架 什么是任务调度 xff1a 即是某个时间点做某件时间 xff01 核心有是什么 xff1a 以时间为关注点
  • Laravel5.5集成七牛云上传、管理(删除、查询)

    一 为什么使用七牛云存储 1 使用七牛带宽和CDN xff0c 速度快 xff0c 不占用开发者服务器 2 支持图片任意格式 任意分辨率自动生成 xff0c 可以用来做图片服务器 3 小流量免费 xff1a 存储空间 10GB xff0c
  • centos为用户添加sudo功能

    su chmod a 43 w etc sudoers vim etc sudoers 找到root ALL 61 ALL ALL这行 复制出新的一行 xff0c 并且将root改为daniel xff08 当前用户名 xff09 chmo
  • Reverse Linked List

    本题是反转一个单链表 xff0c 题目提示使用迭代和递归两种方式 xff0c 属于比较基础的题目 一 迭代方式 xff1a 总体思路是从左到右遍历链表结点 xff0c 依次反转连接关系 每次处理相邻的两个结点 xff0c 从 lt None
  • angularJS1笔记-(17)-ng-bind-html指令

    angular不推荐大家在绑定数据的时候绑定html 但是如果你非要这么干也并不是不可以的 举个例子 xff1a lt DOCTYPE html gt lt html lang 61 34 en 34 gt lt head gt lt me
  • 旧电脑diy文件服务器,2021旧电脑自制NAS存储变废为宝.docx

    amp tid 61 13992 amp page 61 1 amp extra 61 pid15059 amp tid 61 13992 amp page 61 1 amp extra 61 pid15059 旧电脑自制NAS存储变废为宝
  • Druid 连接池 JDBCUtils 工具类的使用

    Druid工具介绍 它不仅仅是一个数据库连接池 xff0c 它还包含一个ProxyDriver xff0c 一系列内置的JDBC组件库 xff0c 一个SQL Parser 支持所有JDBC兼容的数据库 xff0c 包括Oracle MyS
  • matlab练习程序(简单图像融合)

    通过本篇和上一篇的结合 xff0c 应该就能做出拉普拉斯图像融合了 这里用的方法很简单 xff0c 就是用模板和两个图像相乘 xff0c 然后对处理后的两个图像再相加就可以了 拉普拉斯融合就是对金字塔的每一层图像做这样的操作 xff0c 然
  • Ext.grid.Panel表格分页

    Ext grid Panel表格分页示例 代码 xff1a cshtml 64 Layout 61 null lt DOCTYPE html gt lt html gt lt head gt lt title gt Ext grid Pan
  • Build a Multiple Choices Cascader by ant-design-vue

    Preface I need to make a cascader which can support multiple choices However I didn 39 t find any ui which supports that
  • SpringBoot使用LibreOffice转换PDF

    用LibreOffice将Office文档转换PDF 本例使用 LibreOffice 6 0 4 jodconverter 4 2 0 spring boot 1 5 9 RELEASE 在CentOS7 43 openJDK8 和 Wi