需要jar包:

下载aspose-words-15.8.0-jdk16.jar包http://pan.baidu.com/s/1nvbJwnv

下载aspose-cells-8.5.2.jar包 http://pan.baidu.com/s/1kUBzsQ7

下载aspose.slides-15.9.0.jar包 http://pan.baidu.com/s/1jH3ZNbK

预览插件:

https://github.com/mozilla/pdf.js

遇到问题:

1,上传文件org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/aspose/words/License

解决:tomcat中bin目录下catalina.sh文件中,增加 JAVA_OPTS="-Djava.awt.headless=true" ,加完重启。

因为是一个项目调另一个项目,调用项目的aspose的jar包上传有问题,重新上传。

2,上传文件内存溢出 14:19:11.569 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
14:19:14.159 [http-bio-8081-exec-2] DEBUG o.s.w.m.c.CommonsMultipartResolver - Cleaning up multipart file [file] with original filename [aaa.docx], stored at [/xebest/tomcat-manager/work/Catalina/localhost/xe-manager/upload_5f4a16cc_15c1a35c502__8000_00000000.tmp]
14:19:14.525 [http-bio-8081-exec-2] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
Exception in thread "http-bio-8081-exec-2" 
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "http-bio-8081-exec-2"
Exception in thread "http-bio-8081-exec-1"

解决:

#export JAVA_OPTS="-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=1090,server=y,suspend=n -Djava.awt.headless=true"

JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:PermSize=64M -XX:MaxNewSize=512m -XX:MaxPermSize=256m -Djava.awt.headless=true"

3, http://ip:port/projectName/js/pdf/web/viewer.html?file=pdf/aaa.pdf 访问不到

解决:解除拦截

项目web.xml 文件添加

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
   </servlet-mapping>

仍访问不到

解决:tomcat/conf/context.xml添加<Context allowLinking="true">

4,doc转成的pdf文件乱码(其他文件转pdf不乱码)

解决:

(1),代码里设置转换编码格式;

(2)tomcat设置编码格式;tomcat/conf/server.xml<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" URIEncoding="UTF-8" />

(3)设置linux系统字体;

复制C:\Windows\fonts下字体到 /usr/share/Fonts。(此处为中文字体即可)

cd /usr/share/fonts/ #进入字体库文件夹

mkdir fonttmp #创建自己使用的字体库文件夹

chmod 755 /usr/share/fonts/windows/*     #更改这些字体库的权限

cd fonttmp #进行字体库

cp *.ttc,cp *.ttf #copy windows下的字体文件 (ttc和ttf)或支持中文的字体文件(ttc和ttf)到此文件夹下

mkfontdir #生成字体查询文件

mkfontscale #生成scale文件

fc-cache #扫描字体目录并生成字体信息的缓存

代码如下:

license.xml文件

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
      <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  </Data>
  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

DocToPdfUtil.java

package com.xebest.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

public class DocToPdfUtil {

// word转pdf
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = DocToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public static void docToPdf(String fromUrl,String toUrl) {

if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
//long old = System.currentTimeMillis();
File file = new File(toUrl); // 新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(fromUrl); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML,
// OpenDocument, PDF, EPUB, XPS, SWF
// 相互转换
os.flush();
os.close();
// long now = System.currentTimeMillis();
// System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
}
}

}

ExcelToPdf.java

package com.xebest.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;

public class ExcelToPdf {

// excel转pdf
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = DocToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml"); 
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public static void excelToPdf(String fromUrl,String toUrl) {

if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
File pdfFile = new File(toUrl);// 输出路径
Workbook wb = new Workbook(fromUrl);// 原始excel路径
FileOutputStream fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS, SaveFormat.PDF);
fileOS.flush();
fileOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

PptToPdf.java

package com.xebest.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

public class PptToPdf {

// ppt转pdf
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = DocToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml"); 
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public static void pptToPdf(String fromUrl,String toUrl) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
// long old = System.currentTimeMillis();
File file = new File(toUrl);// 输出pdf路径
Presentation pres = new Presentation(fromUrl);// 输入pdf路径
FileOutputStream fileOS = new FileOutputStream(file);
pres.save(fileOS, SaveFormat.Pdf);
fileOS.flush();
fileOS.close();
// long now = System.currentTimeMillis();
// System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" +
// "文件保存在:" + file.getPath()); //转化过程耗时
} catch (Exception e) {
e.printStackTrace();
}
}
}

调用

@RequestMapping("uploadFile")
public void uploadFile(@RequestParam(value="file",required=false) MultipartFile file)throws IOException {
String filePath=file==null?null:file.getOriginalFilename();
String str = "nofile";
String showStr = "nofile";
JSONObject json=new JSONObject();
if (StringUtils.isNotBlank(filePath)) {
String fileSuffixName=filePath.substring(filePath.lastIndexOf(".")+1).toLowerCase();
if (StringUtils.isNotBlank(fileSuffixName)&&("jpg".equals(fileSuffixName)
|| "jpeg".equals(fileSuffixName)|| "png".equals(fileSuffixName)
|| "bmp".equals(fileSuffixName)|| "doc".equals(fileSuffixName)
|| "docx".equals(fileSuffixName)|| "xlsx".equals(fileSuffixName)
|| "xls".equals(fileSuffixName)|| "pdf".equals(fileSuffixName))) {
try {
str=doUpload(file);
showStr = str;
if("doc".equals(fileSuffixName) || "docx".equals(fileSuffixName)
||"xlsx".equals(fileSuffixName) || "xls".equals(fileSuffixName)
||"pdf".equals(fileSuffixName)){
if(!"pdf".equals(fileSuffixName)){
showStr = fileSuffixName + "pdf";
}
json.put("type", 1);
}else{
json.put("type", 2);
}
}catch (IOException e) {
log.info("上传附件出错BrokerageController+uploadFile:",e);
}
}
}
json.put("fileUrl", str);
json.put("showUrl", showStr);
sendAjaxResponseForHtml(response,json);
/*PrintWriter pw = null;
try {
response.setCharacterEncoding("UTF-8");
//response.setContentType("text/html");
response.setContentType("text/html;charset=utf-8");
pw=response.getWriter();//{"reUrl":["pic.xebest.com/product/245/400/29bca1a343fe495e835fce762ce09e7e.jpg"],"recode":0}
pw.print(str);
pw.print(JSONObject.toJSON(json));
} catch (IOException e) {
e.printStackTrace();
}finally{
pw.flush();
pw.close();
}*/
}

