使用jodconverter和swftools实现文件在线预览

参考:仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF

文档在线预览主要用到如下两个工具

1,安装openoffice(同时下载jodconverter.jar,依赖commons-io等包)(将doc/ppt等文档转换为pdf需要用到)
2,安装swftools 0.9.2(依赖gcc/gcc-c++/make等工具)(将pdf转换为swf需要用到) 如果安装swftools出现如下问题: * The following headers/libraries are missing: jpeglib t1lib ungif jpeglib.h freetype t1lib.h gif_lib.h
* Disabling pdf2swf tool...
* Disabling jpeg2swf tool...
* Disabling gif2swf tool... zypper install t1lib-devel freetype2-devel giflib-devel libjpeg-devel

java代码目录如下:

$ ls -R *
com:
test com/test:
DocConverter.class FileUtils.class OpenOfficePDFConverter.class SWFToolsSWFConverter.class TestDocConverter.class
DocConverter.java FileUtils.java OpenOfficePDFConverter.java SWFToolsSWFConverter.java TestDocConverter.java libs:
commons-io-2.4-javadoc.jar commons-io-2.4-test-sources.jar commons-io-2.4.jar juh-3.2.1.jar ridl-3.2.1.jar
commons-io-2.4-sources.jar commons-io-2.4-tests.jar jodconverter-core-3.0-beta-4.jar jurt-3.2.1.jar unoil-3.2.1.jar

核心转换源代码如下:

doc/ppt/xls转pdf

//   com/text/OpenOfficePDFConverter.java
package com.test; import java.io.File;
import java.io.FileNotFoundException; import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager; import com.test.FileUtils; public class OpenOfficePDFConverter{ private static OfficeManager officeManager;
private static String OFFICE_HOME = "/usr/lib64/libreoffice";
private static int port[] = {8100}; public void convert2PDF(String inputFile, String pdfFile) { if(inputFile.endsWith(".txt")){
String odtFile = FileUtils.getFilePrefix(inputFile)+".odt";
if(new File(odtFile).exists()){
inputFile = odtFile;
}else{
try {
FileUtils.copyFile(inputFile,odtFile);
inputFile = odtFile;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
} startService();
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(inputFile),new File(pdfFile));
stopService();
} public void convert2PDF(String inputFile) {
String pdfFile = FileUtils.getFilePrefix(inputFile)+".pdf";
convert2PDF(inputFile,pdfFile);
} public static void startService(){
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
configuration.setOfficeHome(OFFICE_HOME);//设置OpenOffice.org安装目录
configuration.setPortNumbers(port); //设置转换端口,默认为8100
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//设置任务队列超时为24小时 officeManager = configuration.buildOfficeManager();
officeManager.start(); //启动服务
} catch (Exception ce) {
System.out.println("office转换服务启动失败!详细信息:" + ce);
}
} public static void stopService(){
if (officeManager != null) {
officeManager.stop();
}
}
}

pdf转swf

//   com/test/SWFToolsSWFConverter.java
package com.test; import java.io.File;
import java.io.IOException; import com.test.FileUtils; public class SWFToolsSWFConverter{ private static String PDF2SWF_PATH = "/usr/local/bin/pdf2swf"; public void convert2SWF(String inputFile, String swfFile) {
File pdfFile = new File(inputFile);
File outFile = new File(swfFile);
if(!inputFile.endsWith(".pdf")){
return ;
}
if(!pdfFile.exists()){
return ;
}
if(outFile.exists()){
return ;
}
String command = PDF2SWF_PATH +" "+inputFile+" -o "+swfFile+" -f -T 9 -t -s storeallcharacters";
try {
Runtime.getRuntime().exec(command);
} catch (Exception e) {
e.printStackTrace();
System.out.println("转换文档为swf文件失败!");
} } public void convert2SWF(String inputFile) {
String swfFile = FileUtils.getFilePrefix(inputFile)+".swf";
convert2SWF(inputFile,swfFile);
} }

结合两个转换器

package com.test;

import com.test.OpenOfficePDFConverter;
import com.test.SWFToolsSWFConverter; public class TestDocConverter {
public static void main(String[]args){
OpenOfficePDFConverter pdfConverter = new OpenOfficePDFConverter();
SWFToolsSWFConverter swfConverter = new SWFToolsSWFConverter();
DocConverter converter = new DocConverter(pdfConverter, swfConverter);
String pptFile = args[0];
converter.convert(pptFile);
}
}

命令行执行方式如下(生成pdf文件及swf文件)
javac -cp .:libs/* com/test/*.java
java -cp .:libs/* com/test/TestDocConverter uploads/b.txt

(pdf2swf $targetFile -o $targetFile.swf -f -T 9 -t -s storeallcharacters)

注:如果中文乱码,则将simsun.ttc文件放到liboffice的字体目录下,然后如果root用户转pdf不乱码,非root用户乱码,则将字体文件再方一份到/usr/share/font/truetype/

php 上传代码:

<?php

//print error msg
ini_set('display_errors',1);
error_reporting(E_ALL); $targetFolder = '/uploads'; // Relative to the root if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; $fileTypes = array('txt','doc','xls','ppt','docx',
'xlsx','pptx','pdf'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
$fileParts2 = pathinfo($tempFile); if (in_array($fileParts['extension'],$fileTypes)) {
$targetFile = rtrim($targetPath,'/') . '/' . $fileParts2["filename"].".".$fileParts['extension'];
move_uploaded_file($tempFile,$targetFile);
if ($fileParts['extension'] != "pdf") {
$ret = system("cd tool;/usr/bin/java -cp .:libs/* com/test/TestDocConverter $targetFile", $res);
$targetFile = str_replace(".".$fileParts['extension'],".swf", $targetFile);
$path = str_replace("/usr/local/nginx/htdocs","",$targetFile);
} else {
exec("pdf2swf $targetFile -o $targetFile.swf -f -T 9 -t -s storeallcharacters");
$path = str_replace("/usr/local/nginx/htdocs","",$targetFile.".swf");
} header("Location: ./view.php?path=".urlencode($path));
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>

用flexpaper显示如下:(view.php)

<script type="text/javascript">

    $('#documentViewer').FlexPaperViewer(
{ config : { SWFFile : '<?php
echo urldecode($_GET["path"]);
?>', Scale : 0.9,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : false,
FitWidthOnLoad : true,
FullScreenAsMaxWindow : true,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
RenderingOrder : 'flash',
StartAtPage : '', ViewModeToolsVisible : false,
ZoomToolsVisible : false,
NavToolsVisible : false,
CursorToolsVisible : false,
SearchToolsVisible : false,
WMode : 'window',
localeChain: 'en_US'
}}
);
</script>

测试了一下转换效果不是很理想,如果文件有中文字体,很容易乱码及文件无法转换的问题,尤其是pdf转swf这一步,失败率较高。

上一篇:CString向char类型转化 ---“=”: 无法从“wchar_t *”转换为“char *


下一篇:fiddler使用指南