期由於項目的需求,需要word文檔轉pdf在線預覽,由於一直沒有接觸這塊,所以花了將近四天時間才弄明白。
寫這篇文章的主要目的是加深自己的記憶,同時方便以后在用。
(最近有使用了這個功能,發現這篇文章有很多問題,已修改,抱歉)
所需要的工具openoffice
在openoffice安裝成功之后,需要在安裝目錄下porgram文件下打開cmd命令行輸入
- soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
這時會啟動兩個openoffice的進程,打開任務管理器就可以看見。
需要的java包 JodConverter 下載地址 http://sourceforge.net/projects/jodconverter/files/JODConverter/
pom.xml
- <!-- https://mvnrepository.com/artifact/com.artofsolving/jodconverter-maven-plugin -->
- <dependency>
- <groupId>com.artofsolving</groupId>
- <artifactId>jodconverter-maven-plugin</artifactId>
- <version>2.2.1</version></dependency>
<!-- https://mvnrepository.com/artifact/com.artofsolving/jodconverter-maven-plugin --> <dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter-maven-plugin</artifactId> <version>2.2.1</version></dependency>
在這里需要詳細講解一下openoffice的安裝,最好是默認安裝路徑
安裝完之后就開始干正事了。、
首先我們要先寫一個上傳頁面(名字隨意,我寫的是jsp頁面)
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.css">
- <link rel="stylesheet" href="/js/bootstrap/css/bootstrap-theme.css">
- <script src="/js/bootstrap/js/jquery.js"></script>
- <script src="/js/bootstrap/js/bootstrap.js"></script>
- <style>
- .fileinput-button {
- position: relative;
- display: inline-block;
- overflow: hidden;
- }
- .fileinput-button input{
- position:absolute;
- right: 0px;
- top: 0px;
- opacity: 0;
- -ms-filter: 'alpha(opacity=0)';
- font-size: 200px;
- }
- </style>
- </head>
- <body style="padding: 10px">
- <form action="/upload" method="post" enctype="multipart/form-data">
- <div align="center">
- <div class="btn btn-success"><table><tr><td width="20%">上傳文件</td></tr></table></div>
- <span class="btn fileinput-button">
- <img src="/img/up55.png" width="40" height="40">
- <input type="file" name = "file" accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
- </span>
- <input type="submit" value="上傳" />
- </div>
- </form>
- <img alt="" src="${msg }">
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.css"> <link rel="stylesheet" href="/js/bootstrap/css/bootstrap-theme.css"> <script src="/js/bootstrap/js/jquery.js"></script> <script src="/js/bootstrap/js/bootstrap.js"></script> <style> .fileinput-button { position: relative; display: inline-block; overflow: hidden; } .fileinput-button input{ position:absolute; right: 0px; top: 0px; opacity: 0; -ms-filter: 'alpha(opacity=0)'; font-size: 200px; } </style> </head> <body style="padding: 10px"> <form action="/upload" method="post" enctype="multipart/form-data"> <div align="center"> <div class="btn btn-success"><table><tr><td width="20%">上傳文件</td></tr></table></div> <span class="btn fileinput-button"> <img src="/img/up55.png" width="40" height="40"> <input type="file" name = "file" accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"> </span> <input type="submit" value="上傳" /> </div> </form> <img alt="" src="${msg }"> </body> </html>
在這里面我是加了bootstar,bootstar可以自行在百度下載。
在其up55說我在網上下載的上傳圖標,為了頁面好看點
再接下來是controller
//這個方法是跳轉到jsp的方法
@RequestMapping("/othre")
public String otre(){ return "otre"; }
- //這個方法是上傳的方法
- @RequestMapping(value = "/upload", method = RequestMethod.POST)
- @ResponseBody
- public String upload(@RequestParam("file") MultipartFile file) throws IOException {
- if (!file.isEmpty()) {
- try {
- // 這里只是簡單例子,文件直接輸出到項目路徑下。
- // 實際項目中,文件需要輸出到指定位置,需要在增加代碼處理。
- BufferedOutputStream out = new BufferedOutputStream(
- new FileOutputStream("d:/pdf/"+file.getOriginalFilename()));
- System.out.println(file.getOriginalFilename());
- out.write(file.getBytes());
- out.flush();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- return "上傳失敗," + e.getMessage();
- } catch (IOException e) {
- e.printStackTrace();
- return "上傳失敗," + e.getMessage();
- }
- //調用Doc2HtmlUtil工具類
- Doc2HtmlUtil coc2HtmlUtil = Doc2HtmlUtil.getDoc2HtmlUtilInstance();
- File file1 = null;
- FileInputStream fileInputStream = null;
- //這里寫的是被轉文件的路徑
- file1 = new File("d:/pdf/"+file.getOriginalFilename());
- fileInputStream = new FileInputStream(file1);
- //為了后期方便,這里直接把文件名進行了截取,方便后續操作
- int i = file.getOriginalFilename().lastIndexOf(".");
- String substring = file.getOriginalFilename().substring(i);
- //上述的所有路徑以及以下路勁均可自定義
- coc2HtmlUtil.file2pdf(fileInputStream, "d:/ss",substring);
- Doc2HtmlUtil doc = new Doc2HtmlUtil();
- return "上傳成功";
- } else {
- return "上傳失敗,因為文件是空的.";
- }
- }
//這個方法是上傳的方法 @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String upload(@RequestParam("file") MultipartFile file) throws IOException { if (!file.isEmpty()) { try { // 這里只是簡單例子,文件直接輸出到項目路徑下。 // 實際項目中,文件需要輸出到指定位置,需要在增加代碼處理。 BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream("d:/pdf/"+file.getOriginalFilename())); System.out.println(file.getOriginalFilename()); out.write(file.getBytes()); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return "上傳失敗," + e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "上傳失敗," + e.getMessage(); } //調用Doc2HtmlUtil工具類 Doc2HtmlUtil coc2HtmlUtil = Doc2HtmlUtil.getDoc2HtmlUtilInstance(); File file1 = null; FileInputStream fileInputStream = null; //這里寫的是被轉文件的路徑 file1 = new File("d:/pdf/"+file.getOriginalFilename()); fileInputStream = new FileInputStream(file1); //為了后期方便,這里直接把文件名進行了截取,方便后續操作 int i = file.getOriginalFilename().lastIndexOf("."); String substring = file.getOriginalFilename().substring(i); //上述的所有路徑以及以下路勁均可自定義 coc2HtmlUtil.file2pdf(fileInputStream, "d:/ss",substring); Doc2HtmlUtil doc = new Doc2HtmlUtil(); return "上傳成功"; } else { return "上傳失敗,因為文件是空的."; } }
Controller中調用了word轉PdF的工具類,下面是工具類
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ConnectException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- 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;
- /**
- * 利用jodconverter(基於OpenOffice服務)將文件(*.doc、*.docx、*.xls、*.ppt)轉化為html格式或者pdf格式,
- * 使用前請檢查OpenOffice服務是否已經開啟, OpenOffice進程名稱:soffice.exe | soffice.bin
- *
- * @author yjclsx
- */
- /**
- * 利用jodconverter(基於OpenOffice服務)將文件(*.doc、*.docx、*.xls、*.ppt)轉化為html格式或者pdf格式,
- * 使用前請檢查OpenOffice服務是否已經開啟, OpenOffice進程名稱:soffice.exe | soffice.bin
- *
- * @author yjclsx
- */
- public class Doc2HtmlUtil {
- private static Doc2HtmlUtil doc2HtmlUtil;
- /**
- * 獲取Doc2HtmlUtil實例
- */
- public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
- if (doc2HtmlUtil == null) {
- doc2HtmlUtil = new Doc2HtmlUtil();
- }
- return doc2HtmlUtil;
- }
- /**
- * 轉換文件成pdf
- *
- * @param fromFileInputStream:
- * @throws IOException
- */
- public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
- String timesuffix = sdf.format(date);
- String docFileName = null;
- String htmFileName = null;
- if(".doc".equals(type)){
- docFileName = "doc_" + timesuffix + ".doc";
- htmFileName = "doc_" + timesuffix + ".pdf";
- }else if(".docx".equals(type)){
- docFileName = "docx_" + timesuffix + ".docx";
- htmFileName = "docx_" + timesuffix + ".pdf";
- }else if(".xls".equals(type)){
- docFileName = "xls_" + timesuffix + ".xls";
- htmFileName = "xls_" + timesuffix + ".pdf";
- }else if(".ppt".equals(type)){
- docFileName = "ppt_" + timesuffix + ".ppt";
- htmFileName = "ppt_" + timesuffix + ".pdf";
- }else{
- return null;
- }
- File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
- File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
- if (htmlOutputFile.exists())
- htmlOutputFile.delete();
- htmlOutputFile.createNewFile();
- if (docInputFile.exists())
- docInputFile.delete();
- docInputFile.createNewFile();
- /**
- * 由fromFileInputStream構建輸入文件
- */
- try {
- OutputStream os = new FileOutputStream(docInputFile);
- int bytesRead = 0;
- byte[] buffer = new byte[1024 * 8];
- while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
- os.write(buffer, 0, bytesRead);
- }
- os.close();
- fromFileInputStream.close();
- } catch (IOException e) {
- }
- OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
- try {
- connection.connect();
- } catch (ConnectException e) {
- System.err.println("文件轉換出錯,請檢查OpenOffice服務是否啟動。");
- }
- // convert
- DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
- converter.convert(docInputFile, htmlOutputFile);
- connection.disconnect();
- // 轉換完之后刪除word文件
- docInputFile.delete();
- return htmFileName;
- }
- public static void main(String[] args) throws IOException {
- Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance ();
- File file = null;
- FileInputStream fileInputStream = null;
- file = new File("C:/Users/MACHENIKE/Desktop/xxx.doc");
- fileInputStream = new FileInputStream(file);
- coc2HtmlUtil.file2pdf(fileInputStream, "E:/360","doc");
- }
- }
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ConnectException; import java.text.SimpleDateFormat; import java.util.Date; 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; /** * 利用jodconverter(基於OpenOffice服務)將文件(*.doc、*.docx、*.xls、*.ppt)轉化為html格式或者pdf格式, * 使用前請檢查OpenOffice服務是否已經開啟, OpenOffice進程名稱:soffice.exe | soffice.bin * * @author yjclsx */ /** * 利用jodconverter(基於OpenOffice服務)將文件(*.doc、*.docx、*.xls、*.ppt)轉化為html格式或者pdf格式, * 使用前請檢查OpenOffice服務是否已經開啟, OpenOffice進程名稱:soffice.exe | soffice.bin * * @author yjclsx */ public class Doc2HtmlUtil { private static Doc2HtmlUtil doc2HtmlUtil; /** * 獲取Doc2HtmlUtil實例 */ public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() { if (doc2HtmlUtil == null) { doc2HtmlUtil = new Doc2HtmlUtil(); } return doc2HtmlUtil; } /** * 轉換文件成pdf * * @param fromFileInputStream: * @throws IOException */ public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String timesuffix = sdf.format(date); String docFileName = null; String htmFileName = null; if(".doc".equals(type)){ docFileName = "doc_" + timesuffix + ".doc"; htmFileName = "doc_" + timesuffix + ".pdf"; }else if(".docx".equals(type)){ docFileName = "docx_" + timesuffix + ".docx"; htmFileName = "docx_" + timesuffix + ".pdf"; }else if(".xls".equals(type)){ docFileName = "xls_" + timesuffix + ".xls"; htmFileName = "xls_" + timesuffix + ".pdf"; }else if(".ppt".equals(type)){ docFileName = "ppt_" + timesuffix + ".ppt"; htmFileName = "ppt_" + timesuffix + ".pdf"; }else{ return null; } File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName); File docInputFile = new File(toFilePath + File.separatorChar + docFileName); if (htmlOutputFile.exists()) htmlOutputFile.delete(); htmlOutputFile.createNewFile(); if (docInputFile.exists()) docInputFile.delete(); docInputFile.createNewFile(); /** * 由fromFileInputStream構建輸入文件 */ try { OutputStream os = new FileOutputStream(docInputFile); int bytesRead = 0; byte[] buffer = new byte[1024 * 8]; while ((bytesRead = fromFileInputStream.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fromFileInputStream.close(); } catch (IOException e) { } OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (ConnectException e) { System.err.println("文件轉換出錯,請檢查OpenOffice服務是否啟動。"); } // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docInputFile, htmlOutputFile); connection.disconnect(); // 轉換完之后刪除word文件 docInputFile.delete(); return htmFileName; } public static void main(String[] args) throws IOException { Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance (); File file = null; FileInputStream fileInputStream = null; file = new File("C:/Users/MACHENIKE/Desktop/xxx.doc"); fileInputStream = new FileInputStream(file); coc2HtmlUtil.file2pdf(fileInputStream, "E:/360","doc"); } }
修改過后的代碼已經可以轉化pdf了,如果有什么問題可以在下方留言,我會及時回復,感謝大家的支持。
本文轉自:https://blog.csdn.net/weixin_41623274/article/details/79044422