public String doUpload(MultipartFile file)throws IOException{
String returnStr = "nofile";
String filePath=file==null?null:file.getOriginalFilename();
String fromUrl = "nofile";
String targetPath = ImagesConfInit.getStringValue("files_path");
File targetURL = new File(targetPath);
if (!targetURL.exists())
targetURL.mkdirs();

if (StringUtils.isNotBlank(filePath)) {
String fileSuffixName=filePath.substring(filePath.lastIndexOf(".")+1);
if(StringUtils.isNotBlank(fileSuffixName)){
fromUrl = targetPath+filePath;
File newFile = new File(fromUrl);
try {
file.transferTo(newFile);
log.info("文件路径----------------->"+targetPath+filePath);
returnStr = targetPath+filePath.substring(0,filePath.lastIndexOf(".")+1)+"pdf";
if(fileSuffixName.toLowerCase().equals("doc")
|| fileSuffixName.toLowerCase().equals("docx")){
DocToPdfUtil.docToPdf(fromUrl, returnStr);
returnStr = filePath.substring(0,filePath.lastIndexOf(".")+1)+"pdf";
}else if(fileSuffixName.toLowerCase().equals("xlsx")
|| fileSuffixName.toLowerCase().equals("xls")){
ExcelToPdf.excelToPdf(fromUrl, returnStr);
returnStr = filePath.substring(0,filePath.lastIndexOf(".")+1)+"pdf";
}else{
returnStr = filePath;
}
}catch (IOException e) {
log.info(fromUrl+"上传附件出错BrokerageController+uploadFile+doUpload:",e);
}
}
}
return returnStr;
}

@RequestMapping("showFile")
public void showFile(String fileUrl){
int type = 0;//1,pdf;2,图片;0,失败
JSONObject json=new JSONObject();
String showUrl ="nopic";
String fileSuffixName=fileUrl.substring(fileUrl.lastIndexOf(".")+1).toLowerCase();
if(StringUtils.isNotBlank(fileSuffixName)&& 
("doc".equals(fileSuffixName) || "docx".equals(fileSuffixName)
||"xlsx".equals(fileSuffixName) || "xls".equals(fileSuffixName)
||"pdf".equals(fileSuffixName))){
if(!"pdf".equals(fileSuffixName)){
showUrl = fileSuffixName + "pdf";
}else{
showUrl = fileUrl;
}
type = 1;
}else{
type = 2;
showUrl = fileUrl;
}
json.put("type", type);
json.put("showUrl", showUrl);
sendAjaxResponseForHtml(response,json);
}

参考文章:感谢各位大神文章的指点,原文链接如下

http://www.cnblogs.com/qiwu1314/p/6101400.html

http://www.cnblogs.com/qiwu1314/p/6121696.html

http://www.cnblogs.com/qiwu1314/p/6121649.html

http://blog.csdn.net/iphone4grf/article/details/46376935

http://blog.csdn.net/nantian321/article/details/51200180

http://blog.csdn.net/shanelooli/article/details/7212812

http://www.th7.cn/Program/java/201606/878222.shtml

