用java实现,office文件转成pdf文件

本文介绍如何在Linux环境下使用Java和OpenOffice将Office文件转换为PDF,包括OpenOffice的安装、启动服务、Java实现转换代码,以及解决乱码问题的方法。
摘要由CSDN通过智能技术生成

实现环境:要求可以在linux环境下运行,开源插件 , 跨平台

以下有五种方法:

 

 结论:

office不支持跨平台,

aspose收费,

基于pio+itext转换后会严重失真,

基于libreOffice不支持插入图形,否则会造成文本发生走样

而openOffice各种效果,效率……都是未测的。。。。

总结:目前仅考虑openOffice和libreOffice

openOffice实战

原理:

通过第三方工具openOffice,将word、excel、ppt、txt等文件转换为pdf文件

先安装openOffice软件(Windows或Linux有提供软件)

使用JODConverter的Java的OpenDocument 文件转换器API操作Office系列文件转换为PDF文件

优点:

转换效果比较好。是比较主流的做法

缺点:

服务器需要安装openOffice,比较负重。启动服务时效率不是很高

具体实现:

1.下载安装软件

1)Openoffice:Apache下的一个开放免费的文字处理软件

下载地址:Apache OpenOffice - 下载

2)JODConverter一个Java的OpenDocument 文件转换器,只用到它的jar包

下载地址:JODConverter - Browse /JODConverter at SourceForge.net

安装openoffice

1.安装linux的openoffice

1.Apache OpenOffice - 下载 去官网链接下载linux版本的openOffice 以4.1.2 版本为例。

2.将压缩包上传至服务器上,并进行解压安装。

1  tar -zxvf  对应的压缩包名字
2  cd 进入解压后的 /zh-cn/RPMS
3  yum localinstall *.rpm
4  cd desktop-integration
5  rpm -ivh 含redhat的

  默认会安装在/opt目录下会出现openoffice4。

  3.启动服务

1 /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard  临时启动
2 nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &  后台启动

  端口号根据自己项目实际来确定。

后台启动会一直占用内存,据各路大神说 大概100M,我自己没测过具体值不清楚。

  有的程序是需要预先启动openOffice 服务的,有的则在代码里自己启动服务。

  4.查看进程

netstat -lnp |grep 端口号

  大概显示成这样就算启动完了。

`tcp        0      0 127.0.0.1:8100              0.0.0.0:*                   LISTEN      14362``/soffice``.bin`

2.安装window的openoffice

    比linux的更简单,安装好后,只需要在C:\Program Files (x86)\OpenOffice 4\program执行下面的命令就可以了
    
    // soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststa

linux开机,重启openoffice服务(linux下openoffice启动和自动启动设置(centos)_resolute123的博客-CSDN博客_linux openoffice重启

编辑vi /etc/rc.local

添加如下命令

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

重启linux,命令reboot netstat -lnp |grep 8100 - - - 查看是否会出现以下信息,表示成功 tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 8220/soffice.bin

2.Java实现

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version&g
可以使用LibreOfficeJava API来实现将LibreOffice文档转换为PDF文档。以下是实现的步骤: 1. 首先需要确保LibreOffice已经安装在系统中,并且已经配置好了环境变量。同时需要下载并安装LibreOfficeJava API。 2. 在Java程序中引入LibreOfficeJava API所在的jar包。 3. 使用以下代码将文档转换为PDF文档: ``` import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.lang.XComponent; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class ConvertToPDF { public static void main(String[] args) { XComponentContext xContext = null; try { //获取LibreOffice的上下文 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xContext)); //设置待转换文档的路径和文件名 String fileToConvert = "file:///C:/example.docx"; //设置转换后的PDF文档的路径和文件名 String pdfFile = "file:///C:/example.pdf"; //设置转换参数 PropertyValue[] conversionProperties = new PropertyValue[2]; conversionProperties[0] = new PropertyValue(); conversionProperties[0].Name = "Hidden"; conversionProperties[0].Value = Boolean.TRUE; conversionProperties[1] = new PropertyValue(); conversionProperties[1].Name = "FilterName"; conversionProperties[1].Value = "writer_pdf_Export"; //打开待转换文档 XComponent xComponent = xLoader.loadComponentFromURL(fileToConvert, "_blank", 0, conversionProperties); //将文档转换为PDF格式 PropertyValue[] storeProperties = new PropertyValue[3]; storeProperties[0] = new PropertyValue(); storeProperties[0].Name = "FilterName"; storeProperties[0].Value = "writer_pdf_Export"; storeProperties[1] = new PropertyValue(); storeProperties[1].Name = "Overwrite"; storeProperties[1].Value = Boolean.TRUE; storeProperties[2] = new PropertyValue(); storeProperties[2].Name = "Hidden"; storeProperties[2].Value = Boolean.TRUE; UnoRuntime.queryInterface(XStorable.class, xComponent).storeToURL(pdfFile, storeProperties); //关闭文档 UnoRuntime.queryInterface(XCloseable.class, xComponent).close(Boolean.TRUE); } catch (Exception e) { e.printStackTrace(); } finally { if (xContext != null) { com.sun.star.uno.Runtime.getRuntime(xContext).freeUnusedLibraries(); } } } } ``` 4. 运行程序即可将LibreOffice文档转换为PDF文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值