SpringMVC中使用OpenOffice文件转换PDF格式实现文件预览

Centos7安装OpenOffice

下载安装包。

点击下载

上传至linux服务器中并解压

tar -zxvf Apache_OpenOffice_4.1.3_Linux_x86-64_langpack-rpm_zh-CN.tar.gz
zh-CN为解压后的文件目录
在这里插入图片描述

cd zh-CN/RPMS

在这里插入图片描述

安装rpm文件

yum localinstall *.rpm

cd desktop-integration目录

rpm -ivh openoffice4.1.3-redhat-menus-4.1.3-9783.noarch.rpm

启动服务

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

使用OpenOffice转换PDF格式

文件转换工具类(直接上代码)

	import java.io.*;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;

public class Doc2HtmlUtil {

    private static String host = "192.168.247.92";

    private static Integer port = 8100;

    /**
     * @param is
     * @param os
     * @param suffix 文件类型
     * @param type   转换的类型:html or pdf
     * @throws Exception
     */
    public static void fileConversion(InputStream is, OutputStream os, String suffix, String type) throws Exception {
        DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
        // 文件格式
        DocumentFormat format = formatReg.getFormatByFileExtension(type);
        if ("doc".equals(suffix) || "docx".equals(suffix)) {
            format = formatReg.getFormatByFileExtension("doc");
        } else if ("xls".equals(suffix) || "xlsx".equals(suffix)) {
            format = formatReg.getFormatByFileExtension("xls");
        } else if ("ppt".equals(suffix) || "pptx".equals(suffix)) {
            format = formatReg.getFormatByFileExtension("ppt");
        }
        DocumentFormat htmlFormat = formatReg.getFormatByFileExtension(type);
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(host, port);
        try {
            connection.connect();
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(is, format, os, htmlFormat);
        } catch (ConnectException e) {
            throw new Exception("OpenOffice连接失败,请检查服务是否启动!");
        } finally {
            connection.disconnect();
        }
    }

    public static void main(String[] args) throws Exception {
        File file = new File("E:\\file\\test.ppt");
        InputStream is = new FileInputStream(file);
        OutputStream os = new FileOutputStream(new File("E:\\file\\test.pdf"));
        Doc2HtmlUtil.fileConversion(is, os, "ppt", "pdf");
        os.close();
        is.close();

    }
}

读取原文件流,转换为PDF文件流

1. 通过url从fastdfs中读取文件流
2. 校验文件格式(根据自己的业务)
3. 调用工具类转换方法
	@Override
    public byte[] readFile(FilesParam param) throws Exception {
        String url = param.getUrl();
        // 获取文件流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        FileRW.downloadAttachment(out, url);
        byte[] bytes = out.toByteArray();
        String fileSuffix = url.substring(url.indexOf(".") + 1);
        if (checkType(fileSuffix)) {
            InputStream is = new ByteArrayInputStream(bytes);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Doc2HtmlUtil.fileConversion(is, os, fileSuffix, "pdf");
            return os.toByteArray();
        }
        return bytes;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值