文档在线预览有两种实现方式:
1. windows server下用 jacob
2. linux server下 用openoffice
话不多说,看吧。这里是使用jacob实现的
64位系统就用 x64的dll,32位系统就用x86的dll。将dll文件放入放入jdk/bin目录下,如下图所示:
2. 将jacob引入到pom中
记得把jar包放进去项目里面,如下图所示:
pom.xml
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>com.jacob</groupId>
<artifactId>1.0.0</artifactId>
<scope>system</scope>
<systemPath>C:/Users/user/.m2/repository/com/jacob/1.0.0/jacob-1.0.0.jar</systemPath>
<!--<systemPath>标签的值就是jacob.jar的具体路径,改成自己的-->
</dependency>
//直接调用这个方法即可
@ApiOperation(value = "文档转pdf", notes = "文档转pdf")
@RequestMapping(value = "convert2PDF", method = RequestMethod.POST)
public Map convert2PDF(String inputFile, String pdfFile) {
return m_fileService.convert2PDF(inputFile,pdfFile);
}
@Value("${DOCUMENT_URL}")
private String DOCUMENT_URL;
@Value("${UPLOAD_URL}")
private String UPLOAD_URL;
private static final int wdFormatPDF = 17;
private static final int xlTypePDF = 0;
private static final int ppSaveAsPDF = 32;
private static final int msoTrue = -1;
private static final int msofalse = 0;
public Map convert2PDF(String inputFile, String pdfFile) {
Map<String, Object> tResultMap = new HashMap<>();
// 1.得到没有后缀名的文字
String fileName = inputFile.substring(0, inputFile.indexOf("."));
//2.拿到要转换的文件的全路径()
inputFile = UPLOAD_URL + inputFile;
//3.判断传进来的文件是否存在,有则下一步,没有就返回信息:“文件不存在”
String suffix = getFileSufix(inputFile);
File file = new File(inputFile);
if (!file.exists()) {
System.out.println("文件不存在!");
tResultMap.put("status", false);
return tResultMap;
}
//4.拿到要转换文件对应的转换后的文件的全路径
if (pdfFile == null) {
pdfFile = DOCUMENT_URL + fileName + ".pdf";
} else {
// 设置文件存储路径
pdfFile = DOCUMENT_URL + pdfFile + "/" + fileName + ".pdf";
}
//5.将转换后的文件全路径去文件库查该文件是否已经转换过了,如果已经转换过了,直接返回全路径,否则去转换pdf
pdfFile = pdfFile.replaceAll("\\\\", "/");
File filePdf = new File(pdfFile);
if (filePdf.exists()) {
String filePdfPath = filePdf.toString().replaceAll("\\\\", "/");
tResultMap.put("msg",filePdfPath); // 如果是pdf文件,不用转换,直接拿原路径去预览
//tResultMap.put("msg", "为了分清楚是转换的还是直接给出路径的,这里是直接给出路径的。");
return tResultMap;
}
//6.转换pdf的方法
if (suffix.equals("pdf")) {
System.out.println("PDF not need to convert!");
tResultMap.put("msg", inputFile);
return tResultMap;
}
if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
tResultMap.put("status", word2PDF(inputFile, pdfFile));
pdfFile = pdfFile.replaceAll("\\\\", "/");
tResultMap.put("msg", pdfFile);
return tResultMap;
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
tResultMap.put("status", ppt2PDF(inputFile, pdfFile));
pdfFile = pdfFile.replaceAll("\\\\", "/");
tResultMap.put("msg", pdfFile);
return tResultMap;
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
tResultMap.put("status", excel2PDF(inputFile, pdfFile));
pdfFile = pdfFile.replaceAll("\\\\", "/");
tResultMap.put("msg", pdfFile);
return tResultMap;
} else {
System.out.println("文件格式不支持转换!");
tResultMap.put("status", false);
return tResultMap;
}
}
public static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
public boolean word2PDF(String inputFile, String pdfFile) {
try {
//打开word应用程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
//设置word不可见
app.setProperty("Visible", false);
//获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
//调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch doc = Dispatch.call(docs,
"Open",
inputFile,
false,
true
).toDispatch();
//调用Document对象的SaveAs方法,将文档保存为pdf格式
/*
Dispatch.call(doc,
"SaveAs",
pdfFile,
wdFormatPDF //word保存为pdf格式宏,值为17
);
*/
Dispatch.call(doc,
"ExportAsFixedFormat",
pdfFile,
wdFormatPDF //word保存为pdf格式宏,值为17
);
//关闭文档
Dispatch.call(doc, "Close", false);
//关闭word应用程序
app.invoke("Quit", 0);
return true;
} catch (Exception e) {
return false;
}
}
public boolean excel2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels,
"Open",
inputFile,
false,
true
).toDispatch();
Dispatch.call(excel,
"ExportAsFixedFormat",
xlTypePDF,
pdfFile
);
Dispatch.call(excel, "Close", false);
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
}
}
public boolean ppt2PDF(String inputFile, String pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
//app.setProperty("Visible", msofalse);
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts,
"Open",
inputFile,
true,//ReadOnly
true,//Untitled指定文件是否有标题
false//WithWindow指定文件是否可见
).toDispatch();
Dispatch.call(ppt,
"SaveAs",
pdfFile,
ppSaveAsPDF
);
Dispatch.call(ppt, "Close");
app.invoke("Quit");
return true;
} catch (Exception e) {
return false;
}
}
5.预览pdf代码(注意了:文档在线预览是分为两步的,第一步是先将各种文档转换成pdf,第二步是读pdf)
public Map<String, String> readPdf(String filePath,HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, String> tResultMap = new HashMap<>();
tResultMap.put("status", "fail");
//String filePath = request.getParameter("filePath");
File filePic = new File(filePath);
if(filePath == null){
tResultMap.put("status", "路径为空");
return tResultMap;
}
if (filePic.exists()) {
FileInputStream is = new FileInputStream(filePic);
int i = is.available(); // 得到文件大小
byte data[] = new byte[i];
is.read(data); // 读数据
is.close();
response.setContentType("application/pdf;charset=utf-8"); // 设置返回的文件类型
OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
toClient.write(data); // 输出数据
toClient.close();
}
return tResultMap;
}
参考文献:https://www.cnblogs.com/xxyfhjl/p/6773786.html