Java(springboot)项目引入openoffice

1.引入jar包,使用maven命令安装到本地maven仓库

 

链接:https://pan.baidu.com/s/1_sgMffKIgJkFRCHImt5_vA 提取码:j7mj

<!--openoffice使用所需要的jar包-->

        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.juh</groupId>
            <artifactId>juh</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.jurt</groupId>
            <artifactId>jurt</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.ridl</groupId>
            <artifactId>ridl</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.unoil</groupId>
            <artifactId>unoil</artifactId>
            <version>3.0.1</version>
        </dependency>

2.下载openofficewindow版本和linux版本,也可以去官网下载最新版

链接:https://pan.baidu.com/s/1jQfKxyI0XCPx8T5KlxCJpA 提取码:yxda

链接:https://pan.baidu.com/s/1c5kUf7TNfG_RV3UhIrgvOQ 提取码:1ux0

window版本直接安装,选择默认目录即可。安装完成打开命令行,进入program目录

启动openoffice

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

 

 查看8100端口是否打开,打开说明启动成功

linux版本安装教程

tar -zxvf Apache_OpenOffice_4.1.10_Linux_x86-64_install-rpm_zh-CN.tar.gz
cd zh-CN/
cd RPMS/
rpm -ivh *.rpm
cd desktop-integration/
rpm -ivh openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm

启动:

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

查看端口是否打开

losf -i:8100

注意:linux环境下需要执行以下操作,否则无法识别中文


1.  $JAVA_HOME/jre/lib/fonts/fallback文件夹,没有就新建

将字体:simhei.ttf 黑体、simsun.ttc 宋体(windows系统中有)
上传至$JAVA_HOME/jre/lib/fonts/fallback路径下。
2. 查看系统字体文件路径
cat /etc/fonts/fonts.conf
<!-- Font directory list -->
  <dir>/usr/share/fonts</dir>
  <dir>/usr/share/X11/fonts/Type1</dir> <dir>/usr/share/X11/fonts/TTF</dir> <dir>/usr/local/share/fonts</dir>
  <dir>~/.fonts</dir>
3. $JAVA_HOME/jre/lib/fonts的全部内容复制到usr/share/fonts目录下
4.更新缓存    fc-cache
5.重启openoffice

 1.使用代码连接openoffice进程

	public static int office2PDF(File sourceFile, File destFile) {
            try {
                //连接openoffice服务
                OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
                connection.connect();
                DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                converter.convert(sourceFile, destFile);
                connection.disconnect();
                // 关闭进程
                return 0;
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
    }

2.预览文件

   @RequestMapping(value = {"/review"}, produces = {"application/json; charset=utf-8"})
    public void reviewReport(String id,HttpServletResponse response) throws Exception {
        log.info("文件预览开始id={}",id);
        FileData fileData=fileDataService.selectByPrimaryKey(Integer.valueOf(id));//查看需要预览的文件路径
        File sourceFile = new File(fileData.getFilePath()+"/"+fileData.getFileName());//获取文件
        File destFile = File.createTempFile("temp", ".pdf");//转化成pdf格式
        OfficeUtils.office2PDF(sourceFile, destFile);//源文件输入到pdf
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/pdf");
        InputStream fis = new BufferedInputStream(new FileInputStream(destFile));//放入流
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        //response.reset();
        OutputStream toClient = new BufferedOutputStream((OutputStream)response.getOutputStream());
        toClient.write(buffer);写入到response中
        toClient.flush();
        toClient.close();
        log.info("文件预览结束fileName={}",sourceFile.getName());
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JodConverter 是一个 Java 库,用于将文档从一种格式转换为另一种格式。它支持使用 OpenOffice 或 LibreOffice 进行转换,而不需要安装 Microsoft Office。要使用 JodConverter 进行远程访问 OpenOffice,您需要执行以下步骤: 1. 安装 OpenOffice/LibreOffice 并启动服务。确保您的 OpenOffice/LibreOffice 实例已启动并在运行。 2. 在您的 Java 项目中添加 JodConverter 依赖项。您可以从 Maven 中央存储库下载 JodConverter JAR 文件,或者您可以将其添加到您的 Maven 依赖项中。 3. 在您的 Java 代码中创建一个 OfficeManager 实例,以便您可以连接到正在运行的 OpenOffice/LibreOffice 实例。以下是一个示例代码片段: ``` LocalOfficeManager officeManager = LocalOfficeManager.builder() .officeHome("/path/to/openoffice") .install() .build(); officeManager.start(); ``` 请注意,此代码使用 `LocalOfficeManager` 类,该类将连接到本地运行的 OpenOffice/LibreOffice 实例。如果您需要连接到远程 OpenOffice/LibreOffice 实例,请使用 `RemoteOfficeManager` 类。 4. 将您要转换的文档传递给 JodConverter,然后指定要将其转换为的格式。以下是一个示例代码片段: ``` File inputFile = new File("/path/to/input.docx"); File outputFile = new File("/path/to/output.pdf"); DocumentConverter converter = LocalConverter.builder() .officeManager(officeManager) .build(); converter.convert(inputFile).to(outputFile).execute(); ``` 在此示例中,将 `input.docx` 文件转换为 `output.pdf` 文件。您可以将 `to()` 方法的参数更改为您要转换为的任何格式。 5. 最后,记得在您的代码中关闭 OfficeManager 实例,以便它可以正确地停止并释放资源。以下是一个示例代码片段: ``` officeManager.stop(); ``` 希望这可以帮助您开始使用 JodConverter 远程访问 OpenOffice/LibreOffice 进行文档转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值