2 changed files with 10 additions and 333 deletions
			
			
		| @ -1,322 +0,0 @@ | |||||||
| package com.hnac.hzims.operational.util; |  | ||||||
| 
 |  | ||||||
| import com.documents4j.api.DocumentType; |  | ||||||
| import com.documents4j.api.IConverter; |  | ||||||
| import com.documents4j.job.LocalConverter; |  | ||||||
| import com.hnac.hzims.ticket.annotation.DefaultValue; |  | ||||||
| import com.hnac.hzims.ticket.constants.TicketConstants; |  | ||||||
| import lombok.extern.slf4j.Slf4j; |  | ||||||
| import org.apache.commons.io.IOUtils; |  | ||||||
| import org.apache.poi.ss.usermodel.Workbook; |  | ||||||
| import org.springblade.core.log.exception.ServiceException; |  | ||||||
| import org.springblade.core.tool.utils.DateUtil; |  | ||||||
| import org.springblade.core.tool.utils.ObjectUtil; |  | ||||||
| import org.springblade.core.tool.utils.StringUtil; |  | ||||||
| 
 |  | ||||||
| import javax.servlet.http.HttpServletResponse; |  | ||||||
| import java.io.*; |  | ||||||
| import java.lang.reflect.Field; |  | ||||||
| import java.net.URLEncoder; |  | ||||||
| import java.time.LocalDateTime; |  | ||||||
| import java.util.*; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * @author hx |  | ||||||
|  */ |  | ||||||
| @Slf4j |  | ||||||
| public class PdfUtils { |  | ||||||
| 
 |  | ||||||
|     /** 文件后缀名 **/ |  | ||||||
|     public static String DOC_SUFFIX = ".doc"; |  | ||||||
|     public static String DOCX_SUFFIX = ".docx"; |  | ||||||
|     public static String XLS_SUFFIX = ".xls"; |  | ||||||
|     public static String XLSX_SUFFIX = ".xlsx"; |  | ||||||
|     public static String PDF_SUFFIX = ".pdf"; |  | ||||||
| 
 |  | ||||||
|     /*** |  | ||||||
|      * 各文件转换为pdf |  | ||||||
|      * @param templatePath 待转换的文件路径 |  | ||||||
|      * @param templateFileName 待转换的文件名 |  | ||||||
|      * @param savePath 转换为pdf后的文件路径 |  | ||||||
|      * @param saveFileName 转换为pdf后的文件名称 |  | ||||||
|      * @return |  | ||||||
|      */ |  | ||||||
|     public static void convertPdf(String templatePath, String templateFileName, String savePath, String saveFileName) { |  | ||||||
|         String templateFilePath = templatePath + templateFileName; |  | ||||||
|         String saveFilePath = savePath + saveFileName; |  | ||||||
|         log.info("转换前的文件为:{};转换后的路径为:{}", templateFilePath, saveFilePath); |  | ||||||
|         File templateFile = new File(templateFilePath); |  | ||||||
|         File saveFile = new File(saveFilePath); |  | ||||||
|         //获取文件类型
 |  | ||||||
|         String fileType = templateFileName.substring(templateFileName.lastIndexOf("."), templateFileName.length()); |  | ||||||
|         InputStream inputStream = null; |  | ||||||
|         OutputStream outputStream = null; |  | ||||||
|         try { |  | ||||||
|             try { |  | ||||||
|                 inputStream = new FileInputStream(templateFile); |  | ||||||
|                 outputStream = new FileOutputStream(saveFile); |  | ||||||
|                 //document4j 转换 pdf linux环境下不支持
 |  | ||||||
|                 if (DOCX_SUFFIX.equals(fileType)) { |  | ||||||
|                     IConverter converter = LocalConverter.builder().build(); |  | ||||||
|                     converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); |  | ||||||
|                     converter.shutDown(); |  | ||||||
|                 } else if (DOC_SUFFIX.equals(fileType)) { |  | ||||||
|                     IConverter converter = LocalConverter.builder().build(); |  | ||||||
|                     converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute(); |  | ||||||
|                     converter.shutDown(); |  | ||||||
|                 } else if (XLS_SUFFIX.equals(fileType)) { |  | ||||||
|                     IConverter converter = LocalConverter.builder().build(); |  | ||||||
|                     converter.convert(inputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute(); |  | ||||||
|                     converter.shutDown(); |  | ||||||
|                 } else if (XLSX_SUFFIX.equals(fileType)) { |  | ||||||
|                     //此方式行不通
 |  | ||||||
|                     //converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
 |  | ||||||
|                     Workbook wb = new Workbook(); |  | ||||||
|                     wb.loadFromFile(templateFilePath); |  | ||||||
|                     wb.saveToFile(saveFilePath, FileFormat.PDF); |  | ||||||
|                 } |  | ||||||
|             } catch (Exception e) { |  | ||||||
|                 log.error("转换失败,错误信息为:{}", e.getMessage()); |  | ||||||
|                 throw new ServiceException(e.getMessage()); |  | ||||||
|             } finally { |  | ||||||
|                 inputStream.close(); |  | ||||||
|                 outputStream.close(); |  | ||||||
|             } |  | ||||||
|         } catch (IOException e) { |  | ||||||
|             e.printStackTrace(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * 对象转化为Map 并设置默认值 |  | ||||||
|      * |  | ||||||
|      * @param obj |  | ||||||
|      * @return |  | ||||||
|      * @throws Exception |  | ||||||
|      */ |  | ||||||
|     public static Map<String, Object> objectToMap(Object obj) { |  | ||||||
|         Map<String, Object> result = new HashMap<>(); |  | ||||||
|         if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |  | ||||||
|             Class clazz = obj.getClass(); |  | ||||||
|             List<Field> fieldList = new ArrayList<>(); |  | ||||||
|             while (clazz != null) { |  | ||||||
|                 fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |  | ||||||
|                 clazz = clazz.getSuperclass(); |  | ||||||
|             } |  | ||||||
|             fieldList.forEach(field -> { |  | ||||||
|                 field.setAccessible(true); |  | ||||||
|                 DefaultValue defaultValue = field.getAnnotation(DefaultValue.class); |  | ||||||
|                 Object value; |  | ||||||
|                 try { |  | ||||||
|                     value = field.get(obj); |  | ||||||
|                 } catch (IllegalAccessException e) { |  | ||||||
|                     throw new ServiceException("获取属性性出错"); |  | ||||||
|                 } |  | ||||||
|                 //若为list则不处理
 |  | ||||||
|                 if (value instanceof List) { |  | ||||||
|                     return; |  | ||||||
|                 } |  | ||||||
|                 //若为时间格式则进行格式化
 |  | ||||||
|                 if (value instanceof LocalDateTime) { |  | ||||||
|                     value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN); |  | ||||||
|                 } |  | ||||||
|                 if (value instanceof Date) { |  | ||||||
|                     value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 //如果value为空直接跳出
 |  | ||||||
|                 // if ((ObjectUtils.isEmpty(value) || value == "") && StringUtil.isNoneBlank(field.getName())){
 |  | ||||||
|                 // 	return;
 |  | ||||||
|                 // }
 |  | ||||||
| 
 |  | ||||||
|                 //属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
 |  | ||||||
|                 if (ObjectUtil.isNotEmpty(defaultValue)) { |  | ||||||
|                     result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(), |  | ||||||
|                             ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value); |  | ||||||
|                 } else { |  | ||||||
|                     result.put(field.getName(), Optional.ofNullable(value).orElse(" ")); |  | ||||||
| 
 |  | ||||||
|                 } |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|         return result; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * 对象转化为Map 并设置默认值 |  | ||||||
|      * |  | ||||||
|      * @param obj |  | ||||||
|      * @return |  | ||||||
|      * @throws Exception |  | ||||||
|      */ |  | ||||||
|     public static Map<String, Object> objectToMap(Object obj, boolean isWordFlag) { |  | ||||||
|         Map<String, Object> result = new HashMap<>(); |  | ||||||
|         if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |  | ||||||
|             Class clazz = obj.getClass(); |  | ||||||
|             List<Field> fieldList = new ArrayList<>(); |  | ||||||
|             while (clazz != null) { |  | ||||||
|                 fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |  | ||||||
|                 clazz = clazz.getSuperclass(); |  | ||||||
|             } |  | ||||||
|             fieldList.forEach(field -> { |  | ||||||
|                 field.setAccessible(true); |  | ||||||
|                 DefaultValue defaultValue = field.getAnnotation(DefaultValue.class); |  | ||||||
|                 Object value; |  | ||||||
|                 try { |  | ||||||
|                     value = field.get(obj); |  | ||||||
|                 } catch (IllegalAccessException e) { |  | ||||||
|                     throw new ServiceException("获取属性性出错"); |  | ||||||
|                 } |  | ||||||
|                 //若为list则不处理
 |  | ||||||
|                 if (value instanceof List) { |  | ||||||
|                     return; |  | ||||||
|                 } |  | ||||||
|                 //若为时间格式则进行格式化
 |  | ||||||
|                 if (value instanceof LocalDateTime) { |  | ||||||
|                     value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN); |  | ||||||
|                 } |  | ||||||
|                 if (value instanceof Date) { |  | ||||||
|                     value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 //如果value为空直接跳出
 |  | ||||||
|                 // if ((ObjectUtils.isEmpty(value) || value == "") && StringUtil.isNoneBlank(field.getName())){
 |  | ||||||
|                 // 	return;
 |  | ||||||
|                 // }
 |  | ||||||
| 
 |  | ||||||
|                 //属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
 |  | ||||||
|                 if (ObjectUtil.isNotEmpty(defaultValue)) { |  | ||||||
|                     result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(), |  | ||||||
|                             ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value); |  | ||||||
|                 } else { |  | ||||||
|                     if (isWordFlag) { |  | ||||||
|                         //如果导出word为null会出现{{ ,value设置"\u00A0"
 |  | ||||||
|                         result.put(field.getName(), Optional.ofNullable(value).orElse("\u00A0")); |  | ||||||
|                     } else { |  | ||||||
|                         result.put(field.getName(), Optional.ofNullable(value).orElse(" ")); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|         return result; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * 对象转化为Map 并设置默认值 |  | ||||||
|      * |  | ||||||
|      * @param obj |  | ||||||
|      * @return |  | ||||||
|      * @throws Exception |  | ||||||
|      */ |  | ||||||
|     public static Map<String, Object> objectToMapResult(Object obj, Map<String, Object> map) { |  | ||||||
|         Map<String, Object> result = new HashMap<>(); |  | ||||||
|         if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |  | ||||||
|             Class clazz = obj.getClass(); |  | ||||||
|             List<Field> fieldList = new ArrayList<>(); |  | ||||||
|             while (clazz != null) { |  | ||||||
|                 fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |  | ||||||
|                 clazz = clazz.getSuperclass(); |  | ||||||
|             } |  | ||||||
|             fieldList.forEach(field -> { |  | ||||||
|                 field.setAccessible(true); |  | ||||||
|                 DefaultValue defaultValue = field.getAnnotation(DefaultValue.class); |  | ||||||
|                 Object value; |  | ||||||
|                 try { |  | ||||||
|                     value = field.get(obj); |  | ||||||
|                 } catch (IllegalAccessException e) { |  | ||||||
|                     throw new ServiceException("获取属性性出错"); |  | ||||||
|                 } |  | ||||||
|                 //若为list则不处理
 |  | ||||||
|                 if (value instanceof List) { |  | ||||||
|                     return; |  | ||||||
|                 } |  | ||||||
|                 //若为时间格式则进行格式化
 |  | ||||||
|                 if (value instanceof LocalDateTime) { |  | ||||||
|                     value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN); |  | ||||||
|                 } |  | ||||||
|                 if (value instanceof Date) { |  | ||||||
|                     value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 if (map.containsKey(field.getName())) { |  | ||||||
|                     return; |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 //属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
 |  | ||||||
|                 if (ObjectUtil.isNotEmpty(defaultValue)) { |  | ||||||
|                     result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(), |  | ||||||
|                             ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value); |  | ||||||
|                 } else { |  | ||||||
|                     //如果导出word为null会出现{{ ,value设置"\u00A0"
 |  | ||||||
|                     result.put(field.getName(), Optional.ofNullable(value).orElse("")); |  | ||||||
|                 } |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|         return result; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * 读取本地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(); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
					Loading…
					
					
				
		Reference in new issue