溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java使用openOffice對(duì)于word的轉(zhuǎn)換及遇到的問題解決

發(fā)布時(shí)間:2020-09-09 14:50:47 來源:腳本之家 閱讀:387 作者:曾將 欄目:編程語(yǔ)言

一:需求詳情:

OpenOffice.org 是一套跨平臺(tái)的辦公室軟件套件,能在 Windows、Linux、MacOS X (X11)、和 Solaris 等操作系統(tǒng)上執(zhí)行。它與各個(gè)主要的辦公室軟件套件兼容。OpenOffice.org 是自由軟件,任何人都可以免費(fèi)下載、使用、及推廣它。

公司需要存儲(chǔ)合同文件,用戶上傳word文檔的合同,通過openOffice去把word轉(zhuǎn)換為pdf、再把pdf轉(zhuǎn)換為圖片格式,并分別存儲(chǔ)。因?yàn)閛penOffice的轉(zhuǎn)換需要耗費(fèi)挺大的內(nèi)存,所以設(shè)計(jì)為task任務(wù),凌晨自動(dòng)轉(zhuǎn)換。

記錄本次需求完成的時(shí)候遇到的問題。

openoffice既有windows版本也有l(wèi)inux版。不用擔(dān)心生產(chǎn)環(huán)境是linux系統(tǒng)。

關(guān)于linux系統(tǒng)安裝openoffice軟件請(qǐng)參照:點(diǎn)擊這里

二:過程

1:本地環(huán)境編碼(windows)

第一步:因?yàn)槭潜镜丨h(huán)境的編碼而且是Windows環(huán)境,所以從安裝openOffice開始,到啟動(dòng)服務(wù)并沒有遇到難題。

第二步:轉(zhuǎn)換所需要的工具包;

 <dependency>
 <groupId>commons-cli</groupId>
 <artifactId>commons-cli</artifactId>
 <version>1.2</version>
 </dependency>
 
 <dependency>
 <groupId>commons-io</groupId>
 <artifactId>commons-io</artifactId>
 <version>1.4</version>
 </dependency>
 
 <dependency>
 <groupId>org.openoffice</groupId>
 <artifactId>juh</artifactId>
 <version>3.0.1</version>
 </dependency>
 
 <dependency>
 <groupId>org.openoffice</groupId>
 <artifactId>jurt</artifactId>
 <version>3.0.1</version>
 </dependency>
 
 <dependency>
 <groupId>org.openoffice</groupId>
 <artifactId>ridl</artifactId>
 <version>3.0.1</version>
 </dependency>
 
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-jdk14</artifactId>
 <scope>test</scope>
 </dependency>
 
 <dependency>
 <groupId>org.openoffice</groupId>
 <artifactId>unoil</artifactId>
 <version>3.0.1</version>
 </dependency>
 
 <dependency>
 <groupId>com.thoughtworks.xstream</groupId>
 <artifactId>xstream</artifactId>
 <version>1.3.1</version>
 </dependency>
 
 <dependency>
 <groupId>org.apache.pdfbox</groupId>
 <artifactId>fontbox</artifactId>
 <version>2.0.8</version>
 </dependency>
 
 <dependency>
 <groupId>org.apache.pdfbox</groupId>
 <artifactId>pdfbox</artifactId>
 <version>2.0.8</version>
 </dependency>

問題1:在這里遇到了第一個(gè)問題,就是在maven的中央倉(cāng)庫(kù)找不到關(guān)鍵的依賴jar包的問題。

jodconverter-cli   這個(gè)jar包中央倉(cāng)庫(kù)找不到j(luò)ar包依賴,jodconverter 版本才到2.2.1(這個(gè)版本之前的不能支持docx格式轉(zhuǎn)換,2.2.2及以后才開始支持。)

然后和大牛商量,加入到公司內(nèi)網(wǎng)自己的maven倉(cāng)庫(kù)。