doc,excel,ppt转存pdf并预览相关推荐

  1. docker onlyoffice7.1.1 word excel ppt在线编辑、在线预览_添加中文字体和中文字号_02

    文章目录 一. onlyoffice添加中文字体 1. 下载字体 2. 上传字体 3. 删除原版自带字体 4. 字体复制 5. 安装字体 6. 重启容器 7. 清除缓存 8. 效果验证 二. only ...

  2. 在线文件(Word、Excel、PPT、PDF)预览

    Go File View 是基于 Golang 的在线文件(Word.Excel.PPT.PDF)预览程序,受 kkFileView 启发并基于其 Web 前端开发. 使用spring boot打造文 ...

  3. Windows中PDF TXT Excel Word PPT等Office文件在预览窗格无法预览的终级解决方法大全

    切记:以上方法均会对注册表进行修改,一定要先备份整个注册表,以防万一,避免导致系统错误 一.问题症状或错误复现: 1.首先要打开 文件资源管理器的 文件 预览窗格 2.然后在文件资源管理器的右边就会显 ...

  4. [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!...

    [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你! 引言 之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询. 方案一 ...

  5. ❤️强烈推荐!Word、Excel、PPT、PDF在线预览解决方案

    大家好,我是锋哥: 平时大伙开发项目的时候,经常遇到业务需求Word.Excel.PPT.PDF在线预览功能: 市面上这方面的解决方案也有一些,不做过多评价.今天主要推荐的是一个特定提前下的永久免费解 ...

  6. 使用aspose方式使excel,ppt,word进行在线预览。(无水印)

    使用aspose方式使excel,ppt,word进行在线预览.(无水印) 1.首先,页面需要用jquery中window.open();打开一个新页面. window.open(../fileMan ...

  7. Java实现PDF在线预览

    Java实现PDF在线预览 前言:之前一直PDF一直是下载后再查看,一直在想如何如何在线预览,现已找到方法,作此笔记,也希望都对其他人有所帮助 代码实现 @Slf4j @Controller @Req ...

  8. Java使用openOffice转PDF以及PDF文件预览乱码问题

    Java使用openOffice转PDF以及PDF文件预览乱码问题 使用openOffice,支持doc, docx, .xls, .xlsx, .ppt, .pptx转pdf 一:依赖 <de ...

  9. Vue PDF文件预览打印vue-pdf

    Vue PDF文件预览vue-pdf 最近做项目,遇到预览PDF这个功能,在网上找了找,大多推荐的是pdf.js,不过在Vue中还是想偷懒直接npm组件,最后找到了一个还不错的Vue-pdf 组件,G ...

  10. java代码编辑器 pdf文件预览 主流SSM 代码生成器 shrio redis websocket即时通讯

    获取[下载地址] QQ: 313596790 官网 http://www.fhadmin.org/ A 代码编辑器,在线模版编辑,仿开发工具编辑器,pdf在线预览,文件转换编码 B 集成代码生成器 [ ...

最新文章

  1. 官宣!推动深圳大学、南科大创建“双一流”!
  2. HDU 4685 Prince and Princess(二分匹配加点建图+强连通分量)
  3. 使用 yield 减少内存消耗
  4. Dynamic Data Web Application编译是报GetActionPath调用模糊解决办法
  5. 5分绩点转4分_5分绩点转4分(五分制 四分制 对照表)
  6. 【Spring学习笔记-MVC-1.3】消息转换器HttpMessageConverter
  7. usbserialconverter驱动找不到_驱动到底是什么?别再用精灵管家无脑装驱动了
  8. Git -- 分支管理简介
  9. 基于深度学习的一款五子棋小游戏
  10. [概率论]-随机变量
  11. BFS解决连同块问题
  12. 手机炒股软件测试自学,手机炒股用什么软件好 主流手机炒股软件评测
  13. 女神:今天我3倍工资,放假半天 有法可依,我...
  14. debian10杀毒软件安装和使用
  15. eMTC是什么技术?
  16. put url带参数_Superlurl 一款开源关键词URL采集工具
  17. 剑~~~~~~~~~~
  18. php银联支付接口 demo,php版银联支付接口开发简单实例详解
  19. adjacency list(邻接表)神物
  20. 纵观30年5000多部国产电视剧,豆瓣评分最低的演员原来是……

热门文章

  1. C语言/C++中strcpy_s函数
  2. 妙用PRN文件,实现文档换机打印
  3. CAD如何完成10以上带圈序号的输入?
  4. 【Unity3D】人体模型及动画
  5. 明翰游戏学笔记V0.2(持续更新)
  6. win10无线投屏_WIN10笔记本投屏小米电视
  7. 《数字图像处理》第三版笔记(一)模糊处理
  8. tomcat编码配置gbk_修改Tomcat编码方式的两种方法
  9. fast-DTW算法
  10. Tomcat-startup.bat一点闪退的原因与解决方法