1.现在要做成这种效果
2.点击预览按钮,需要传stuffid
3.用pdf.js,我们发现一个URL带两个?号,导致浏览器不能正常解析
"${ctx}/pdfjs-1.5.188-dist/web/viewer.html?file=${ctx}/powerruntime/generalOperator!downStuffInfoZW.do?
stuffInfo.stuffid="+stuffid;
这时候我们要用到encodeURIComponent函数
参考:http://www.cnblogs.com/kagome2014/p/kagome2014001.html
4.后台获取file,转换为流
public void downStuffInfoZW() throws IOException {
//stuffInfo = optProcInfoManager.getStuffById("ebe69e8b4f1c4e6a904a89f0d16c549f");
stuffInfo = optProcInfoManager.getStuffById(stuffInfo.getStuffid());
//d:/oa.home/upload/workflowaffix/fw/FW00000000013872/619dede4b8df402faa2b1aa5aa449676.doc
String stufile = (SysParametersUtils.getWorkflowAffixHome() + stuffInfo.getStuffpath()).replaceAll("\\\\", "/");
String stufiledir = (SysParametersUtils.getWorkflowAffixHome() + stuffInfo.getStuffpath()).substring(0,stufile.lastIndexOf("/"));
//619dede4b8df402faa2b1aa5aa449676.doc
String fileP = stufile.substring(stufile.lastIndexOf("/") + 1, stufile.length());
File f = new File(stufile);
EfileStore efileStore = new EfileStore(f, fileP, stufiledir);
EfileManager.store(efileStore);
String converfilename = efileStore.getStoreDir().replaceAll("\\\\", "/")+"/"+efileStore.getStoreName();;//d:/oa.home/upload/workflowaffix/fw/FW00000000013872
//word转pdf
DocConverter d = new DocConverter(converfilename);
d.conver();
//读取pdf文件
try {
String filePath = converfilename;
//d:/oa.home/upload/workflowaffix/fw/FW00000000013872/
String filePath2 = filePath.substring(0,filePath.lastIndexOf("/") + 1);
//d:/oa.home/upload/workflowaffix/fw/FW00000000013872/
String pdfFileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length()).split("\\.")[0];
//d:/oa.home/upload/workflowaffix/fw/FW00000000013872/af3e50ca013c485cb9b33d50b98b1836.pdf
String pdfFileNamePath = filePath2+pdfFileName+".pdf";
File file = new File(pdfFileNamePath);
byte[] data = null;
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
} catch (Exception e) {
log.error("pdf文件处理异常:" + e.getMessage());
}
}
DocConverter.java
package com.centit.powerruntime.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
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;
/**
* doc docx格式转换
*/
public class DocConverter {
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;
public DocConverter(String fileString) {
ini(fileString);
}
/**
* 重新设置file
*
* @param fileString
*/
public void setFile(String fileString) {
ini(fileString);
}
/**
* 初始化
*
* @param fileString
*/
private void ini(String fileString) {
fileName = fileString.substring(0, fileString.lastIndexOf("."));
docFile = new File(fileString);
pdfFile = new File(fileName + ".pdf");
swfFile = new File(fileName + ".swf");
}
/**
* 转为PDF
*
* @param file
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
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转换器异常,需要转换的文档不存在,无法转换****");
}
}
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();
}
/**
* 检查操作系统类型
*
* @return
*/
private boolean isWin() {
Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
return os.startsWith("win") || os.startsWith("Win");
}
/**
* 转换主方法
*/
public boolean conver() {
if (swfFile.exists()) {
System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
return true;
}
if (isWin()) {
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
} else {
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}
try {
doc2pdf();
} catch (Exception e) {
e.printStackTrace();
return false;
}
if (swfFile.exists()) {
return true;
} else {
return false;
}
}
/**
* 返回文件路径
*
* @param s
*/
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
}
}
/**
*
* @return
*/
public String getPdfPath() {
if (pdfFile.exists()) {
String tempString = pdfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
}
}
/**
* 设置输出路径
*/
public void setOutputPath(String 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");
}
}
}
}
5.pom.xml中的jar包
6.
参考:https://www.cnblogs.com/vijayblog/p/6126335.html