生活随笔
收集整理的這篇文章主要介紹了
Java实现仿百度文库文档在线阅读
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為了實現這個功能,我們需要用到如下軟件,Java+FlexPaper+SwfTool+OpenOffice這四大件.
1、思路
我們將doc、ppt等文檔格式通過openoffice轉換成pdf的格式,然后用SWFTool工具將pdf切分成小塊的swf文件或者大型的swf文件或者圖片格式也行.一般都采用小塊swf或者小塊圖片格式,這樣用戶就能按需加載.比如我們的pdf文件有80M,用戶打開預覽頁面加載的就是當前查看頁的前一頁、當前頁、后一頁,當然這數字是可以通過調整配置去設置.所以用戶可以很快的去打開頁面進行預覽而不是加載一個整體的文件.
2、環境的安裝
1.安裝OpenOffice,官網下載地址:下載地址,我是使用的最新版.具體安裝方法自行百度.
2.啟動OpenOffice服務,CMD命令進入OpenOffice安裝目錄下的program目錄,鍵入如下命令
/opt/openoffice4/program/soffice “-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager” -nologo -headless -nofirststartwizard &
這個是啟動office后臺進行轉換的一個服務,無圖形界面.
3.下載JODConverter:下載地址,項目中主要使用lib目錄下的jar包。
4.下載并安裝SWFTools:下載地址,下載exe文件安裝完成即可
5.下載FlexPlayer
下載地址
未使用最新版,使用1.5.1
6.下載相關的xpdf及字符集(處理中文文檔字體的錯誤)
xpdf-3.02pl5-win32.zip?
下載地址為?
ftp://ftp.foolabs.com/pub/xpdf/xpdf-3.02pl5-win32.zip?
xpdf-chinese-simplified.tar.gz?
下載地址為?
ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz?
也可以去http://www.foolabs.com/xpdf/download.html查找你自己需要的字符集
您也可以去官網下載最新版本
1.解壓xpdf-3.02pl5-win32.zip,解壓后修改名稱為xpdf,并將其考到c盤根目錄下【路徑可以隨意指定,但是執行pdf2swf指令時要指定到當前目錄】。?
2.解壓xpdf-chinese-simplified,將xpdf-chinese-simplified放置xpf目錄下?
3.修改xpdf-chinese-simplified目錄下的add-to-xpdfrc文件
Add-to-xpdfrc代碼
#----- begin Chinese Simplified support package (2004-jul-27) cidToUnicode Adobe-GB1 /usr/local/share/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode unicodeMap ISO-2022-CN /usr/local/share/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap unicodeMap EUC-CN /usr/local/share/xpdf/chinese-simplified/EUC-CN.unicodeMap unicodeMap GBK /usr/local/share/xpdf/chinese-simplified/GBK.unicodeMap cMapDir Adobe-GB1 /usr/local/share/xpdf/chinese-simplified/CMap toUnicodeDir /usr/local/share/xpdf/chinese-simplified/CMap fontDir C:\WINDOWS\Fonts #添加字體格式displayCIDFontTT Adobe-GB1 /usr/local/share/xpdf/chinese-simplified/Fonts/simhei.ttf #displayCIDFontTT Adobe-GB1 /usr/..../gkai00mp.ttf #----- end Chinese Simplified support package
如果您未注釋掉下面提供的類中的languagedir段,則會在轉換的時候顯示如下信息?
表示成功添加了中文字體?
3.開發過程
1.添加JODConverter的jar包,同時也需要將所依賴的包添加進工程.
2.咱們需要一個轉換的類
public class Converter2Swf { private File pdfFile; private File swfFile; private File docFile; private File originalFile; private static final String[] extArray = {"doc","docx","ppt","pptx","xls","xlsx","txt","rtf"}; public Converter2Swf(String fileString) { init(fileString); } /* * 重新設置 file @param fileString */ public void setFile(String fileString) { init(fileString); } /* * 初始化 @param fileString */ private void init(String fileString) { originalFile = new File(fileString); String fileName = FilenameUtils.getBaseName(fileString); String fileExt = FilenameUtils.getExtension(fileString); if (Converter2Swf.can2Pdf(fileExt)) { docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName+new Date().getTime() + ".swf"); } else if ("pdf".equals(fileExt)) { pdfFile = new File(fileString); swfFile = new File(fileName+new Date().getTime() + ".swf"); } } public static boolean can2Pdf(String ext) { for (String temp : extArray) { if(temp.equals(ext)) return true; } return false; } public static boolean can2Swf(String ext) { if("pdf".equals(ext)) return true; for (String temp : extArray) { if(temp.equals(ext)) return true; } return false; } /* * 轉為PDF @param file */ private void doc2pdf() throws Exception { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); connection.disconnect(); } /* * 轉換成swf */ private int pdf2swf() throws Exception { int countFile = -1; int errorFlag = 0; String path = File.separator+FilenameUtils.getPath(swfFile.getPath()); String filename = FilenameUtils.getBaseName(swfFile.getPath()) + "%." + FilenameUtils.getExtension(swfFile.getPath()); String[] command = {"pdf2swf", pdfFile.getPath(), "-o", path + filename, "-T 9","-s","languagedir=/usr/local/share/xpdf/chinese-simplified"}; ProcessBuilder proc = new ProcessBuilder(command); proc.redirectErrorStream(true); Process p = proc.start(); String outlog = loadStream(p.getInputStream()); if(StringUtils.contains(outlog,"FATAL")) errorFlag = 2; else if(StringUtils.contains(outlog,"ERROR")) errorFlag = 1; if(errorFlag == 1) System.err.println(outlog); else if (errorFlag == 2) { System.err.println(outlog); countFile = 0; } else { System.out.println(outlog); countFile = StringUtils.countMatches(outlog, FilenameUtils.getBaseName(pdfFile.getName())); } if (pdfFile.exists() && !pdfFile.getPath().equals(originalFile.getPath())) { pdfFile.delete(); } return countFile; } static String loadStream(InputStream in) throws IOException { int ptr = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder buffer = new StringBuilder(); while ((ptr = reader.read()) != -1) { buffer.append((char) ptr); } return buffer.toString(); } /* * 轉換主方法 */ public int conver() throws Exception { int countFile = -1; if (swfFile != null) { if (docFile != null && can2Pdf(FilenameUtils.getExtension(docFile.getPath()))) doc2pdf(); if(pdfFile != null && can2Swf(FilenameUtils.getExtension(pdfFile.getPath()))) countFile = pdf2swf(); } return countFile; } /* * 返回文件路徑 @param s */ public String getswfPath() { if (swfFile.exists()) { String tempString = swfFile.getPath(); tempString = tempString.replaceAll("\\\\", "/"); return tempString; } else { return ""; } } /* * 設置輸出路徑 */ public void setOutputPath(String outputPath) { File output = new File(outputPath); if(!output.exists()) output.mkdirs(); if (!outputPath.equals("")) { if (outputPath.charAt(outputPath.length() - 1) == '/') { swfFile = new File(outputPath + FilenameUtils.getBaseName(swfFile.getPath()) + ".swf"); } else { swfFile = new File(outputPath + File.separator + FilenameUtils.getBaseName(swfFile.getPath()) + ".swf"); } } } public File getSwfFile() { return swfFile; } public static void main(String s[]) { File file = new File("/home/michael/06142722_m3Sr.pdf"); Converter2Swf d = new Converter2Swf(file.getPath()); d.setOutputPath(RequestContext.root()); int swfcount = 0; try { swfcount = d.conver(); } catch (Exception e) { e.printStackTrace(); } }}
該類只是單獨對文件進行轉換成pdf swf,可以在用戶上傳的時候進行對文件的處理,也可以在用戶上傳完之后通過一定定時任務去處理,這樣用戶就不需要為了等待服務器的轉換而浪費時間.該代碼是建立在linux基礎上進行,如果您是要在window上進行處理則要稍稍修改代碼?
該代碼這里進行切分是按頁切分成小型的swf文件?
如果不需要則command中的-T 9則行?
該代碼引入了中文字體庫,否則在進行轉換成swf的時候可能會出現一些缺少中文字體等錯誤,如果您不需要處理這種中文的錯誤,您也可以在command中去掉
languagedir=/usr/local/share/xpdf/chinese-simplified
通過執行這個類的main方法可以測試是否成功生成swf文件?
3.如果您已經成功產生了如上的swf文件,則可以進行最后一步,在頁面上調用FlexPaperViewer的swf文件顯示就可以
<script type="text/javascript"> var fp = new FlexPaperViewer( '/js/flexpaper/FlexPaperViewer', 'viewerPlaceHolder', { config : { SwfFile : '{$doc.swfurl(),$doc.swfcount()}', Scale : 0.6, ZoomTransition : 'easeOut', ZoomTime : 0.5, ZoomInterval : 0.2, FitPageOnLoad : false, FitWidthOnLoad : true, FullScreenAsMaxWindow : false, ProgressiveLoading : true, MinZoomSize : 0.2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: 'zh_CN' }}); </script>
這部分代碼不同版本的flexpaper會稍有不同,具體細節參考官網的文檔說明,如果您用的最新版的flexpaper,可能這樣的配置或者調用方式有些不同.
$doc.swfurl()是代碼的swf的url地址,而后面的$doc.swfcount()代表該文件轉換成功生成的swf小文件個數.?
需要注意的是我們的這個swfurl()方法返回的鏈接是必須不能帶有空格之類的,所以我們在轉換生成swf文件的時候最好采用時間來作為文件命名的一個參照,這樣就能避免出現空格的問題,同時對于大規模的部署這個應用,我們還得處理在Linux上,一個目錄下文件不能超過10000個文件,所以我們可以分成大大小小的目錄去處理.
最后的效果可以參考
效果演示
如果遇到什么問題,可以直接評論,我將竭盡所能幫助你.
http://www.blinkcoder.com/read-the-document-implemented-in-java-imitation-online
總結
以上是生活随笔為你收集整理的Java实现仿百度文库文档在线阅读的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。