PdfUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.huimv.receive.common.utils;
  2. //import cn.bt.common.utils.StringUtils;
  3. //import cn.bt.modules.process.instance.enums.InstanceStatusEnum;
  4. //import com.itextpdf.text.*;
  5. //import com.itextpdf.text.pdf.*;
  6. import cn.hutool.core.io.IoUtil;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.BufferedInputStream;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.InputStream;
  12. /**
  13. * @Author: xp
  14. * @Date: 2021/7/14
  15. */
  16. public class PdfUtil {
  17. /**
  18. * 返回PDF流
  19. *
  20. * @param response 相应设置
  21. * @param pathName 水印文件路径和名称
  22. * @throws Exception 异常
  23. */
  24. /*public static void returnPdfStream(HttpServletResponse response, String pathName) throws Exception {
  25. response.setContentType("application/pdf");
  26. File file = new File(pathName);
  27. if (file.exists()) {
  28. FileInputStream in = new FileInputStream(file);
  29. OutputStream out = response.getOutputStream();
  30. byte[] b = new byte[1024 * 4];
  31. int n;
  32. while ((n = in.read(b)) > -1) {
  33. out.write(b, 0, n);
  34. }
  35. out.flush();
  36. in.close();
  37. out.close();
  38. }
  39. }*/
  40. public static void returnPdfStream2(HttpServletResponse response, String pathName, String subsidyNane) throws Exception {
  41. InputStream in = null;
  42. try {
  43. File file = new File(pathName);
  44. in = new BufferedInputStream(new FileInputStream(file));
  45. String filename = new String((subsidyNane+".pdf").getBytes(), "ISO8859_1");
  46. response.setContentType("application/binary;charset=ISO8859_1");
  47. response.setHeader("Content-disposition", "attachment; filename=" + filename);// 组装附件名称和格式
  48. IoUtil.copy(in, response.getOutputStream());
  49. } catch (Exception e) {
  50. System.out.println("------------ 这里除了问题!!!-----------");
  51. System.out.println(e);
  52. }finally {
  53. if (in != null) {
  54. in.close();
  55. }
  56. }
  57. }
  58. public static void returnPdfStream3(HttpServletResponse response, String pathName, String subsidyNane) throws Exception {
  59. InputStream in = null;
  60. try {
  61. File file = new File(pathName);
  62. in = new BufferedInputStream(new FileInputStream(file));
  63. String filename = new String((subsidyNane+".xls").getBytes(), "ISO8859_1");
  64. response.setContentType("application/binary;charset=ISO8859_1");
  65. response.setHeader("Content-disposition", "attachment; filename=" + filename);// 组装附件名称和格式
  66. IoUtil.copy(in, response.getOutputStream());
  67. } catch (Exception e) {
  68. System.out.println("------------ 这里除了问题!!!-----------");
  69. System.out.println(e);
  70. }finally {
  71. if (in != null) {
  72. in.close();
  73. }
  74. }
  75. }
  76. /**
  77. * 返回水印图片路径
  78. *
  79. * @param instanceStatus 流程状态
  80. * @return 水印图片路径
  81. */
  82. // public static String returnWatermarkPath(int instanceStatus) {
  83. // String watermarkPath = "ioffice-api/src/main/resources/static/watermark";
  84. // if (InstanceStatusEnum.E_0.getCode().equals(instanceStatus)) {
  85. // watermarkPath = watermarkPath + "/已作废.png";
  86. //
  87. // } else if (InstanceStatusEnum.E_2.getCode().equals(instanceStatus)) {
  88. // watermarkPath = watermarkPath + "/审批通过.png";
  89. //
  90. // } else {
  91. // watermarkPath = watermarkPath + "/审批中.png";
  92. // }
  93. // return watermarkPath;
  94. // }
  95. //
  96. // /**
  97. // * 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
  98. // *
  99. // * @param pdfPath 保存路径
  100. // * @param document 文档
  101. // * @param pdfPathName 文件保存路径和名称
  102. // * @return 书写器(Writer)
  103. // * @throws Exception
  104. // */
  105. // public static PdfWriter createPdfWriter(String pdfPath, Document document, String pdfPathName) throws Exception {
  106. //
  107. // //判断文件夹是否存在
  108. // File file = new File(pdfPath);
  109. // if (!file.exists()) {
  110. // file.mkdir();
  111. // }
  112. // file = new File(pdfPathName);
  113. //
  114. // PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
  115. // writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
  116. //
  117. // return writer;
  118. // }
  119. //
  120. // /**
  121. // * 设置第一标题内容
  122. // *
  123. // * @param title 第一标题
  124. // * @param document 文档
  125. // * @throws Exception 异常
  126. // */
  127. // public static void setPdfFirstTitle(String title, Document document) throws Exception {
  128. // Paragraph paragraph = new Paragraph(title, getPdfChineseFont(0));
  129. // paragraph.setAlignment(Element.ALIGN_CENTER);
  130. // paragraph.setSpacingAfter(2);
  131. // document.add(paragraph);
  132. // }
  133. //
  134. // /**
  135. // * 设置第二标题内容
  136. // *
  137. // * @param title 第二标题
  138. // * @param document 文档
  139. // * @param alignment 对齐方式
  140. // * @param firstLineIndent 第一行缩进
  141. // * @param spacingAfter 之后间隔
  142. // * @throws Exception
  143. // */
  144. // public static void setPdfSecondTitle(String title, Document document, int alignment, int firstLineIndent, int spacingAfter) throws Exception {
  145. // Paragraph lsh = new Paragraph(title, getPdfChineseFont(1));
  146. // lsh.setAlignment(alignment);
  147. // lsh.setFirstLineIndent(firstLineIndent);
  148. // lsh.setSpacingAfter(spacingAfter);
  149. // document.add(lsh);
  150. // }
  151. //
  152. // /**
  153. // * 设置表格内容 并将表格加入文档中
  154. // *
  155. // * @param dates 数据
  156. // * @param document 文档
  157. // * @param table 表格
  158. // * @throws Exception 异常
  159. // */
  160. // public static void setPdfTableContent(Object[][] dates, Document document, PdfPTable table) throws Exception {
  161. // for (int i = 0; i < dates.length; i++) {
  162. // for (int j = 0; j < dates[i].length; j++) {
  163. // //表格的单元格
  164. // PdfPCell pdfCell = new PdfPCell();
  165. // //设置表格行高
  166. // if (i > 0 && i < 3) {
  167. // pdfCell.setMinimumHeight(50);
  168. // } else {
  169. // pdfCell.setMinimumHeight(25);
  170. // }
  171. // Paragraph paragraph = new Paragraph(dates[i][j] + "", getPdfChineseFont(1));
  172. // pdfCell.setPhrase(paragraph);
  173. //
  174. // setCellStyle(pdfCell);
  175. // table.addCell(pdfCell);
  176. // }
  177. // }
  178. //
  179. // document.add(table);
  180. // }
  181. //
  182. // /**
  183. // * 设置水印
  184. // *
  185. // * @param inputFile 要这设置水印的文件路径
  186. // * @param imageFile 水印图片路径
  187. // * @param tag 标记
  188. // * @return 生成水印文件的路径
  189. // */
  190. // public static String imageWaterMark(String inputFile, String imageFile, String tag) throws Exception {
  191. // String[] spe = separatePath(inputFile);
  192. // String outputFile = spe[0] + tag + "." + spe[1];
  193. //
  194. // PdfReader reader = new PdfReader(inputFile);
  195. // PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
  196. //
  197. // int total = reader.getNumberOfPages() + 1;
  198. //
  199. // Image image = Image.getInstance(imageFile);
  200. // //坐标
  201. // image.setAbsolutePosition(389, 730);
  202. // //自定义大小
  203. // image.scaleAbsolute(80, 80);
  204. // //旋转 弧度
  205. // image.setRotation(-55);
  206. //
  207. // //旋转 角度
  208. // image.setRotationDegrees(0);
  209. // //依照比例缩放
  210. // image.scalePercent(50);
  211. //
  212. // PdfGState gs = new PdfGState();
  213. // // 设置透明度为0.2
  214. // gs.setFillOpacity(0.2f);
  215. //
  216. //
  217. // PdfContentByte under;
  218. // //给每一页加水印
  219. // for (int i = 1; i < total; i++) {
  220. // under = stamper.getUnderContent(i);
  221. // under.beginText();
  222. // // 添加水印图片
  223. // under.addImage(image);
  224. // under.setGState(gs);
  225. // }
  226. // stamper.close();
  227. // reader.close();
  228. // return outputFile;
  229. // }
  230. //
  231. // /**
  232. // * 拆分原文件路径
  233. // *
  234. // * @param path 原文件路径
  235. // * @return 拆分原路径的数组
  236. // */
  237. // public static String[] separatePath(String path) {
  238. // if (StringUtils.isBlank(path)) {
  239. // return null;
  240. // }
  241. // String[] sep = path.split("\\.");
  242. // return new String[]{sep[0], sep[1]};
  243. // }
  244. //
  245. // /**
  246. // * 设置表格单元格样式
  247. // *
  248. // * @param pdfCell 单元格对象
  249. // */
  250. // public static void setCellStyle(PdfPCell pdfCell) {
  251. // pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  252. // pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  253. // pdfCell.setBackgroundColor(new BaseColor(0xdd7e6b));
  254. // pdfCell.setBorderWidthTop(0.1f);
  255. // pdfCell.setBorderWidthBottom(0.1f);
  256. // pdfCell.setBorderWidthLeft(0.1f);
  257. // pdfCell.setBorderWidthRight(0.1f);
  258. // }
  259. //
  260. // /**
  261. // * 设置pdf字体及大小
  262. // *
  263. // * @param type 标题或者内容 0-内容 1-标题
  264. // * @return 字体
  265. // * @throws Exception 异常
  266. // */
  267. // public static Font getPdfChineseFont(int type) throws Exception {
  268. // // 使用系统字体
  269. // BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  270. // Font font;
  271. // if (type == 1) {
  272. // font = new Font(bfChinese);
  273. // } else {
  274. // font = new Font(bfChinese, 18, Font.BOLD);
  275. // }
  276. //
  277. // return font;
  278. // }
  279. //
  280. // /**
  281. // * 删除文件夹
  282. // *
  283. // * @param folderPath 文件路基
  284. // */
  285. // public static void delFolder(String folderPath) {
  286. // // 删除完里面所有内容
  287. // delAllFile(folderPath);
  288. // String filePath = folderPath;
  289. // filePath = filePath.toString();
  290. // java.io.File myFilePath = new java.io.File(filePath);
  291. // // 删除空文件夹
  292. // myFilePath.delete();
  293. // }
  294. //
  295. // /**
  296. // * 删除指定文件夹下所有文件
  297. // *
  298. // * @param path 文件路基
  299. // * @return 是否成功
  300. // */
  301. // public static boolean delAllFile(String path) {
  302. // boolean flag = false;
  303. // File file = new File(path);
  304. // String[] tempList = file.list();
  305. // File temp = null;
  306. // for (int i = 0; i < tempList.length; i++) {
  307. // if (path.endsWith(File.separator)) {
  308. // temp = new File(path + tempList[i]);
  309. // } else {
  310. // temp = new File(path + File.separator + tempList[i]);
  311. // }
  312. // if (temp.isFile()) {
  313. // temp.delete();
  314. // }
  315. // if (temp.isDirectory()) {
  316. // // 先删除文件夹里面的文件
  317. // delAllFile(path + "/" + tempList[i]);
  318. // // 再删除空文件夹
  319. // delFolder(path + "/" + tempList[i]);
  320. // flag = true;
  321. // }
  322. // }
  323. // return flag;
  324. // }
  325. // public static boolean delAllFile(String path) {
  326. // boolean flag = false;
  327. // File file = new File(path);
  328. // File temp = null;
  329. //
  330. // if (temp.isFile()) {
  331. // temp.delete();
  332. // }
  333. // return flag;
  334. // }
  335. }