原址:https://www.cnblogs.com/wdcwy/p/6949540.html
这个博主提供的最为详细,基本思路和流程都走了一片,包括一些坑都踩了
备份原文:
Java实现web在线预览office文档与pdf文档实例
https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX
摘要: 本文讲的是Java实现web在线预览office文档与pdf文档实例, 1、首先我们需要找到可以把office转换成pdf的方法,查找资料发现有openoffice这一软件可以把office转换成pdf,这一软件先下载下来,然后记住自己安装的在那个位置。然后在cmd环境下进入安装目录的program目
云计算 云服务器ECS 大数据 建站 备案 文档 域名 whois查询 文集
1、首先我们需要找到可以把office转换成pdf的方法,查找资料发现有openoffice这一软件可以把office转换成pdf,这一软件先下载下来,然后记住自己安装的在那个位置。然后在cmd环境下进入安装目录的program目录,输入打开openoffice的命令:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
输入完成之后在任务管理器可以看见soffice.bin的进程在任务管理器,这一服务就启动成功。当然在代码中转换office2pdf我们还需要一些架包。下载jodconverter-2.2.2架包,然后复制到lib目录下,引入架包就可以了。这个架包有如下包:
文档与pdf文档实例-office文档在线预览">
有一些项目重复的可以删除,根据实际情况自己处理。
2、我们需要找到转换pdf2swf的方法。查找资料发现swftools这个软件可以把pdf转换成swf文件。把它下下来安装好就可以了。
3、我们需要一个展示swf文件的容器,发现有flexpaper这个插件。而且展示效果还不错。所以我们需要下载这个插件。使用这个插件需要有三个js文件。分别是:jquery.js、flexpaper_flash.js、flexpaper_flash_debug.js。插件的名字是FlexPaperViewer.swf。
整个项目结如下:
准备工作完成下面开始编码.
转换类为DocConverter 的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
调用转换类只需要传word、ptt、excel、pdf文件所在的路径参数就可以了。
展示在线预览的jsp代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
其中可能会出现在线预览只能实现10页的情况,需要把RenderingOrder : 'flash',设置为flash才可以实现超过10页的在线预览。swfFilePat为转换后的文件所在路径。
注意问题:
1、发现错误一般是openoffice服务没有开启。
2、Linux环境下会存在中文乱码的问题,是linux下不像windows支持那么多字体,需要安装多的字体,并且把字体所在位置链接到flexpaper所在位置。在使用pdf2swf加上参数-s languagedir=/usr/local/xpdf-chinese-simplified/。具体的一些参数请百度。
java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org
openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行。
主要模块有writer(文本文档),impress(演示文稿),Calc(电子表格),Draw(绘图),Math(公式),base(数据库)
笔者下载的是openoffice.org 3.3.0。下载完直接安装即可。
但是,我们还需要启动openoffice server。有两种做法:
1.以命令行方式启动openoffice server,缺点是每次系统重启,都需要手动去把openoffice server启动。
2.将openoffice server作为操作系统的服务启动,既然成为了系统服务,就可以设定开机自动启动了。
我们先来看第一种方式,
1.以命令行方式启动openoffice server
在cmd命令下,cd opeonofiice的安装路径/program 如:cd c:\program files\openoffice.org 3\program\soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
2.以系统服务的方式启动
这里我们还需要Windows Resource Kit tools ,将openoffice server设为系统服务。
Windows Resource Kit tools 是微软专为管理人员、开发人员和高级用户开发的,包括管理活动目录、组策略、TCP/IP网络、注册表、系统安全、监测等涉及Windows Server 2003 操作系统的其它很多方面的非常规安装的工具组件。Resource Kit Tools for XP的发布使得XP用户也能使用Resource Kit Tools对这些问题进行处理。
下载windows resource kit tools,我们进行默认安装。
1.打开Windows Resource Kit Tools
在Command Shell执行以下命令:
"C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
打开 管理工具->服务 可以找到以 OpenOfficeUnoServer 命名的服务
2.打开注册表寻找以下路径
HKEY_LOCAL_MACHINE -> SYSTEM ->ControlSet001 ->Services ->OpenOfficeUnoServer
新建项 Parameters,在该项下添加两个字符串值:
key:Application
value:C:\Program Files\OpenOffice.org 3\program\soffice.exe
key:AppParameters
value:-invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard
3.在服务控制台,启动 openoffice 服务
4.在CMD中用以下命令查看8100是否已被监听:netstat -anop tcp
这样OpenOffice3.0就以服务方式运行在Windows系统上了。(使用cmd命令:netstat -anp tcp查看8100端口是否工作)
然後可以通过socket方式连接openOffice,以使用openoffice提供的某些服务,如文件转换服务,ms office转pdf等等。
开源项目 JODConverter 就是结合openoffice来进行文档转换的java组件。
另外有一个命令行工具swftools,该工具可以将pdf转换为swf格式的文档,提供给ie客?舳肆骼馈?nbsp;
另外,我们可以将该配置用bat文件来快速实现,运行前请先修改相应目录参数:
openoffice service.bat文件
"C:\Program Files\Windows Resource Kits\Tools\instsrv" OpenOfficeUnoServer "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters /ve /d
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters /v Application /t REG_SZ /d "C:\Program Files\OpenOffice.org 3\program\soffice.exe"
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\OpenOfficeUnoServer\Parameters /v AppParameters /t REG_SZ /d "-invisible -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard"
第二步,使用JODConverter将office文档转换为pdf
JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用
OpenOffice来进行转换工作,它能进行以下的转换工作:
1.Microsoft Office格式转换为OpenDucument,以及OpenDucument转换为Microsoft Office
2.OpenDucument转换为PDF,Word、Excel、PowerPoint转换为PDF,RTF转换为PDF等。
它是一个开源项目。
我的项目是在MyEclipse下开发的。
下载最新版的jodconverter-2.2.2,把lib文件夹的包导入到你的DocConverter项目的lib文件夹内。
(假设你的项目是DocConverter)
新建DOC2PDFUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
在DOC2PDFUtil.java,右键属性 - >Run as - >Java Application ,输出main的测试结果。
在jsp中执行
新建MyDOC2PDFTest.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
第三步,使用swftools将pdf转换为swf
建议下载swftools-0.9.1,笔者起先下载的是最新版的swftools-1.0版。貌似转换时出错,缺少什么组件。
继续笔者的DocConverter项目。笔者使用的开发环境是MyEclipse 9.0。
新建PDF2SWFUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
在PDF2SWFUtil.java,右键属性 - >Run as - >Java Application ,输出main的测试结果。
在jsp中执行
新建MyPDF2SWFTest.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
在项目DocConverter根目录,右键属性 - >Run as - >MyEclipse Server Application
发布到之前安装的Tomcat 6.0的根目录,然后用url路径访问:Http://localhost:8080/DocConverter/MyPDF2SWFTest.jsp 进行测试。
第四步,office文档转为pdf,同时进一步转为swf
网上资料有很多office文档转为pdf,pdf转为swf,但都是单步转换。关于一起转换的资料比较少。
一起转换有个问题就是转为pdf时,这个转换过程将花费一段时间才能成功,如何控制在pdf转换成功后,才进行swf的转换。
以及多个文档批量转换又该怎么办。
有幸笔者还是找到了一篇同时转换的代码:
新建DocConverter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
在DocConverter.java,右键属性 - >Run as - >Java Application ,输出main的测试结果。笔者分别进行单个转换,及批量转换,都测试可行。
至于为什么能成功进行pdf及swf的完整转换,在代码中没有看到和上述问题相关的控制。笔者在得到预期的结果,偶尔也会装糊涂,不去继续深究。
第五步,flexpaper在线浏览swf文档
FlexPaper是一个开源轻量级的在浏览器上显示各种文档的组件,被设计用来与PDF2SWF一起使用,
使在Flex中显示PDF成为可能,而这个过程并无需PDF软件环境的支持。它可以被当做Flex的库来使用。
另外你也可以通过将一些例如Word、PPT等文档转成PDF,然后实现在线浏览。
FlexPaper_1.2.4_flash:无打印功能
FlexPaper_1.4.7_flash:打印功能,右键打印
这里我们不需要让用户打印,所以笔者选择FlexPaper_1.2.4_flash。
FlexPaper项目中有演示demo,这里笔者不多述。
综上,一个完整的在线文档浏览方案。
附一:使用iText将jpg、jpeg、png转换为pdf
其他,使用iText将jpg/jpeg/png转换为pdf
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成 PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。1.在企业的信息系统中,报表处理一直占比较重要的作用,iText--一种生 成PDF报表的Java组件,通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或下载得到生成的报表,这样就很好的解 决了B/S系统的报表处理问题。2.支持文本,表格,图形的操作,可以方便的跟 Servlet 进行结合。
继续笔者的DocConverter项目。开发环境是MyEclipse 9.0。笔者下载的是iText5.0.4。
新建JPG2PDFUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
在JPG2PDFUtil.java,右键属性 - >Run as - >Java Application ,输出main的测试结果。
在jsp中执行
新建MyJPG2PDFTest.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
在项目DocConverter根目录,右键属性 - >Run as - >MyEclipse Server Application
发布到之前安装的Tomcat 6.0的根目录,然后用url路径访问:Http://localhost:8080/DocConverter/MyDOC2PDFTest.jsp 进行测试。
附二:常见问题集FAQ
1.txt转换swf,发生中文乱码。
txt转换为utf-8编码,或txt格式手动改为odt,上传就不会发生乱码。从根源上解决,暂时就算了...暂时不想去纠结这些鸡毛。
2.加密的pdf可能导致转换为swf失败。
3.Microsoft Excel在公式运算中支持文本型的数值,而OpenOffice.org Calc不支持
此问题暂无解,请手动将Excel中文本型的数值修改为数值型的数值。
4.部分Excel存在过于丰富的样式(大部分指没有数据的单元格也填充了各种样式),即使用专业Adobe Acrobat 7(或9) Pro来进行转换,
本来可能预计将产生20-30分页的pdf,结果却产生800-900分页的pdf。此类文档在线转换,难以避免的将导致转换死锁。
请在你的Excel文档中删除多余的,毫无必要的样式,或者你有更灵活的做法。
5.有些中文PDF文件转换为SWF后,出现乱码(特别一些专业期刊)
1.下载XPDF:xpdf-chinese-simplified.tar.gz
2.下载字体:gkai00mp.rar
3.修改xpdf-chinese-simplified目录下的add-to-xpdfrc文件。将里面的路径设为自己的路径:
#----- begin Chinese Simplified support package (2011-sep-02)
cidToUnicode Adobe-GB1 C:\xpdf-chinese-simplified\Adobe-GB1.cidToUnicode
unicodeMap ISO-2022-CN C:\xpdf-chinese-simplified\ISO-2022-CN.unicodeMap
unicodeMap EUC-CN C:\xpdf-chinese-simplified\EUC-CN.unicodeMap
unicodeMap GBK C:\xpdf-chinese-simplified\GBK.unicodeMap
cMapDir Adobe-GB1 C:\xpdf-chinese-simplified\CMap
toUnicodeDir C:\xpdf-chinese-simplified\CMap
fontDir C:\WINDOWS\Fonts
displayCIDFontTT Adobe-GB1 C:\xpdf-chinese-simplified\CMap\gkai00mp.ttf
#fontFileCC Adobe-GB1 /usr/..../gkai00mp.ttf
#----- end Chinese Simplified support package
4.参照上面的代码,在调用pdf2swf命令中加入“ -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ”参数。
PDF2SWFUtil.java
String cmd = exePath + " \"" + fileDir + "\" -o \"" + filePath + "/" + fileName + ".swf\" -T 9 -s languagedir=c:\\xpdf-chinese-simplified";
这样乱码的问题就解决了。