openOffice 文档转PDF
windows平台

安装-开启服务
进入安装目录 如:C:\Program Files (x86)\OpenOffice 4\program 执行命令
| ./soffice -accept="socket,host=0.0.0.0,port=8100,tcpNoDelay=1;urp;" -nofirststartwizard |
端口监听命令
| netstat -ano|findstr 8100 |
linux平台
下载

上传 安装
1.解压 进入 zh-Ch 目录 ->RPMS 目录
| tar -zxvf Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz |
| |
| cd zh-Ch |
| |
| cd RPMS |
| |
2.安装rpm 文件
3.进入desktop-integration/目录
4.安装openoffice
| rpm -ivh openoffice4.1.11-redhat-menus-4.1.11-9808.noarch.rpm |
安装成功后会在/opt下出现一个openoffice4文件

5.启动openofiice4 :
临时启动
| /opt/openoffice4/program/soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard |
后台启动
| /opt/openoffice4/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard & |
服务器字体安装
1、安装字体库。
| yum -y install fontconfig |
安装完成后,在 /usr/share/ 目录下会新增 fonts 目录。
2、进入 fonst 目录在此新建一个chinese,导入字体文件。
| mkdir chinses |
| chmod -R 755 /usr/share/fonts/chinses/ |
把自己Windows系统(C:\Windows\Fonts )中的相关字库添加至 /usr/share/fonts/chinses/ 下面!
| cd /usr/share/fonts/chinses/ |
3、安装搜索目录字库。
4、进入 fonts.conf 进行文件修改编辑。
| vim /etc/fonts/fonts.conf |
| 在<dir prefix="xdg">fonts</dir> 这一行下面加入: |
| <dir>/usr/share/fonts/chinese</dir> |
5、建立字体缓存:
| chmod -R 755 /usr/share/fonts/chinese/ |
| cd /usr/share/fonts/chinese |
| mkfontscale (若提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale ) |
| mkfontdir(若提示 fc-cache: command not found,则需要安装# yum install fontconfig ) |
| fc-cache -fv |
最后再次通过fc-list看一下字体列表相关字体是否添加成功
注:重启用到字体库的服务,将字体库应用到服务中去
服务器转换服务
maven 依赖引入
| <dependency> |
| <groupId>com.artofsolving</groupId> |
| <artifactId>jodconverter</artifactId> |
| <version>2.2.1</version> |
| </dependency> |
| <dependency> |
| <groupId>org.jodconverter</groupId> |
| <artifactId>jodconverter-spring-boot-starter</artifactId> |
| <version>4.3.0</version> |
| </dependency> |
工具类代码
| package com.woma.utils; |
| |
| import org.artofsolving.jodconverter.OfficeDocumentConverter; |
| import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; |
| import org.artofsolving.jodconverter.office.OfficeManager; |
| |
| import java.io.File; |
| import java.io.FileNotFoundException; |
| |
| |
| |
| public class OpenOfficePdfConvert { |
| |
| |
| |
| |
| private static OfficeManager officeManager; |
| private static String OFFICE_HOME = "C:/Program Files (x86)/OpenOffice 4/"; |
| private static int port[] = { 8100 }; |
| |
| public void convert2PDF(String inputFile, String outputFile) throws FileNotFoundException { |
| |
| startService(); |
| System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile); |
| |
| OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); |
| converter.convert(new File(inputFile), new File(outputFile)); |
| |
| stopService(); |
| System.out.println(); |
| |
| } |
| |
| |
| public static void startService() { |
| DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); |
| try { |
| System.out.println("准备启动服务...."); |
| configuration.setOfficeHome(OFFICE_HOME); |
| configuration.setPortNumbers(port); |
| configuration.setTaskExecutionTimeout(1000 * 60 * 5L); |
| configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L); |
| |
| officeManager = configuration.buildOfficeManager(); |
| officeManager.start(); |
| System.out.println("office转换服务启动成功!"); |
| } catch (Exception ce) { |
| System.out.println("office转换服务启动失败!详细信息:" + ce); |
| } |
| } |
| |
| |
| public static void stopService() { |
| System.out.println("关闭office转换服务...."); |
| if (officeManager != null) { |
| officeManager.stop(); |
| } |
| System.out.println("关闭office转换成功!"); |
| } |
| |
| public static void main(String[] args) throws Exception { |
| String path = "D:/Gongcheng/xiangmu/springbootDemo/download/"; |
| OpenOfficePdfConvert opc = new OpenOfficePdfConvert(); |
| opc.convert2PDF(path+"1.docx", path+"1.pdf"); |
| } |
| |
| } |
使用方法
| @Test |
| void demo2(){ |
| String fromPath="D:/Gongcheng/xiangmu/springbootDemo/download/极简新中式PPT模板.ppt"; |
| String toPath="D:/Gongcheng/xiangmu/springbootDemo/upload/"; |
| OfficeToPDF officeToPDF = new OfficeToPDF(); |
| officeToPDF.convert(fromPath,toPath,"极简新中式PPT模板"); |
| } |
本地转换服务(旧)
maven 依赖引入
| <dependency> |
| <groupId>com.google.guava</groupId> |
| <artifactId>guava</artifactId> |
| <version>19.0</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.github.livesense</groupId> |
| <artifactId>jodconverter-core</artifactId> |
| <version>1.0.5</version> |
| </dependency> |
工具类代码
| import java.io.File; |
| import java.io.FileNotFoundException; |
| |
| import org.artofsolving.jodconverter.OfficeDocumentConverter; |
| import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; |
| import org.artofsolving.jodconverter.office.OfficeManager; |
| |
| |
| |
| public class OpenOfficePdfConvert { |
| |
| |
| |
| |
| private static OfficeManager officeManager; |
| private static String OFFICE_HOME = "C:/Program Files (x86)/OpenOffice 4/"; |
| private static int port[] = { 8100 }; |
| |
| public void convert2PDF(String inputFile, String outputFile) throws FileNotFoundException { |
| |
| startService(); |
| System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile); |
| |
| OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); |
| converter.convert(new File(inputFile), new File(outputFile)); |
| |
| stopService(); |
| System.out.println(); |
| |
| } |
| |
| |
| public static void startService() { |
| DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); |
| try { |
| System.out.println("准备启动服务...."); |
| configuration.setOfficeHome(OFFICE_HOME); |
| configuration.setPortNumbers(port); |
| configuration.setTaskExecutionTimeout(1000 * 60 * 5L); |
| configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L); |
| |
| officeManager = configuration.buildOfficeManager(); |
| officeManager.start(); |
| System.out.println("office转换服务启动成功!"); |
| } catch (Exception ce) { |
| System.out.println("office转换服务启动失败!详细信息:" + ce); |
| } |
| } |
| |
| |
| public static void stopService() { |
| System.out.println("关闭office转换服务...."); |
| if (officeManager != null) { |
| officeManager.stop(); |
| } |
| System.out.println("关闭office转换成功!"); |
| } |
| |
| |
| public static void main(String[] args) throws Exception { |
| String path = "C:/Users/johnny/Desktop/文档/20170420/test/001/"; |
| OpenOfficePdfConvert opc = new OpenOfficePdfConvert(); |
| opc.convert2PDF(path+"1.docx", path+"1.pdf"); |
| } |
| |
| } |
使用方法
| @Test |
| void demo() throws FileNotFoundException { |
| String path = "D:/Gongcheng/xiangmu/springbootDemo/download/"; |
| OpenOfficePdfConvert opc = new OpenOfficePdfConvert(); |
| opc.convert2PDF(path+"黑白几何体.pptx", path+"黑白几何体.pdf"); |
| } |
本文作者:-Watcher-
本文链接:https://www.cnblogs.com/womaspace/p/18635408
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步