|
|
@ -32,187 +32,243 @@ import java.util.stream.Collectors; |
|
|
|
@Slf4j |
|
|
|
@Slf4j |
|
|
|
public class PdfUtils { |
|
|
|
public class PdfUtils { |
|
|
|
|
|
|
|
|
|
|
|
/**文件后缀名**/ |
|
|
|
/** 文件后缀名 **/ |
|
|
|
public static String DOC_SUFFIX = ".doc"; |
|
|
|
public static String DOC_SUFFIX = ".doc"; |
|
|
|
public static String DOCX_SUFFIX = ".docx"; |
|
|
|
public static String DOCX_SUFFIX = ".docx"; |
|
|
|
public static String XLS_SUFFIX = ".xls"; |
|
|
|
public static String XLS_SUFFIX = ".xls"; |
|
|
|
public static String XLSX_SUFFIX = ".xlsx"; |
|
|
|
public static String XLSX_SUFFIX = ".xlsx"; |
|
|
|
public static String PDF_SUFFIX = ".pdf"; |
|
|
|
public static String PDF_SUFFIX = ".pdf"; |
|
|
|
|
|
|
|
|
|
|
|
/*** |
|
|
|
/*** |
|
|
|
* 各文件转换为pdf |
|
|
|
* 各文件转换为pdf |
|
|
|
* @param templatePath 待转换的文件路径 |
|
|
|
* @param templatePath 待转换的文件路径 |
|
|
|
* @param templateFileName 待转换的文件名 |
|
|
|
* @param templateFileName 待转换的文件名 |
|
|
|
* @param savePath 转换为pdf后的文件路径 |
|
|
|
* @param savePath 转换为pdf后的文件路径 |
|
|
|
* @param saveFileName 转换为pdf后的文件名称 |
|
|
|
* @param saveFileName 转换为pdf后的文件名称 |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void convertPdf (String templatePath, String templateFileName, String savePath, String saveFileName) { |
|
|
|
public static void convertPdf(String templatePath, String templateFileName, String savePath, String saveFileName) { |
|
|
|
String templateFilePath = templatePath + templateFileName; |
|
|
|
String templateFilePath = templatePath + templateFileName; |
|
|
|
String saveFilePath = savePath + saveFileName; |
|
|
|
String saveFilePath = savePath + saveFileName; |
|
|
|
log.info("转换前的文件为:{};转换后的路径为:{}",templateFilePath,saveFilePath); |
|
|
|
log.info("转换前的文件为:{};转换后的路径为:{}", templateFilePath, saveFilePath); |
|
|
|
File templateFile = new File(templateFilePath); |
|
|
|
File templateFile = new File(templateFilePath); |
|
|
|
File saveFile = new File(saveFilePath); |
|
|
|
File saveFile = new File(saveFilePath); |
|
|
|
//获取文件类型
|
|
|
|
//获取文件类型
|
|
|
|
String fileType = templateFileName.substring(templateFileName.lastIndexOf("."),templateFileName.length()); |
|
|
|
String fileType = templateFileName.substring(templateFileName.lastIndexOf("."), templateFileName.length()); |
|
|
|
InputStream inputStream = null; |
|
|
|
InputStream inputStream = null; |
|
|
|
OutputStream outputStream = null; |
|
|
|
OutputStream outputStream = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
try { |
|
|
|
try { |
|
|
|
inputStream = new FileInputStream(templateFile); |
|
|
|
inputStream = new FileInputStream(templateFile); |
|
|
|
outputStream = new FileOutputStream(saveFile); |
|
|
|
outputStream = new FileOutputStream(saveFile); |
|
|
|
//document4j 转换 pdf linux环境下不支持
|
|
|
|
//document4j 转换 pdf linux环境下不支持
|
|
|
|
if(DOCX_SUFFIX.equals(fileType)) { |
|
|
|
if (DOCX_SUFFIX.equals(fileType)) { |
|
|
|
IConverter converter = LocalConverter.builder().build(); |
|
|
|
IConverter converter = LocalConverter.builder().build(); |
|
|
|
converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); |
|
|
|
converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); |
|
|
|
converter.shutDown(); |
|
|
|
converter.shutDown(); |
|
|
|
}else if(DOC_SUFFIX.equals(fileType)){ |
|
|
|
} else if (DOC_SUFFIX.equals(fileType)) { |
|
|
|
IConverter converter = LocalConverter.builder().build(); |
|
|
|
IConverter converter = LocalConverter.builder().build(); |
|
|
|
converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute(); |
|
|
|
converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute(); |
|
|
|
converter.shutDown(); |
|
|
|
converter.shutDown(); |
|
|
|
}else if(XLS_SUFFIX.equals(fileType)){ |
|
|
|
} else if (XLS_SUFFIX.equals(fileType)) { |
|
|
|
IConverter converter = LocalConverter.builder().build(); |
|
|
|
IConverter converter = LocalConverter.builder().build(); |
|
|
|
converter.convert(inputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute(); |
|
|
|
converter.convert(inputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute(); |
|
|
|
converter.shutDown(); |
|
|
|
converter.shutDown(); |
|
|
|
}else if(XLSX_SUFFIX.equals(fileType)){ |
|
|
|
} else if (XLSX_SUFFIX.equals(fileType)) { |
|
|
|
//此方式行不通
|
|
|
|
//此方式行不通
|
|
|
|
//converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
|
//converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
|
Workbook wb = new Workbook(); |
|
|
|
Workbook wb = new Workbook(); |
|
|
|
wb.loadFromFile(templateFilePath); |
|
|
|
wb.loadFromFile(templateFilePath); |
|
|
|
wb.saveToFile(saveFilePath,FileFormat.PDF); |
|
|
|
wb.saveToFile(saveFilePath, FileFormat.PDF); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
catch (Exception e) { |
|
|
|
log.error("转换失败,错误信息为:{}", e.getMessage()); |
|
|
|
log.error("转换失败,错误信息为:{}",e.getMessage()); |
|
|
|
throw new ServiceException(e.getMessage()); |
|
|
|
throw new ServiceException(e.getMessage()); |
|
|
|
} finally { |
|
|
|
} |
|
|
|
inputStream.close(); |
|
|
|
finally { |
|
|
|
outputStream.close(); |
|
|
|
inputStream.close(); |
|
|
|
} |
|
|
|
outputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
} |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch(IOException e) { |
|
|
|
} |
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
/** |
|
|
|
|
|
|
|
* 对象转化为Map 并设置默认值 |
|
|
|
/** |
|
|
|
* |
|
|
|
* 对象转化为Map 并设置默认值 |
|
|
|
* @param obj |
|
|
|
* @param obj |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static Map<String, Object> objectToMap(Object obj) { |
|
|
|
public static Map<String, Object> objectToMap(Object obj) { |
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |
|
|
|
if(ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |
|
|
|
Class clazz = obj.getClass(); |
|
|
|
Class clazz = obj.getClass(); |
|
|
|
List<Field> fieldList = new ArrayList<>(); |
|
|
|
List<Field> fieldList = new ArrayList<>(); |
|
|
|
while (clazz != null) { |
|
|
|
while(clazz != null) { |
|
|
|
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |
|
|
|
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |
|
|
|
clazz = clazz.getSuperclass(); |
|
|
|
clazz = clazz.getSuperclass(); |
|
|
|
} |
|
|
|
} |
|
|
|
fieldList.forEach(field -> { |
|
|
|
fieldList.forEach(field -> { |
|
|
|
field.setAccessible(true); |
|
|
|
field.setAccessible(true); |
|
|
|
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class); |
|
|
|
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class); |
|
|
|
Object value; |
|
|
|
Object value; |
|
|
|
try { |
|
|
|
try { |
|
|
|
value = field.get(obj); |
|
|
|
value = field.get(obj); |
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
throw new ServiceException("获取属性性出错"); |
|
|
|
throw new ServiceException("获取属性性出错"); |
|
|
|
} |
|
|
|
} |
|
|
|
//若为list则不处理
|
|
|
|
//若为list则不处理
|
|
|
|
if (value instanceof List) { |
|
|
|
if(value instanceof List){ |
|
|
|
return; |
|
|
|
return ; |
|
|
|
} |
|
|
|
} |
|
|
|
//若为时间格式则进行格式化
|
|
|
|
//若为时间格式则进行格式化
|
|
|
|
if (value instanceof LocalDateTime) { |
|
|
|
if(value instanceof LocalDateTime){ |
|
|
|
value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN); |
|
|
|
value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN); |
|
|
|
} |
|
|
|
} |
|
|
|
if (value instanceof Date) { |
|
|
|
if(value instanceof Date){ |
|
|
|
value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN); |
|
|
|
value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//如果value为空直接跳出
|
|
|
|
if ((ObjectUtils.isEmpty(value) || value == "") && StringUtil.isNoneBlank(field.getName())){ |
|
|
|
// if ((ObjectUtils.isEmpty(value) || value == "") && StringUtil.isNoneBlank(field.getName())){
|
|
|
|
return; |
|
|
|
// return;
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
//属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
|
|
|
|
//属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
|
|
|
|
if(ObjectUtil.isNotEmpty(defaultValue)){ |
|
|
|
if (ObjectUtil.isNotEmpty(defaultValue)) { |
|
|
|
result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(), |
|
|
|
result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(), |
|
|
|
ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value); |
|
|
|
ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else { |
|
|
|
result.put(field.getName(), Optional.ofNullable(value).orElse(" ")); |
|
|
|
result.put(field.getName(), Optional.ofNullable(value).orElse("")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 读取本地pdf,这里设置的是预览 |
|
|
|
/** |
|
|
|
*/ |
|
|
|
* 对象转化为Map 并设置默认值 |
|
|
|
public static void readPdf(HttpServletResponse response, String filePath) { |
|
|
|
* |
|
|
|
response.reset(); |
|
|
|
* @param obj |
|
|
|
response.setContentType("application/pdf"); |
|
|
|
* @return |
|
|
|
FileInputStream fileInputStream = null; |
|
|
|
* @throws Exception |
|
|
|
OutputStream outputStream = null; |
|
|
|
*/ |
|
|
|
try { |
|
|
|
public static Map<String, Object> objectToMap(Object obj, boolean isWordFlag) { |
|
|
|
File file = new File(filePath); |
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
fileInputStream = new FileInputStream(file); |
|
|
|
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |
|
|
|
outputStream = response.getOutputStream(); |
|
|
|
Class clazz = obj.getClass(); |
|
|
|
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream); |
|
|
|
List<Field> fieldList = new ArrayList<>(); |
|
|
|
response.setHeader("Content-Disposition", |
|
|
|
while (clazz != null) { |
|
|
|
"inline; filename= " + URLEncoder.encode( file.getName() , "UTF-8")); |
|
|
|
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |
|
|
|
outputStream.flush(); |
|
|
|
clazz = clazz.getSuperclass(); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
} |
|
|
|
e.printStackTrace(); |
|
|
|
fieldList.forEach(field -> { |
|
|
|
} catch (IOException e) { |
|
|
|
field.setAccessible(true); |
|
|
|
e.printStackTrace(); |
|
|
|
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class); |
|
|
|
} |
|
|
|
Object value; |
|
|
|
finally { |
|
|
|
try { |
|
|
|
try{ |
|
|
|
value = field.get(obj); |
|
|
|
fileInputStream.close(); |
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
outputStream.close(); |
|
|
|
throw new ServiceException("获取属性性出错"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
//若为list则不处理
|
|
|
|
catch (IOException e) { |
|
|
|
if (value instanceof List) { |
|
|
|
e.printStackTrace(); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//若为时间格式则进行格式化
|
|
|
|
} |
|
|
|
if (value instanceof LocalDateTime) { |
|
|
|
|
|
|
|
value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN); |
|
|
|
/** |
|
|
|
} |
|
|
|
* 读取本地pdf,这里设置的是导出 |
|
|
|
if (value instanceof Date) { |
|
|
|
*/ |
|
|
|
value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN); |
|
|
|
public static void exportPdf(HttpServletResponse response, String filePath) { |
|
|
|
} |
|
|
|
response.reset(); |
|
|
|
|
|
|
|
response.setContentType("application/pdf"); |
|
|
|
//如果value为空直接跳出
|
|
|
|
FileInputStream fileInputStream = null; |
|
|
|
// if ((ObjectUtils.isEmpty(value) || value == "") && StringUtil.isNoneBlank(field.getName())){
|
|
|
|
OutputStream outputStream = null; |
|
|
|
// return;
|
|
|
|
try { |
|
|
|
// }
|
|
|
|
File file = new File(filePath); |
|
|
|
|
|
|
|
fileInputStream = new FileInputStream(file); |
|
|
|
//属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
|
|
|
|
outputStream = response.getOutputStream(); |
|
|
|
if (ObjectUtil.isNotEmpty(defaultValue)) { |
|
|
|
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream); |
|
|
|
result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(), |
|
|
|
response.setHeader("Content-Disposition", "inline; filename= " + URLEncoder.encode( file.getName() , "UTF-8")); |
|
|
|
ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value); |
|
|
|
outputStream.flush(); |
|
|
|
} else { |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
if (isWordFlag) { |
|
|
|
e.printStackTrace(); |
|
|
|
//如果导出word为null会出现{{ ,value设置"\u00A0"
|
|
|
|
} catch (IOException e) { |
|
|
|
result.put(field.getName(), Optional.ofNullable(value).orElse("\u00A0")); |
|
|
|
e.printStackTrace(); |
|
|
|
} else { |
|
|
|
} |
|
|
|
result.put(field.getName(), Optional.ofNullable(value).orElse(" ")); |
|
|
|
finally { |
|
|
|
} |
|
|
|
try{ |
|
|
|
} |
|
|
|
fileInputStream.close(); |
|
|
|
}); |
|
|
|
outputStream.close(); |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
/** |
|
|
|
} |
|
|
|
* 读取本地pdf,这里设置的是预览 |
|
|
|
} |
|
|
|
*/ |
|
|
|
} |
|
|
|
public static void readPdf(HttpServletResponse response, String filePath) { |
|
|
|
|
|
|
|
response.reset(); |
|
|
|
|
|
|
|
response.setContentType("application/pdf"); |
|
|
|
|
|
|
|
FileInputStream fileInputStream = null; |
|
|
|
|
|
|
|
OutputStream outputStream = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
File file = new File(filePath); |
|
|
|
|
|
|
|
fileInputStream = new FileInputStream(file); |
|
|
|
|
|
|
|
outputStream = response.getOutputStream(); |
|
|
|
|
|
|
|
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream); |
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", |
|
|
|
|
|
|
|
"inline; filename= " + URLEncoder.encode(file.getName(), "UTF-8")); |
|
|
|
|
|
|
|
outputStream.flush(); |
|
|
|
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fileInputStream.close(); |
|
|
|
|
|
|
|
outputStream.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 读取本地pdf,这里设置的是导出 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void exportPdf(HttpServletResponse response, String filePath) { |
|
|
|
|
|
|
|
response.reset(); |
|
|
|
|
|
|
|
response.setContentType("application/pdf"); |
|
|
|
|
|
|
|
FileInputStream fileInputStream = null; |
|
|
|
|
|
|
|
OutputStream outputStream = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
File file = new File(filePath); |
|
|
|
|
|
|
|
fileInputStream = new FileInputStream(file); |
|
|
|
|
|
|
|
outputStream = response.getOutputStream(); |
|
|
|
|
|
|
|
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream); |
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "inline; filename= " + URLEncoder.encode(file.getName(), "UTF-8")); |
|
|
|
|
|
|
|
outputStream.flush(); |
|
|
|
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fileInputStream.close(); |
|
|
|
|
|
|
|
outputStream.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// public static void main(String[] args) {
|
|
|
|
// public static void main(String[] args) {
|
|
|
|
// Runnable runnable = new Runnable() {
|
|
|
|
// Runnable runnable = new Runnable() {
|
|
|
|