windows/linux伺服器上java使用openoffice将word文檔轉換為PDF(親測可用)
一、 前言
1. 開發過程中經常會使用java将office系列文檔轉換為PDF, 一般都使用微軟提供的openoffice+jodconverter 實作轉換文檔。
2. openoffice既有windows版本也有linux版。不用擔心生産環境是linux系統。
二、 準備
1. 下載下傳安裝openoffice http://www.openoffice.org/ (路徑随意,因為不管怎麼選路徑,咱們想要的都在"C:/Program Files (x86)/OpenOffice 4/",版本建議選最新的)
2. 安裝完事後 cmd 運作 cd C:\Program Files\OpenOffice.org 3\program>soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard (Linux上自己百度,太多了)
3. 準備jar包(下面這些基本上夠了,至于報錯你就在根據報錯的提示下載下傳吧!)
commons-cli-1.0.jar
commons-io-1.3.1.jar
jodconverter-2.2.1.jar
jodconverter-cli-2.2.1.jar
juh-2.3.0.jar
jurt-2.3.0.jar
ridl-2.3.0.jar
slf4j-api-1.4.3.jar
slf4j-jdk14-1.4.3.jar
unoil-2.3.0.jar
xstream-1.2.2.jar
三、 幹活
1. 主要部分
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
public class WordToPdf {
public static int office2PDF(String sourceFile, String destFile) {
//String OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";// 這裡是OpenOffice的安裝目錄,C:\Program Files (x86)\OpenOffice 4
String OpenOffice_HOME = "C:/Program Files (x86)/OpenOffice 4/";
// 在我的項目中,為了便于拓展接口,沒有直接寫成這個樣子,但是這樣是盡對沒題目的
// 假如從檔案中讀取的URL位址最後一個字元不是 \'\\',則添加\'\\'
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != \'/\') {
OpenOffice_HOME += "/";
}
Process pro = null;
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;// 找不到源檔案, 則傳回-1
}
// 如果目标路徑不存在, 則建立該路徑
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
// 啟動OpenOffice的服務
String command = OpenOffice_HOME
+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;StarOffice.ServiceManager\" -nofirststartwizard";
pro = Runtime.getRuntime().exec(command);
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
//OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
//converter.convert(inputFile,xml,outputFile,pdf);
// close the connection
connection.disconnect();
// 封閉OpenOffice服務的程序
pro.destroy();
return 0;
} catch (FileNotFoundException e) {
e.printStackTrace();
return -1;
} catch (IOException e) {
e.printStackTrace();
} finally {
pro.destroy();
}
return 1;
}
public static void main(String[] args){
// WordToPdf wordToPdf = new WordToPdf();
}
}
2. 測試部分
public static void main(String[]args) throws Exception{
String sourceFile = "E:\\zgtzs.doc";
String destFile = "E:\\pdfdest.pdf";
int i = WordToPdf.office2PDF(sourceFile, destFile);
System.out.println(i);
}
10月12日新增
上面的word轉pdf隻限正規的word轉(如果你的Word是通過freemark等xml模闆轉過來的,那你轉成pdf後是一對xml代碼,建議你用poi轉代碼如下)
1.所需jar包
dom4j-1.6.1.jar
poi-3.12-20150511.jar
poi-examples-3.12-20150511.jar
poi-excelant-3.12-20150511.jar
poi-ooxml-3.12-20150511.jar
poi-ooxml-schemas-3.12-20150511.jar
poi-scratchpad-3.12-20150511.jar
xmlbeans-2.3.0.jar
2. 代碼
public void testWrite() throws Exception {
String templatePath = "E:\\demo\\zgtzs.doc"; //這個是一個doc模闆 樣式完全不會亂
InputStream is = new FileInputStream(templatePath);
HWPFDocument doc = new HWPFDocument(is);
Range range = doc.getRange();
//替換資料(當然如果你要替換的比較多,那建議你寫一個實體類的反射來填充資料)
range.replaceText("${top_year}", new SimpleDateFormat("yyyy").format(new Date()));
range.replaceText("${content_year1}", new SimpleDateFormat("yyyy").format(new Date()));
range.replaceText("${unit}", "北京雲盾科技666");
range.replaceText("${bianHao}", "22");
range.replaceText("${content2}", "asdfasdfasdfassdfasdfasdfaf");
OutputStream os = new FileOutputStream("E:\\demo\\write2.doc");
//把doc輸出到輸出流中
doc.write(os);
os.close();
is.close();
}
3. 至于模闆我還是截個圖吧!
windows/linux伺服器上java使用openoffice将word文檔轉換為PDF(親測可用) - w強哥