为了实现这个功能,我们需要用到如下软件,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.cidToUnicodeunicodeMap ISO-2022-CN /usr/local/share/xpdf/chinese-simplified/ISO-2022-CN.unicodeMapunicodeMap EUC-CN /usr/local/share/xpdf/chinese-simplified/EUC-CN.unicodeMapunicodeMap GBK /usr/local/share/xpdf/chinese-simplified/GBK.unicodeMapcMapDir Adobe-GB1 /usr/local/share/xpdf/chinese-simplified/CMaptoUnicodeDir /usr/local/share/xpdf/chinese-simplified/CMapfontDir 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
152

被折叠的 条评论
为什么被折叠?



