使用aspose方式使excel,ppt,word进行在线预览。(无水印)
1.首先,页面需要用jquery中window.open();打开一个新页面。
window.open(../fileManage/preview?id=1);
2.请求后台代码(Jfinal框架)
2.1获取文件绝对路径,拼接pdf生成的路径
public void preview() {
HttpServletResponse response = this.getResponse();
Integer id = this.getParaToInt("id");
FileInfo fileInfo = FileInfo.dao.getFileInfoById(id);
String uploaddir = fileInfo.getStr("uploaddir");
HttpServletRequest request = this.getRequest();
String contextPath = request.getContextPath();
String basePath = request.getSession().getServletContext().getRealPath("/" + uploaddir + "");
String path = request.getContextPath();
String url = pdfUrl(basePath, fileInfo);
String fileName = fileInfo.getStr("name");
URL u = null;
try {
u = new URL("file:///" + url);
response.reset();
response.setContentType(u.openConnection().getContentType());
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" +fileName );
BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File(url)));
byte[] buf = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0) {
out.write(buf, 0, len);
}
br.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
renderNull();
}
2.2获取生成pdf路径。
public String pdfUrl(String basePath, FileInfo fileInfo) {
String filename = fileInfo.getStr("name");
String type = fileInfo.getStr("type");
int dot = basePath.lastIndexOf(".");
String path = basePath.substring(0, dot) + ".pdf";
AsposePdfUtil.convertToPdfAsAspose(basePath, path, type);
return path;
}
2.3aspose工具类
package com.wom.utils;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
import java.io.*;
public class AsposePdfUtil {
public static boolean convertToPdfAsAspose(String inputFile, String pdfFile,String suffix) {
File file = new File(inputFile) ;
if(!file.exists()) {
return false ;
}
if("pdf".equalsIgnoreCase(suffix)) {
return false ;
}
if("doc".equalsIgnoreCase(suffix) || "docx".equalsIgnoreCase(suffix) || "txt".equalsIgnoreCase(suffix)) {
return doc2pdf(inputFile,pdfFile) ;
} else if("xls".equalsIgnoreCase(suffix) || "xlsx".equalsIgnoreCase(suffix)) {
return excel2Pdf(inputFile, pdfFile);
} else if("ppt".equalsIgnoreCase(suffix) || "pptx".equalsIgnoreCase(suffix)) {
return ppt2pdf(inputFile, pdfFile);
} else if (suffix.equalsIgnoreCase("png") || suffix.equalsIgnoreCase("jpg")
|| suffix.equalsIgnoreCase("jpeg") || suffix.equalsIgnoreCase("gif")) {
return img2PDF(inputFile, pdfFile);
} else {
return false;
}
}
public static boolean getWordLicense() {
boolean result = false;
InputStream is = null ;
try {
is = AsposePdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
is.close();
} catch (Exception e) {
if(is != null) {
try {
is.close() ;
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
return result;
}
public static boolean doc2pdf(String inPath, String outPath) {
if (!getWordLicense()) {
return false;
}
FileOutputStream os = null ;
try {
long old = System.currentTimeMillis();
File file = new File(outPath);
os = new FileOutputStream(file);
Document doc = new Document(inPath);
doc.save(os, com.aspose.words.SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
os.close();
} catch (Exception e) {
if(os != null) {
try {
os.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
return false ;
}
return true ;
}
public static boolean getExcelLicense() {
boolean result = false;
InputStream is = null ;
try {
is = AsposePdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
is.close();
} catch (Exception e) {
if(is != null) {
try {
is.close() ;
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
return result;
}
public static boolean excel2Pdf(String excelPath, String pdfPath) {
long old = System.currentTimeMillis();
if (!getExcelLicense()) {
return false;
}
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File excelFile = new File(excelPath);
if (excelFile.exists()) {
fileInputStream = new FileInputStream(excelFile);
Workbook workbook = new Workbook(fileInputStream);
File pdfFile = new File(pdfPath);
fileOutputStream = new FileOutputStream(pdfFile);
workbook.save(fileOutputStream, com.aspose.cells.SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + pdfFile.getPath());
return true ;
} else {
System.out.println("文件不存在");
return false ;
}
} catch (Exception e) {
e.printStackTrace();
return false ;
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static boolean getPptLicense() {
boolean result = false;
InputStream is = null ;
try {
is = AsposePdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
is.close();
} catch (Exception e) {
if(is != null) {
try {
is.close() ;
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
return result;
}
public static boolean ppt2pdf(String inPath,String outPath) {
if (!getPptLicense()) {
return false;
}
FileOutputStream fileOS = null ;
try {
long old = System.currentTimeMillis();
File file = new File(outPath);
Presentation pres = new Presentation(inPath);
fileOS = new FileOutputStream(file);
pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
fileOS.close();
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath());
return true;
} catch (Exception e) {
if(fileOS != null) {
try {
fileOS.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
return false;
}
}
public static boolean img2PDF(String imgFilePath, String pdfFilePath){
File file = new File(imgFilePath);
if (file.exists()) {
com.lowagie.text.Document document = new com.lowagie.text.Document();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pdfFilePath);
PdfWriter.getInstance(document, fos);
document.addAuthor("newprint");
document.addSubject("test pdf.");
document.setPageSize(PageSize.A4);
document.open();
Image image = Image.getInstance(imgFilePath);
float imageHeight = image.getScaledHeight();
float imageWidth = image.getScaledWidth();
int i = 0;
while (imageHeight > 500 || imageWidth > 500) {
image.scalePercent(100 - i);
i++;
imageHeight = image.getScaledHeight();
imageWidth = image.getScaledWidth();
}
image.setAlignment(Image.ALIGN_CENTER);
document.add(image);
} catch (DocumentException de) {
System.out.println(de.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
document.close();
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else {
return false;
}
}
}
所需jar包下载:
aspose下载