您的位置:首页 > 运维架构 > Linux

Java语言下,openoffcie+swfTools+flexPaper在线预览功能,包含linux服务器部署

2018-04-11 15:05 399 查看
1.概论1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件2.通过swfTools插件将pdf文件转换成swf格式的文件3.通过FlexPaper文档组件在页面实现在线预览功能2.所需软件
openoffice : http://www.openoffice.org/zh-cn/download/ ,本人下载4.1.5版本
swfTools:http://www.swftools.org/download.html, 本人下载swftools-2013-04-09-1007.exe
FlexPaper :  https://flowpaper.com/download/ , 能够实现在页面上将swf文件进行在线预览展示,类似百度文库功能,可托拉拽网页,可分页等。
JODConverter : 是Java的文件转换器,此处仅用到几个jar包,
3.安装软件(1) openoffice安装,依次下一步进行,无需改变安装盘符,以cmd命令行进入安装目录,以我的为例:C:\Program Files (x86)\OpenOffice 4\program, 启动命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard


当进程中出现如下两个时,启动openoffice成功


(2)swfTools,依然按照提示一键安装,无需改变盘符路径4.开发
依照各自项目实际情况确定方案,本人项目采用springboot,将文件转换逻辑放在java后端进行,输出swf格式文件,将输出的swf文件放置项目工程目录下,前端页面进行读取指定目录的文件,从而输出文件,生成在线文档。
(1)flexpaper_flash.js,flexpaper_flash_debug.js, flexpaper.js,flexPaperViewer.swf , jquery.min.js,jquery.js 这几个个js文件主要是预览swf文件的插件放到工程目录下



(2)在pom.xml中导入jodConvertor 以及openoffice所需jar包
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>

<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>(3)将word,ppt,excle,txt等格式文件转换为swf
package cn.gnw.resource.util;

import java.io.File;

/**
* @author gege
* @Description word -> html转换工具
*
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* doc docx格式转换
*/
public class ConverterUtil {
private static final int environment = 2;// 环境 1:windows 2:linux
private String fileString;// (只涉及pdf2swf路径问题)
private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;
Logger logger = LoggerFactory.getLogger(getClass());
public ConverterUtil(String fileString) {
ini(fileString);
}

/**
* 重新设置file
*
* @param fileString
*/
public void setFile(String fileString) {
ini(fileString);
}

/**
* 初始化
*
* @param fileString
*/
private void ini(String fileString) {
this.fileString = fileString;
fileName = fileString.substring(0, fileString.lastIndexOf("."));
docFile = new File(fileString);
pdfFile = new File(fileName + ".pdf");
swfFile = new File(fileName + ".swf");
}

/**
* 转为PDF
*
* @param
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");
} catch (java.net.ConnectException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,openoffice服务未启动!****");
throw e;
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
} else {
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
}

/**
* 转换成 swf,指定swf安装路径
*/
@SuppressWarnings("unused")
private void pdf2swf() throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1) {// windows环境处理
try {
System.err.println("pdfFile.getPath()=============="+pdfFile.getPath());
System.err.println("swfFile.getPath()=============="+swfFile.getPath());
// Process p = r.exec("/usr/swftools/bin/pdf2swf"+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
Process p = r.exec("C:/Program Files (x86)/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf转换成功,文件输出:"
+ swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}

} catch (IOException e) {
e.printStackTrace();
throw e;
}
} else if (environment == 2) {// linux环境处理
try {
System.err.println("pdfFile.getPath()=============="+pdfFile.getPath());
System.err.println("swfFile.getPath()=============="+swfFile.getPath());
Process p = r.exec("pdf2swf" +" "+ pdfFile.getPath()
+ " -o " + swfFile.getPath() +" -s languagedir=/usr/share/xpdf/xpdf-chinese-simplified -s flashversion=9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf转换成功,文件输出:"
+ swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
} else {
System.out.println("****pdf不存在,无法转换****");
}
} else {
System.out.println("****swf已经存在不需要转换****");
}
}

static String loadStream(InputStream in) throws IOException {

int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();

while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
}

return buffer.toString();
}
/**
* 转换主方法
*/
@SuppressWarnings("unused")
public boolean conver() {

if (swfFile.exists()) {
System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
return true;
}

if (environment == 1) {
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
} else {
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}
try {
doc2pdf();
pdf2swf();
} catch (Exception e) {
e.printStackTrace();
return false;
}

if (swfFile.exists()) {
return true;
} else {
return false;
}
}

/**
* 返回文件路径
*
* @param
*/
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
}

}
/**
* 设置输出路径
*/
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
if (!outputPath.equals("")) {
String realName = fileName.substring(fileName.lastIndexOf("/"),
fileName.lastIndexOf("."));
if (outputPath.charAt(outputPath.length()) == '/') {
swfFile = new File(outputPath + realName + ".swf");
} else {
swfFile = new File(outputPath + realName + ".swf");
}
}
}

}
(3)将需转换的文件落地至项目工程目录下,调用上方工程实用类,转换为swf文件,并输出swf文件落地路径。
(4)前端页面
<script th:inline="javascript">
jQuery(function ($) {
var path = $('#swfSrc').val();
console.log(path);
path = '/pdfPreview/'+ path;
var fp = new FlexPaperViewer('/js/FlexPaperViewer', 'viewerPlaceHolder', {
config: {
SwfFile: escape(path), // swf文件位置
Scale: 1.2,
ZoomTransition: 'easeOut',
ZoomTime: 0.5,
ZoomInterval: 0.2,
FitPageOnLoad: false,
FitWidthOnload: false,
FullScreenAsMaxWindow: false,
ProgressiveLoading: false,
MinZoomSize: 0.2,
MaxZoomSize: 5,
SearchMatchAll: false,
InitViewMode: 'SinglePage',
RenderingOrder: 'flash',
ViewModeToolsVisible: true,
ZoomToolsVisible: true,
NavToolsVisible: true,
CursorToolsVisible: true,
SearchToolsVisible: true,
localeChain: 'en_US'
}
});
});
</script>(5)实现效果



(6)linux部分 (待完善)
在linux服务器上部署,若出现乱码现象,
linux服务器字体乱码:需下载字体库: yum install -y bitmap-fonts-compat yum groupinstall -y chinese-support 安装完要重启系统才生效
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: