commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.util; |
|
26 |
|
|
27 |
import cn.hutool.extra.spring.SpringUtil; |
|
28 |
import cn.hutool.log.Log; |
|
29 |
import org.jodconverter.DocumentConverter; |
|
30 |
import org.jodconverter.document.DocumentFormat; |
|
31 |
import org.jodconverter.office.OfficeException; |
|
32 |
import org.springframework.http.MediaType; |
|
33 |
import vip.xiaonuo.core.consts.MediaTypeConstant; |
|
34 |
import vip.xiaonuo.core.enums.DocumentFormatEnum; |
|
35 |
import vip.xiaonuo.core.exception.LibreOfficeException; |
|
36 |
|
|
37 |
import java.io.IOException; |
|
38 |
import java.io.InputStream; |
|
39 |
import java.io.OutputStream; |
|
40 |
|
|
41 |
/** |
|
42 |
* LibreOffice工具类,用于将word,excel,ppt等格式文件转为pdf预览 |
|
43 |
* |
|
44 |
* @author xuyuxiang |
|
45 |
* @date 2020/7/6 14:55 |
|
46 |
*/ |
|
47 |
public class LibreOfficeUtil { |
|
48 |
|
|
49 |
private static final Log log = Log.get(); |
|
50 |
|
|
51 |
private static DocumentConverter documentConverter; |
|
52 |
|
|
53 |
private static void init() { |
|
54 |
try { |
|
55 |
documentConverter = SpringUtil.getBean(DocumentConverter.class); |
|
56 |
} catch (Exception e) { |
|
57 |
throw new LibreOfficeException(); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* 将文件流转换为PDF流 |
|
63 |
* |
|
64 |
* @param inputStream:输入流 |
|
65 |
* @param outputStream:输入pdf流 |
|
66 |
* @param fileSuffix:源文件后缀 |
|
67 |
* @return 目标类型的contentType |
|
68 |
* @author xuyuxiang |
|
69 |
* @date 2020/7/6 15:02 |
|
70 |
*/ |
|
71 |
public static void convertToPdf(InputStream inputStream, OutputStream outputStream, String fileSuffix) { |
|
72 |
if(!MediaTypeConstant.DOC_PDF.equals(fileSuffix)) { |
|
73 |
init(); |
|
74 |
final DocumentFormatEnum documentFormatEnum = DocumentFormatEnum.valueOf(fileSuffix.toUpperCase()); |
|
75 |
final DocumentFormat format = documentFormatEnum.getFormFormat(); |
|
76 |
log.info(">>> 待转换的文档类型:{}", format); |
|
77 |
final DocumentFormat targetFormat = documentFormatEnum.getTargetFormat(); |
|
78 |
log.info(">>> 转换的目标文档类型:{}", targetFormat); |
|
79 |
try { |
|
80 |
final InputStream is = documentFormatEnum.getInputStream(inputStream); |
|
81 |
documentConverter.convert(is).as(format).to(outputStream).as(targetFormat).execute(); |
|
82 |
} catch (IOException | OfficeException e) { |
|
83 |
e.printStackTrace(); |
|
84 |
} |
|
85 |
log.info(">>> 文件转换结束"); |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* 根据文件后缀判断是否图片 |
|
91 |
* |
|
92 |
* @author xuyuxiang |
|
93 |
* @date 2020/7/6 15:31 |
|
94 |
*/ |
|
95 |
public static boolean isPic(String fileSuffix) { |
|
96 |
return MediaTypeConstant.IMG_JPG.equals(fileSuffix) |
|
97 |
|| MediaTypeConstant.IMG_JPEG.equals(fileSuffix) |
|
98 |
|| MediaTypeConstant.IMG_PNG.equals(fileSuffix) |
|
99 |
|| MediaTypeConstant.IMG_GIF.equals(fileSuffix) |
|
100 |
|| MediaTypeConstant.IMG_TIF.equals(fileSuffix) |
|
101 |
|| MediaTypeConstant.IMG_BMP.equals(fileSuffix); |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* 根据文件后缀判断是否文档 |
|
106 |
* |
|
107 |
* @author xuyuxiang |
|
108 |
* @date 2020/7/6 15:31 |
|
109 |
*/ |
|
110 |
public static boolean isDoc(String fileSuffix) { |
|
111 |
return MediaTypeConstant.DOC_TXT.equals(fileSuffix) |
|
112 |
|| MediaTypeConstant.DOC_DOC.equals(fileSuffix) |
|
113 |
|| MediaTypeConstant.DOC_DOCX.equals(fileSuffix) |
|
114 |
|| MediaTypeConstant.DOC_XLS.equals(fileSuffix) |
|
115 |
|| MediaTypeConstant.DOC_XLSX.equals(fileSuffix) |
|
116 |
|| MediaTypeConstant.DOC_PPT.equals(fileSuffix) |
|
117 |
|| MediaTypeConstant.DOC_PPTX.equals(fileSuffix) |
|
118 |
|| MediaTypeConstant.DOC_PDF.equals(fileSuffix); |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* 根据文件后缀获取转换目标类型 |
|
123 |
* |
|
124 |
* @author xuyuxiang |
|
125 |
* @date 2020/7/6 17:03 |
|
126 |
*/ |
|
127 |
public static String getTargetContentTypeBySuffix(String fileSuffix) { |
|
128 |
//如果目标类型是pdf |
|
129 |
if (MediaTypeConstant.DOC_TXT.equals(fileSuffix) |
|
130 |
|| MediaTypeConstant.DOC_DOC.equals(fileSuffix) |
|
131 |
|| MediaTypeConstant.DOC_DOCX.equals(fileSuffix) |
|
132 |
|| MediaTypeConstant.DOC_PPT.equals(fileSuffix) |
|
133 |
|| MediaTypeConstant.DOC_PPTX.equals(fileSuffix) |
|
134 |
|| MediaTypeConstant.DOC_PDF.equals(fileSuffix)) { |
|
135 |
return MediaType.APPLICATION_PDF_VALUE; |
|
136 |
} else { |
|
137 |
//否则是html类型 |
|
138 |
return MediaType.TEXT_HTML_VALUE; |
|
139 |
} |
|
140 |
} |
|
141 |
} |