第三步:工具類

 /**
 * @author GH
 * 輸入文件
 * 輸出文件
 */
 public class WordToPdf {//word轉(zhuǎn)pdf
 public static void docToPdf(File inputFile, File outputFile){
 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
 try{
 connection.connect();
 DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
 converter.convert(inputFile, outputFile);
 }catch(ConnectException cex){
 cex.printStackTrace();
 }finally{
 if(connection!=null){
 connection.disconnect();
 connection = null;
 }
 }
 }
 }
 /**
 * @author GH
 * 參數(shù)1:要裝換的pdf位置
 * 參數(shù)2:轉(zhuǎn)換后的圖片存放位置
 * 參數(shù)3:中間要拼接的名字
 * return:轉(zhuǎn)換后的img名字集合
 */
 public class PdfToImage {//pdf轉(zhuǎn)img
 public static List<String> pdfToImagePath(String srcFile,String contractFromSrc,String name){
 List<String> list = new ArrayList<>();
 String imagePath;
 File file = new File(srcFile);
 try {
 File f = new File(contractFromSrc);
 if(!f.exists()){
 f.mkdir();
 }
 PDDocument doc = PDDocument.load(file);
 PDFRenderer renderer = new PDFRenderer(doc);
 int pageCount = doc.getNumberOfPages();
 for(int i=0; i<pageCount; i++){
 // 方式1,第二個(gè)參數(shù)是設(shè)置縮放比(即像素)
 // BufferedImage image = renderer.renderImageWithDPI(i, 296);
 // 方式2,第二個(gè)參數(shù)是設(shè)置縮放比(即像素)
 BufferedImage image = renderer.renderImage(i, 2f); //第二個(gè)參數(shù)越大生成圖片分辨率越高,轉(zhuǎn)換時(shí)間也就越長(zhǎng)
 imagePath = contractFromSrc+name+"-"+i +".jpg";
 ImageIO.write(image, "PNG", new File(imagePath));
 list.add(name+"-"+i +".jpg");
 }
 doc.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return list;
 }
 }

第四步:編碼

首先從數(shù)據(jù)庫(kù)讀取沒有轉(zhuǎn)換過的集合,循環(huán)下載oss對(duì)象存儲(chǔ)文件到指定臨時(shí)文件夾。

通過工具類轉(zhuǎn)換下載的word為pdf,錄入數(shù)據(jù)pdf記錄,上傳oss對(duì)象pdf圖片。

通過工具類轉(zhuǎn)換得到的pdf圖片,錄入數(shù)據(jù)路圖片記錄,上傳轉(zhuǎn)換得到的img圖片。

try catch捕捉異常,有異常就回滾數(shù)據(jù)庫(kù),刪除oss對(duì)象上傳的文件。

修改word的轉(zhuǎn)換狀態(tài)為已轉(zhuǎn)換。

問題2:因?yàn)榈阶詈鬁y(cè)試環(huán)境和生產(chǎn)環(huán)境都是Linux系統(tǒng)的,因?yàn)樯婕暗轿募牟僮鳎荓inux和Windows的文件路徑是不一樣的,例如:Windows文件路徑為(C:\tmp\test.txt)Linux則為(/tmp/test.txt)

因此 采用這種方式

   public final static String Convert_Tmp_Url="C:"+File.separator+"temp"+File.separator+"contractToImg"+File.separator;//進(jìn)行word——img轉(zhuǎn)換的時(shí)候的暫時(shí)存放路徑 window
 public final static String Convert_Tmp_Url2=File.separator+"tmp"+File.separator+"contractToImg"+File.separator;//進(jìn)行word——img轉(zhuǎn)換的時(shí)候的暫時(shí)存放路徑 linux

File.separator 與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符,為了方便,它被表示為一個(gè)字符串 在Linux此字段的值為 '/' Windows為'\'

第五步:本地測(cè)試,沒有問題。

2:測(cè)試環(huán)境測(cè)試(windows)

問題3:在Linux環(huán)境下word轉(zhuǎn)換word中文出現(xiàn)亂碼 空白,導(dǎo)致的原因是Linux缺少中文字體編碼。

解決方法:

步驟1:創(chuàng)建路徑。

在centos的/usr/java/jdk1.8.0_91/jre/lib/fonts下新建路徑:fallback。

步驟2:上傳字體。

將字體:simhei.ttf 黑體、simsun.ttc 宋體(windows下通過everything找下)上傳至/usr/java/jdk1.8.0_91/jre/lib/fonts/fallback路徑下。

步驟3:查看系統(tǒng)字體文件路徑。

查看方案:

[root@80ec6 fallback]# 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>

步驟4:字體拷貝。

將 /usr/java/jdk1.8.0_91/jre/lib/fonts的全部?jī)?nèi)容,拷貝到步驟3查看的路徑下, 我的字體路徑為:/usr/share/fonts。

步驟5:更新緩存

執(zhí)行命令:fc-cache

步驟6:kill掉openoffice進(jìn)程。

  [root@80ec6 fonts]# ps -ef | grep openoffice

  root 3045 3031 0 06:19 pts/1 00:00:03 /opt/openoffice4/program/soffice.bin -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard

執(zhí)行kill:kill -9 3045

步驟7:重啟后臺(tái)運(yùn)行openoffice。

 [root@a3cf78780ec6 openoffice4]# soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

3:測(cè)試環(huán)境和生產(chǎn)環(huán)境內(nèi)核不一樣,安裝的安裝包不一樣。

測(cè)試環(huán)境的安裝的是deb文件,使用 dpkg命令安裝所有的deb文件,啟動(dòng)服務(wù)就能使用。

生產(chǎn)環(huán)境的是dpkg命令找不到。改換安裝prm文件,執(zhí)行安裝之后,竟然啟動(dòng)不了,查找原因之后盡然是沒有安裝完,RPMS目錄下有desktop-integration文件夾,進(jìn)入到desktop-integration目錄,里面有四個(gè)rpm  文件,選擇相應(yīng)的安裝即可,這里我選擇的是redhat版本。

執(zhí)行 rpm -ivh openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI