LibreOffice转 命令 操作 word excel ppt 图片也可以 兼容03 和07 直接下载可用

package com.zjqy.qbcs.util;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 将word文档通过java调用命令的方式转化成HTML
 */
public class DocConvertDemo {
 /**
         * 必须去官网下载一个LibreOffice 安装到电脑上才能行了 linux或者windos都有
         * 有一个包下载不下来,我就手动引入了
         *  <!-- jodconverter-core -->
         *         <!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
         *         <dependency>
         *             <groupId>org.artofsolving.jodconverter</groupId>
         *             <artifactId>jodconverter-core</artifactId>
         *             <version>3.0-beta-4</version>
         *             <scope>system</scope>
         *             <systemPath>${project.basedir}/src/main/resources/lib/jodconverter-core-3.0-beta-4.jar</systemPath>
         *         </dependency>
         *         <dependency>
         *             <groupId>commons-cli</groupId>
         *             <artifactId>commons-cli</artifactId>
         *             <version>1.1</version>
         *         </dependency>
         *         <dependency>
         *             <groupId>org.openoffice</groupId>
         *             <artifactId>juh</artifactId>
         *             <version>3.2.1</version>
         *         </dependency>
         *         <dependency>
         *             <groupId>org.openoffice</groupId>
         *             <artifactId>unoil</artifactId>
         *             <version>3.2.1</version>
         *         </dependency>
         *         <!-- jodconverter-core -->
         */
    public static void main(String[] args) throws IOException {
        /**
         * D:\soft\LibreOffice_6.0.6\program\soffice:表示libreoffice安装路径
         * D:\Desktop\DocCloud\testDir\hadoopInstall.doc:表示要转化的word文件
         */
        String fileName= "D:\\aa.txt";
        String command = "C:\\Program Files\\LibreOffice\\program\\soffice --headless --invisible --convert-to html "+fileName;

        /**
         *workDir:表示转化之后的HTML文件保存的路径地址
         */
        String workDir = "D:\\cache\\"+UUID.randomUUID().toString()+"\\";
        File file = new File(workDir);
        //创建目录--因为是UUID所以不用判断目录一定不存在
        file.mkdirs();
        /**
         * command:命令
         * null:操作系统运行程序时通过envp 参数将系统环境变量传递给程序
         * file:命令在那个路径下执行
         */
        //返回过程对象--Process
        Process exec = Runtime.getRuntime().exec(command,null,file);
        //错误信息
        InputStream errorStream = exec.getErrorStream();
        //结果信息
        InputStream inputStream = exec.getInputStream();
        //IOUtils-直接将流转化成字符串
        String error = IOUtils.toString(errorStream);
        String result = IOUtils.toString(inputStream);
        //打印信息
        System.out.println(error);
        System.out.println(result);
    }
}

 

 

package com.zjqy.qbcs.controller;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

import java.io.*;
import java.util.Date;

public class Baa {

    public static void main(String[] args) throws Exception {
        /**
         * 必须去官网下载一个LibreOffice 安装到电脑上才能行了 linux或者windos都有
         * 有一个包下载不下来,我就手动引入了
         *  <!-- jodconverter-core -->
         *         <!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core -->
         *         <dependency>
         *             <groupId>org.artofsolving.jodconverter</groupId>
         *             <artifactId>jodconverter-core</artifactId>
         *             <version>3.0-beta-4</version>
         *             <scope>system</scope>
         *             <systemPath>${project.basedir}/src/main/resources/lib/jodconverter-core-3.0-beta-4.jar</systemPath>
         *         </dependency>
         *         <dependency>
         *             <groupId>commons-cli</groupId>
         *             <artifactId>commons-cli</artifactId>
         *             <version>1.1</version>
         *         </dependency>
         *         <dependency>
         *             <groupId>org.openoffice</groupId>
         *             <artifactId>juh</artifactId>
         *             <version>3.2.1</version>
         *         </dependency>
         *         <dependency>
         *             <groupId>org.openoffice</groupId>
         *             <artifactId>unoil</artifactId>
         *             <version>3.2.1</version>
         *         </dependency>
         *         <!-- jodconverter-core -->
         */

        doDocToFdpLibre();
    }

    /**
     * 只是针对txt文档进行一次转码操作  win上面的直接使用会乱码
     * @param inputFile
     * @return
     */
    public static String getUtf8File(File inputFile){
        //写入到服务器上的一个目录中
        String pathTxt="D:\\ada\\tex.txt";
        File file=new File(pathTxt);
        try(BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(new FileInputStream(inputFile), "gbk"));
            BufferedWriter os =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));) {
            String lineTxt = null;
            while((lineTxt = bufferedReader.readLine()) != null){
//                System.err.println(lineTxt);
                os.write(lineTxt);
                os.newLine();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return pathTxt;
    }


    public static String doDocToFdpLibre(){
        /**
         * txt 我是用io流转码一次操作 就不乱码了
         */
//        File inpu1 = new File("D:\\ppt\\asd.txt");
//        String sadsa = getUtf8File(inpu1);
//        File inputFile = new File(sadsa);
        /**
         * word
         */
//         File inputFile = new File("D:\\ppt\\s03ad.doc");
//         File inputFile = new File("D:\\ppt\\sad.docx");
        /**
         * excel 有图片也行
         */
//         File inputFile = new File("D:\\ppt\\as03d.xls");
//         File inputFile = new File("D:\\ppt\\asd.xlsx");
        /**
         * ppt
         */
//         File inputFile = new File("D:\\ppt\\a03a.ppt");
//         File inputFile = new File("D:\\ppt\\asdas.pptx");
        /**
         * 图片
         */
         File inputFile = new File("d:/asdas.gif");
        System.out.println("libreOffice开始转换..............................");
        Long startTime = new Date().getTime();
        // txt:使用libreOffice来转换pdf,转换成功,但是中文有乱码!!!!!
        // doc:这是office中的doc文档,可以转换成功,并且中文没有乱码
        //docx:这是office中的docx文档,可以转换成功,并且中文没有乱码
        //ppt和pptx:转换成功。
        //xls:转换成功,没有中文乱码
        //xlsx:转换成功
        //jpg和png:成功
        String libreOfficePath = "C:\\Program Files\\LibreOffice"; //安装的主目录就行了
        // 此类在jodconverter中3版本中存在,在2.2.2版本中不存在
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
        // libreOffice的安装目录
        configuration.setOfficeHome(new File(libreOfficePath));
        // 端口号
        configuration.setPortNumber(8100);
        // configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//
        // 设置任务执行超时为5分钟
        // configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//
        // 设置任务队列超时为24小时
        OfficeManager officeManager = configuration.buildOfficeManager();
        officeManager.start();
        System.out.println("...start.....");
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        File outputFile = new File("d:/jpgLibre.pdf");
        converter.convert(inputFile, outputFile);
        // converter.convert(inputFile, stw, outputFile, pdf); 
        // 转换结束
        System.out.println("转换结束。。。。。");
        String pdfPath = outputFile.getPath();
        long endTime = new Date().getTime();
        long time = endTime - startTime;
        System.out.println("libreOffice转换所用时间为:" + time);
        return pdfPath;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值