1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > aspose pdf 转成html 利用Aspose将office转换成HTML PDF格式

aspose pdf 转成html 利用Aspose将office转换成HTML PDF格式

时间:2021-04-29 06:19:59

相关推荐

aspose pdf 转成html 利用Aspose将office转换成HTML PDF格式

一、重要转换代码:

@Override

public void transform(InputStream in, OutputStream out, String fileName) throws IOException {

UUID uuid = UUID.randomUUID();

this.cacheFileName = fileName.substring(0, fileName.lastIndexOf(".")) + "-" + uuid;

// 设置本地缓存的路径

String tmpPath = localHtmlCachePath + IOUtils.DIR_SEPARATOR + this.cacheFileName + ".pdf";

String lowerFileName = fileName.toLowerCase();

try {

if (!new File(tmpPath).exists()) {

if (lowerFileName.endsWith(".xls") || lowerFileName.endsWith(".xlsx")

|| lowerFileName.endsWith(".csv")) {

com.aspose.cells.Workbook workbook = new com.aspose.cells.Workbook(in);

workbook.save(tmpPath, com.aspose.cells.SaveFormat.PDF);

} else if (lowerFileName.endsWith(".doc") || lowerFileName.endsWith(".docx")

|| lowerFileName.endsWith(".rtf")) {

com.aspose.words.Document doc = new com.aspose.words.Document(in);

doc.save(tmpPath, com.aspose.words.SaveFormat.PDF);

} else if (lowerFileName.endsWith(".ppt") || lowerFileName.endsWith(".pptx")

|| lowerFileName.endsWith(".pps") || lowerFileName.endsWith(".ppsx")) {

com.aspose.slides.Presentation ppt = new com.aspose.slides.Presentation(in);

ppt.save(tmpPath, com.aspose.slides.SaveFormat.Pdf);

} else if (lowerFileName.endsWith(".vdx") || lowerFileName.endsWith(".vsx")

|| lowerFileName.endsWith(".vtx") || lowerFileName.endsWith(".vsd")

|| lowerFileName.endsWith(".vsdx")) {

com.aspose.diagram.Diagram visio = new com.aspose.diagram.Diagram(in);

visio.save(tmpPath, com.aspose.diagram.SaveFileFormat.PDF);

} else if (lowerFileName.endsWith(".pdf")) {

com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(in);

pdf.save(tmpPath, com.aspose.pdf.SaveFormat.Pdf);

}

}

if (out instanceof ResponseOutputStream) {

HttpServletResponse response = ((ResponseOutputStream) out).getResponse();

response.sendRedirect("/filePreview/officeHtmlFileViwer/" + this.cacheFileName + ".pdf");

}

} catch (Exception ex) {

throw new RuntimeException(ex);

}

}

二、创建了一个maven 项目,用到的相关jar如下:

三、遇到问题:

1、缓存文件夹不存在则创建:

// 文件夹不存在则创建

if (!new File(localHtmlCachePath).exists()) {

new File(localHtmlCachePath).mkdirs();

}

2、有水印:获取lic有问题:不能图省事,要以此次的获取InputStream(setLicense方法执行完可能是把InputStream给close了)

3、response.sendRedirect("/preview/officeHtmlFileViwer/" + this.cacheFileName + ".html");执行之后没有打开新页面

原因:不能close,因为还有请求在跑。

来源:/time-on/p/7126359.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。