当前位置:   article > 正文

fastDFS+LibreOffice文件预览(四):转为pdf格式在线预览_fastdfs word转换pdf查看

fastdfs word转换pdf查看

注意:

如果你复制我的代码到你的程序上报错,可以看看我第一篇文章实体类跟配置文件的设置:https://blog.csdn.net/qq_36688143/article/details/84162924

第二篇文件上传的代码:

https://blog.csdn.net/qq_36688143/article/details/84166676

1)需要下载LibreOffice

2)括号里的LibreOffice是这个程序的安装位置

  1. // 连接OpenOffice/LibreOffice服务 此地址为服务端安装项目路径,本地需要自行安装libreoffice
  2. OfficeManager officeManager = LocalOfficeManager.builder().officeHome(LibreOffice)
  3. .install().build();
  1. /* 本地LibreOffice的存储位置 */
  2. public static final String LibreOffice = "C:\\Program Files (x86)\\LibreOffice";

3)预览功能的后台代码

  1. /**
  2. * 预览
  3. * @param fileInfo
  4. * @param response
  5. * @throws Exception
  6. */
  7. @RequestMapping(value = "preview")
  8. public void preview(FileInfo fileInfo, HttpServletResponse response) throws Exception {
  9. response.setContentType("application/pdf");
  10. // 设置返回文件名
  11. response.setHeader("Content-Disposition",
  12. "inline; filename=" + new String((fileInfo.getFileName() + ".pdf").getBytes("UTF8"), "ISO8859_1"));
  13. try {
  14. // 从fastdfs中获取实际文件
  15. byte[] fileByte = fastDFSTemplate.loadFile(fileInfo.getFdfsGroup(),fileInfo.getFdfsPath());
  16. // 输出流
  17. OutputStream out = response.getOutputStream();
  18. // 是PDF就直接输出
  19. if ("PDF".equalsIgnoreCase(fileInfo.getFdfsPath().substring(fileInfo.getFdfsPath().indexOf(".")))){
  20. // 直接输出
  21. out.write(fileByte);
  22. }
  23. // 不是就做转换
  24. else {
  25. InputStream byteArrayInputStream = new ByteArrayInputStream(fileByte);
  26. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  27. // 此方法不能多线程使用
  28. synchronized (LocalOfficeManager.class) {
  29. // 连接OpenOffice/LibreOffice服务 此地址为服务端安装项目路径,本地需要自行安装libreoffice
  30. OfficeManager officeManager = LocalOfficeManager.builder().officeHome(LibreOffice)
  31. .install().build();
  32. try {
  33. // 服务启动
  34. officeManager.start();
  35. // 转换文档到pdf
  36. long time = System.currentTimeMillis();
  37. // 转换格式,注意:execute必须要用到,此为转换
  38. JodConverter.convert(byteArrayInputStream).as(fileInfo.getType()).to(byteArrayOutputStream)
  39. .as(DefaultDocumentFormatRegistry.PDF).execute();
  40. System.out.println("convert used :" + (System.currentTimeMillis() - time) + "ms");
  41. // 输出
  42. out.write(byteArrayOutputStream.toByteArray());
  43. } catch (OfficeException e) {
  44. e.printStackTrace();
  45. } finally {
  46. try {
  47. officeManager.stop();
  48. } catch (OfficeException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53. }
  54. out.flush();
  55. out.close();
  56. } catch (FastDFSException e) {
  57. e.printStackTrace();
  58. }
  59. }

4)预览功能的前端代码

不建议用<button/>,用<input type="button"/>代替。

  1. <input type="button" onclick="preview('${attachment.fileName}','${attachment.ext}','${attachment.fdfsGroup}','${attachment.fdfsPath}')"
  2. class="previewButton fontTypeTwo" value="预览"/>

JS部分

  1. /* 下载 */
  2. function download(fileName,fdfsGroup,fdfsPath) {
  3. location.href ="${ctx}/info/notice/download?fileName="+fileName+"&fdfsGroup="+fdfsGroup+"&fdfsPath="+fdfsPath;
  4. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/195430
推荐阅读
相关标签
  

闽ICP备14008679号