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;
}
}