PdfUtil.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. package com.xunmei.file.utils;
  2. import cn.hutool.core.collection.ListUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.extra.spring.SpringUtil;
  5. import com.alibaba.fastjson2.JSON;
  6. import com.lowagie.text.*;
  7. import com.lowagie.text.pdf.*;
  8. import com.xunmei.common.core.constant.DictConstants;
  9. import com.xunmei.common.core.domain.iot.domain.IotAlarmSubsystem;
  10. import com.xunmei.common.security.utils.DictUtils;
  11. import com.xunmei.file.vo.ItextPdfTableVo;
  12. import com.xunmei.system.api.domain.AccessDataVo;
  13. import com.xunmei.system.api.domain.CheckDataVo;
  14. import com.xunmei.system.api.domain.SafeCheckTaskRegisterBookVo;
  15. import com.xunmei.system.api.domain.SysDictData;
  16. import com.xunmei.system.api.enums.ProtectionStatus;
  17. import io.netty.util.internal.StringUtil;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.io.FileUtils;
  20. import org.springframework.context.ApplicationContext;
  21. import org.springframework.core.io.Resource;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. @Slf4j
  30. public class PdfUtil {
  31. public static Document createDocument(float marginLeft, float marginRight, float marginTop, float marginBottom) {
  32. return new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom);
  33. }
  34. public static ItextPdfTableVo createTable(String filename, int numColumns, int fontSize) throws Exception {
  35. Document document = createDocument(0, 0, 50, 50);
  36. File file = FileUtils.getFile(filename);
  37. FileOutputStream fos = new FileOutputStream(file);
  38. final PdfWriter writer = PdfWriter.getInstance(document, fos);
  39. document.open();
  40. // 使用语言包字
  41. BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  42. // 外部字体
  43. BaseFont fs = BaseFont.createFont("/fonts/msyh.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  44. Font tableFont = new Font(fs, fontSize, Font.NORMAL);
  45. PdfPTable table = new PdfPTable(numColumns);
  46. // 设置各列列宽
  47. // table.setTotalWidth(new float[]{90, 100, 100, 120, 100, 100});
  48. table.setSpacingBefore(16f);
  49. table.setSplitRows(true);
  50. table.setSplitLate(false);
  51. ItextPdfTableVo itextPdfTableVo = new ItextPdfTableVo();
  52. itextPdfTableVo.setDocument(document);
  53. itextPdfTableVo.setWriter(writer);
  54. itextPdfTableVo.setAbf(abf);
  55. itextPdfTableVo.setFs(fs);
  56. itextPdfTableVo.setTableFont(tableFont);
  57. itextPdfTableVo.setTable(table);
  58. return itextPdfTableVo;
  59. }
  60. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan) {
  61. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan);
  62. table.addCell(cell);
  63. return cell;
  64. }
  65. public static PdfPCell createPDFCell(Font tableFont, PdfPTable table, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  66. final PdfPCell cell = cell(tableFont, content, align, colspan, rowspan, lineSpacing);
  67. table.addCell(cell);
  68. return cell;
  69. }
  70. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan) {
  71. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  72. if (colspan != null && colspan > 0) {
  73. cell.setColspan(colspan);
  74. }
  75. if (rowspan != null && rowspan > 0) {
  76. cell.setRowspan(rowspan);
  77. }
  78. // cell.setPaddingTop(8f);
  79. cell.setPaddingLeft(8f);
  80. cell.setPaddingRight(8f);
  81. cell.setPaddingBottom(8f);
  82. cell.setLeading(1F, 1.5F);
  83. if (PdfPCell.ALIGN_MIDDLE != align) {
  84. cell.setHorizontalAlignment(align);
  85. } else {
  86. //设置单元格的垂直方向对齐方式
  87. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
  88. //设置单元格的水平方向对齐方式
  89. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  90. }
  91. return cell;
  92. }
  93. private static PdfPCell cell(Font tableFont, String content, int align, Integer colspan, Integer rowspan, Integer lineSpacing) {
  94. PdfPCell cell = new PdfPCell(new Phrase(content, tableFont));
  95. if (colspan != null && colspan > 0) {
  96. cell.setColspan(colspan);
  97. }
  98. if (rowspan != null && rowspan > 0) {
  99. cell.setRowspan(rowspan);
  100. }
  101. cell.setNoWrap(false);
  102. cell.setLeading(lineSpacing, 0);
  103. if (PdfPCell.ALIGN_MIDDLE != align) {
  104. cell.setHorizontalAlignment(align);
  105. cell.setPaddingLeft(8f);
  106. cell.setPaddingRight(8f);
  107. cell.setPaddingBottom(8f);
  108. } else {
  109. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//设置单元格的垂直对齐方式
  110. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  111. }
  112. return cell;
  113. }
  114. /**
  115. * 生成文字类型的水印
  116. */
  117. public static void createWatermark(Document document, PdfWriter writer, Font waterMarkFont, String content) throws DocumentException, IOException {
  118. PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
  119. Phrase phrase = new Phrase(content, waterMarkFont);
  120. float pageWidth = document.right() + document.left();//获取pdf内容正文页面宽度
  121. float pageHeight = document.top() + document.bottom();//获取pdf内容正文页面高度
  122. //两行三列
  123. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  124. pageWidth * 0.25f, pageHeight * 0.2f, 45);
  125. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  126. pageWidth * 0.25f, pageHeight * 0.5f, 45);
  127. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  128. pageWidth * 0.25f, pageHeight * 0.8f, 45);
  129. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  130. pageWidth * 0.65f, pageHeight * 0.2f, 45);
  131. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  132. pageWidth * 0.65f, pageHeight * 0.5f, 45);
  133. ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER, phrase,
  134. pageWidth * 0.65f, pageHeight * 0.8f, 45);
  135. }
  136. public static void dealHeader(Document document, BaseFont fs, String title, int fontSize) throws DocumentException {
  137. //文件title
  138. Paragraph p = new Paragraph(title, new Font(fs, fontSize, Font.NORMAL));
  139. p.setAlignment(Paragraph.ALIGN_CENTER);
  140. // 文件title 段落下空白
  141. p.setSpacingAfter(10f);
  142. p.setAlignment(Element.ALIGN_CENTER);
  143. document.add(p);
  144. }
  145. public static void dealEduBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  146. //第一行
  147. createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 1, 1);
  148. createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
  149. createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
  150. createPDFCell(tableFont, table, data.get("address").toString(), Element.ALIGN_MIDDLE, 2, 1);
  151. //第二行
  152. createPDFCell(tableFont, table, "主持人", Element.ALIGN_MIDDLE, 1, 1);
  153. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  154. createPDFCell(tableFont, table, "记录人", Element.ALIGN_MIDDLE, 1, 1);
  155. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  156. //内容
  157. createPDFCell(tableFont, table, "内容", Element.ALIGN_MIDDLE, 1, 1);
  158. createPDFCell(tableFont, table, data.get("content").toString(), Element.ALIGN_LEFT, 5, 1);
  159. createPDFCell(tableFont, table, "总结", Element.ALIGN_MIDDLE, 1, 1);
  160. createPDFCell(tableFont, table, data.get("note").toString(), Element.ALIGN_LEFT, 5, 1);
  161. createPDFCell(tableFont, table, "参会人员签字", Element.ALIGN_MIDDLE, 1, 1);
  162. dealEduImageCell((List<String>) data.get("signImage"), table, 5, 80, 30);
  163. document.add(table);
  164. //第二页
  165. //按6份等分图片数组,一页只显示6张
  166. final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 6);
  167. for (List<String> stringList : listList) {
  168. PdfPTable innerTable = new PdfPTable(6);
  169. createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  170. //一行展示一张图片
  171. dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
  172. document.newPage();
  173. document.add(innerTable);
  174. }
  175. }
  176. public static void dealResumptionBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  177. // 第一行
  178. PdfUtil.createPDFCell(tableFont, table, "单位名称", Element.ALIGN_MIDDLE, 2, 0);
  179. PdfUtil.createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_MIDDLE, 3, 0);
  180. PdfUtil.createPDFCell(tableFont, table, "检查时间", Element.ALIGN_MIDDLE, 2, 0);
  181. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isNotEmpty(data.get("submiterNames")) ? data.get("dateStr").toString() : "", Element.ALIGN_MIDDLE, 3, 0);
  182. // 第二行
  183. PdfUtil.createPDFCell(tableFont, table, "检查人", Element.ALIGN_MIDDLE, 2, 0);
  184. PdfUtil.createPDFCell(tableFont, table, data.get("submiterNames").toString(), Element.ALIGN_MIDDLE, 8, 0);
  185. // 第三行
  186. PdfUtil.createPDFCell(tableFont, table, "检查时间", Element.ALIGN_MIDDLE, 2, 0);
  187. PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 6, 0);
  188. PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 0);
  189. final List<SysDictData> dictCache = DictUtils.getDictCache(DictConstants.RESUMPTION_PLAN_EXEC);
  190. final List<String> names = dictCache.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
  191. for (String s : names) {
  192. List<LinkedHashMap<String, Object>> lists = (List<LinkedHashMap<String, Object>>) data.get(s);
  193. if (ObjectUtil.isEmpty(lists)) {
  194. //不同的执行时刻
  195. continue;
  196. }
  197. //不同的执行时刻
  198. PdfUtil.createPDFCell(tableFont, table, getLineStr(s), PdfPCell.ALIGN_MIDDLE, 2, lists.size());
  199. int o = 1;
  200. for (LinkedHashMap<String, Object> listVo : lists) {
  201. // 检查内容
  202. String rowContent = o + "、" + listVo.get("pointName");
  203. PdfUtil.createPDFCell(tableFont, table, rowContent, Element.ALIGN_LEFT, 6, 0);
  204. // 检查情况
  205. PdfUtil.createPDFCell(tableFont, table, ObjectUtil.isEmpty(listVo.get("resValue")) ? "" : (((Integer) listVo.get("resValue")) == 0 ? "正常" : "异常"), Element.ALIGN_MIDDLE, 2, 0);
  206. // 检查人
  207. //PdfUtil.createPDFCell(tableFont, table, String.valueOf(listVo.get("submitName")), Element.ALIGN_CENTER, 0, 0);
  208. o++;
  209. }
  210. }
  211. //日常登记簿-布撤防数据
  212. PdfUtil.createPDFCell(tableFont, table, "布撤防情况", Element.ALIGN_MIDDLE, 10, 1);
  213. PdfUtil.createPDFCell(tableFont, table, "控制器", Element.ALIGN_MIDDLE, 4, 1);
  214. PdfUtil.createPDFCell(tableFont, table, "状态", Element.ALIGN_MIDDLE, 3, 1);
  215. PdfUtil.createPDFCell(tableFont, table, "时间", Element.ALIGN_MIDDLE, 3, 1);
  216. List<Map<String, List<Map<String, Object>>>> protectionList = (List<Map<String, List<Map<String, Object>>>>) data.get("protection");
  217. if (ObjectUtil.isEmpty(protectionList)) {
  218. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 4, 1);
  219. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 3, 1);
  220. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 3, 1);
  221. } else {
  222. for (Map<String, List<Map<String, Object>>> listMap : protectionList) {
  223. for (Map.Entry<String, List<Map<String, Object>>> listEntry : listMap.entrySet()) {
  224. IotAlarmSubsystem subsystem = JSON.parseObject(listEntry.getKey(), IotAlarmSubsystem.class);
  225. List<Map<String, Object>> value = listEntry.getValue();
  226. //布防状态,0:撤防,1:布防
  227. if (value.isEmpty()) {
  228. //子系统名称
  229. PdfUtil.createPDFCell(tableFont, table, subsystem.getName(), Element.ALIGN_MIDDLE, 4, 1);
  230. //状态
  231. PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_MIDDLE, 3, 1);
  232. // 时间
  233. PdfUtil.createPDFCell(tableFont, table, StringUtil.EMPTY_STRING, Element.ALIGN_MIDDLE, 3, 1);
  234. } else {
  235. //子系统名称
  236. PdfUtil.createPDFCell(tableFont, table, subsystem.getName(), Element.ALIGN_MIDDLE, 4, value.size());
  237. for (Map<String, Object> map : value) {
  238. //状态
  239. PdfUtil.createPDFCell(tableFont, table, getStatusText(map), Element.ALIGN_MIDDLE, 3, 1);
  240. //时间
  241. PdfUtil.createPDFCell(tableFont, table, getStatusTime(map), Element.ALIGN_MIDDLE, 3, 1);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 10, 1);
  248. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  249. PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 4, 1);
  250. PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 4, 1);
  251. final List<LinkedHashMap> questionPdfVoList = (List<LinkedHashMap>) data.get("questions");
  252. if (ObjectUtil.isEmpty(questionPdfVoList)) {
  253. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  254. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 4, 1);
  255. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 4, 1);
  256. document.add(table);
  257. return;
  258. }
  259. int questionIndex = 1;
  260. for (int i = 0; i < questionPdfVoList.size(); i++) {
  261. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
  262. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("checkContent")), Element.ALIGN_MIDDLE, 4, 1);
  263. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionPdfVoList.get(i).get("questionDesc")), Element.ALIGN_MIDDLE, 4, 1);
  264. }
  265. document.add(table);
  266. }
  267. private static String getStatusTime(Map<String, Object> value) {
  268. if (value == null) {
  269. return StringUtil.EMPTY_STRING;
  270. }
  271. Object status = value.get("date");
  272. return status == null ? StringUtil.EMPTY_STRING : status.toString();
  273. }
  274. private static String getStatusText(Map<String, Object> value) {
  275. if (value == null) {
  276. return StringUtil.EMPTY_STRING;
  277. }
  278. Object status = value.get("status");
  279. if (status == null) {
  280. return StringUtil.EMPTY_STRING;
  281. }
  282. for (ProtectionStatus protectionStatus : ProtectionStatus.values()) {
  283. if (ObjectUtil.equal(protectionStatus.ordinal(), Integer.parseInt(status.toString()))) {
  284. return protectionStatus.getText();
  285. }
  286. }
  287. return StringUtil.EMPTY_STRING;
  288. }
  289. public static String getLineStr(String str) {
  290. StringBuilder result = new StringBuilder();
  291. for (int i = 0; i < str.length(); i++) {
  292. result.append(str.charAt(i)).append("\r");
  293. }
  294. return result.toString();
  295. }
  296. public static void dealOutInBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  297. table.setSplitLate(false);
  298. table.setSplitRows(true);
  299. //第一行
  300. createPDFCell(tableFont, table, "接待机构", Element.ALIGN_MIDDLE, 1, 1);
  301. createPDFCell(tableFont, table, data.get("inOrg").toString(), Element.ALIGN_MIDDLE, 2, 1);
  302. createPDFCell(tableFont, table, "接待日期", Element.ALIGN_MIDDLE, 1, 1);
  303. createPDFCell(tableFont, table, data.get("time").toString(), Element.ALIGN_MIDDLE, 2, 1);
  304. //第二行
  305. createPDFCell(tableFont, table, "来访事由", Element.ALIGN_MIDDLE, 1, 1);
  306. createPDFCell(tableFont, table, data.get("reasons").toString(), Element.ALIGN_MIDDLE, 2, 1);
  307. createPDFCell(tableFont, table, "陪同人员", Element.ALIGN_MIDDLE, 1, 1);
  308. createPDFCell(tableFont, table, data.get("accompanyingPerson").toString(), Element.ALIGN_MIDDLE, 2, 1);
  309. //第三行
  310. createPDFCell(tableFont, table, "来访单位", Element.ALIGN_MIDDLE, 1, 1);
  311. createPDFCell(tableFont, table, data.get("outOrgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  312. createPDFCell(tableFont, table, "来访人员", Element.ALIGN_MIDDLE, 1, 1);
  313. createPDFCell(tableFont, table, data.get("userName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  314. //第四行
  315. createPDFCell(tableFont, table, "证件类型", Element.ALIGN_MIDDLE, 1, 1);
  316. createPDFCell(tableFont, table, data.get("idType").toString(), Element.ALIGN_MIDDLE, 2, 1);
  317. createPDFCell(tableFont, table, "证件号码", Element.ALIGN_MIDDLE, 1, 1);
  318. createPDFCell(tableFont, table, data.get("idCard").toString(), Element.ALIGN_MIDDLE, 2, 1);
  319. //第五行
  320. createPDFCell(tableFont, table, "进入时间", Element.ALIGN_MIDDLE, 1, 1);
  321. createPDFCell(tableFont, table, data.get("inTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
  322. createPDFCell(tableFont, table, "离开时间", Element.ALIGN_MIDDLE, 1, 1);
  323. createPDFCell(tableFont, table, data.get("outTime").toString(), Element.ALIGN_MIDDLE, 2, 1);
  324. //第六行
  325. createPDFCell(tableFont, table, "审批人", Element.ALIGN_MIDDLE, 1, 1);
  326. createPDFCell(tableFont, table, data.get("approveUser").toString(), Element.ALIGN_MIDDLE, 2, 1);
  327. createPDFCell(tableFont, table, "登记人", Element.ALIGN_MIDDLE, 1, 1);
  328. createPDFCell(tableFont, table, data.get("createBy").toString(), Element.ALIGN_MIDDLE, 2, 1);
  329. //第七行
  330. createPDFCell(tableFont, table, "审批人签字", Element.ALIGN_MIDDLE, 1, 1);
  331. dealImageCell((List<String>) data.get("approveSign"), table, 1, 80, 30, 2, 1);
  332. createPDFCell(tableFont, table, "登记人签字", Element.ALIGN_MIDDLE, 1, 1);
  333. dealImageCell((List<String>) data.get("submitSign"), table, 1, 80, 30, 2, 1);
  334. // /statics/2023/12/05/20231205183106A001.png
  335. //证件图片
  336. createPDFCell(tableFont, table, "证件图片", Element.ALIGN_MIDDLE, 1, 1);
  337. //证件图片 图片填充
  338. final PdfPTable imageTable1 = getImage((List<String>) data.get("imageFile"), 2, 190, 130, 2);
  339. final PdfPCell cell1 = new PdfPCell();
  340. cell1.setNoWrap(false);
  341. cell1.setPaddingLeft(4f);
  342. cell1.setPaddingRight(4f);
  343. cell1.setPaddingBottom(4f);
  344. cell1.setPaddingTop(4f);
  345. cell1.setColspan(5);
  346. cell1.setRowspan(1);
  347. //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  348. cell1.addElement(imageTable1);
  349. table.addCell(cell1);
  350. createPDFCell(tableFont, table, "身份核验材料", Element.ALIGN_MIDDLE, 1, 1);
  351. final PdfPTable imageTable3 = getImage((List<String>) data.get("checkImage"), 2, 190, 190, 2);
  352. final PdfPCell cell3 = new PdfPCell();
  353. cell3.setNoWrap(false);
  354. cell3.setPaddingLeft(4f);
  355. cell3.setPaddingRight(4f);
  356. cell3.setPaddingBottom(4f);
  357. cell3.setPaddingTop(4f);
  358. cell3.setColspan(5);
  359. cell3.setRowspan(1);
  360. //cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  361. cell3.addElement(imageTable3);
  362. table.addCell(cell3);
  363. document.add(table);
  364. PdfPTable innerTable = new PdfPTable(6);
  365. createPDFCell(tableFont, innerTable, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  366. // createPDFCell(tableFont, table, "介绍信附件", Element.ALIGN_MIDDLE, 1, 1);
  367. final PdfPTable imageTable2 = getImage((List<String>) data.get("file"), 2, 380, 320, 1);
  368. final PdfPCell cell2 = new PdfPCell();
  369. cell2.setNoWrap(false);
  370. cell2.setPaddingLeft(4f);
  371. cell2.setPaddingRight(4f);
  372. cell2.setPaddingBottom(4f);
  373. cell2.setPaddingTop(4f);
  374. cell2.setColspan(5);
  375. cell2.setRowspan(1);
  376. cell2.addElement(imageTable2);
  377. innerTable.addCell(cell2);
  378. document.newPage();
  379. document.add(innerTable);
  380. }
  381. public static void dealDrillBody(Document document, PdfPTable table, Font tableFont, Map<String, Object> data) throws Exception {
  382. //第一行
  383. createPDFCell(tableFont, table, "演练单位", Element.ALIGN_MIDDLE, 1, 1);
  384. createPDFCell(tableFont, table, data.get("orgName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  385. createPDFCell(tableFont, table, "地点", Element.ALIGN_MIDDLE, 1, 1);
  386. createPDFCell(tableFont, table, data.get("drillSite").toString(), Element.ALIGN_MIDDLE, 2, 1);
  387. //第二行
  388. createPDFCell(tableFont, table, "指挥人", Element.ALIGN_MIDDLE, 1, 1);
  389. createPDFCell(tableFont, table, data.get("hostName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  390. createPDFCell(tableFont, table, "记录人", Element.ALIGN_MIDDLE, 1, 1);
  391. createPDFCell(tableFont, table, data.get("recorderName").toString(), Element.ALIGN_MIDDLE, 2, 1);
  392. //第三行
  393. createPDFCell(tableFont, table, "演练时间", Element.ALIGN_MIDDLE, 1, 1);
  394. createPDFCell(tableFont, table, data.get("drillTime").toString(), Element.ALIGN_LEFT, 5, 1);
  395. createPDFCell(tableFont, table, "演练项目", Element.ALIGN_MIDDLE, 1, 1);
  396. createPDFCell(tableFont, table, data.get("typeText").toString(), Element.ALIGN_LEFT, 5, 1);
  397. //预设案由
  398. createPDFCell(tableFont, table, "预设案由", Element.ALIGN_MIDDLE, 1, 1);
  399. createPDFCell(tableFont, table, data.get("presetCase").toString(), Element.ALIGN_LEFT, 5, 1);
  400. //演练情况
  401. createPDFCell(tableFont, table, "演练情况", Element.ALIGN_MIDDLE, 1, 1);
  402. createPDFCell(tableFont, table, data.get("drillSituation").toString(), Element.ALIGN_LEFT, 5, 1);
  403. //点评总结
  404. createPDFCell(tableFont, table, "点评总结", Element.ALIGN_MIDDLE, 1, 1);
  405. createPDFCell(tableFont, table, data.get("comment").toString(), Element.ALIGN_LEFT, 5, 1);
  406. //参会人员签字
  407. createPDFCell(tableFont, table, "参演人员", Element.ALIGN_MIDDLE, 1, 1);
  408. dealEduImageCell((List<String>) data.get("signImage"), table, 5, 80, 30);
  409. document.add(table);
  410. //第二页
  411. //按6份等分图片数组,一页只显示6张
  412. final List<List<String>> listList = ListUtil.split((List<String>) data.get("fileImage"), 6);
  413. for (List<String> stringList : listList) {
  414. PdfPTable innerTable = new PdfPTable(6);
  415. createPDFCell(tableFont, innerTable, "图片附件", Element.ALIGN_MIDDLE, 1, 1);
  416. //一行展示一张图片
  417. dealEduImageCell(new ArrayList<>(stringList), innerTable, 2, 185, 200);
  418. document.newPage();
  419. document.add(innerTable);
  420. }
  421. }
  422. public static void dealEduImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh) throws Exception {
  423. //签字区域
  424. PdfPCell outCell = new PdfPCell();
  425. outCell.setNoWrap(false);
  426. outCell.setColspan(6);
  427. // outCell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  428. final int imageAddNums = imageList.size() % imageNumsOfRow;
  429. if (imageAddNums != 0) {
  430. for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
  431. imageList.add("black.png");
  432. }
  433. }
  434. PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
  435. imageInnerTable.setSplitRows(true);
  436. imageInnerTable.setSplitLate(false);
  437. imageInnerTable.setWidthPercentage(100F);
  438. for (String image : imageList) {
  439. Image imageData = convertFileToByteArray(new File(image));
  440. if (imageData != null) {
  441. imageData.scaleAbsolute(imageWith, imageHigh);
  442. }
  443. PdfPCell innerCell = new PdfPCell(imageData);
  444. innerCell.setNoWrap(false);
  445. innerCell.setPaddingTop(8f);
  446. innerCell.setPaddingLeft(8f);
  447. innerCell.setPaddingRight(8f);
  448. innerCell.setPaddingBottom(8f);
  449. innerCell.setBorder(Rectangle.NO_BORDER);
  450. innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  451. innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  452. imageInnerTable.addCell(innerCell);
  453. }
  454. outCell.addElement(imageInnerTable);
  455. table.addCell(outCell);
  456. }
  457. public static void dealImageCell(List<String> imageList, PdfPTable table, Integer imageNumsOfRow, Integer imageWith, Integer imageHigh, Integer cosSpan, Integer rowSpan) throws Exception {
  458. //签字区域
  459. PdfPCell outCell = new PdfPCell();
  460. outCell.setNoWrap(false);
  461. outCell.setColspan(cosSpan);
  462. outCell.setRowspan(rowSpan);
  463. final int imageAddNums = imageList.size() % imageNumsOfRow;
  464. if (imageAddNums != 0) {
  465. for (int i = 0; i < imageNumsOfRow - imageAddNums; i++) {
  466. imageList.add("black.png");
  467. }
  468. }
  469. PdfPTable imageInnerTable = new PdfPTable(imageNumsOfRow);
  470. imageInnerTable.setSplitRows(true);
  471. imageInnerTable.setSplitLate(false);
  472. imageInnerTable.setWidthPercentage(100F);
  473. for (String image : imageList) {
  474. Image imageData = convertFileToByteArray(new File(image));
  475. if (imageData != null) {
  476. imageData.scaleAbsolute(imageWith, imageHigh);
  477. }
  478. PdfPCell innerCell = new PdfPCell(imageData);
  479. innerCell.setNoWrap(false);
  480. innerCell.setPaddingTop(8f);
  481. innerCell.setPaddingLeft(8f);
  482. innerCell.setPaddingRight(8f);
  483. innerCell.setPaddingBottom(8f);
  484. innerCell.setBorder(Rectangle.NO_BORDER);
  485. innerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  486. innerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  487. imageInnerTable.addCell(innerCell);
  488. }
  489. outCell.addElement(imageInnerTable);
  490. table.addCell(outCell);
  491. }
  492. private static PdfPTable getImage(List<String> images, int totalImages, float imageWidth, float imageHeight, Integer ImageNumsOfRow) throws Exception {
  493. if (images == null) {
  494. images = new ArrayList<>();
  495. }
  496. PdfPTable innerTable = new PdfPTable(ImageNumsOfRow);
  497. innerTable.setWidthPercentage(100f);
  498. innerTable.setSplitRows(true);
  499. innerTable.setSplitLate(false);
  500. //这里根据实际图片数量来判断是否需要补充白色图片,保证每行显示3张图片,用以填充空白
  501. final int reallySize = images.size();
  502. List<String> list = new ArrayList<>(images);
  503. for (int i = 0; i < totalImages - reallySize && totalImages > reallySize; i++) {
  504. list.add("black.png");
  505. }
  506. //分割,每行显示3张图片,获取分割的行数
  507. List<List<String>> rows = new ArrayList<>();
  508. for (int i = 0; i < list.size(); i += ImageNumsOfRow) {
  509. List<String> row = list.subList(i, Math.min(i + ImageNumsOfRow, list.size()));
  510. rows.add(row);
  511. }
  512. for (List<String> row : rows) {
  513. for (String image : row) {
  514. Image imageData = null;
  515. imageData = convertFileToByteArray(new File(image));
  516. if (imageData != null) {
  517. imageData.scaleAbsolute(imageWidth, imageHeight);
  518. }
  519. PdfPCell cell = new PdfPCell(imageData);
  520. cell.setBorder(Rectangle.NO_BORDER);
  521. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  522. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  523. innerTable.addCell(cell);
  524. }
  525. }
  526. PdfPTable outerTable = new PdfPTable(1);
  527. PdfPCell innerCell = new PdfPCell(innerTable);
  528. innerCell.setBorder(Rectangle.NO_BORDER);
  529. outerTable.setSplitRows(true);
  530. outerTable.setSplitLate(false);
  531. outerTable.addCell(innerCell);
  532. outerTable.setWidthPercentage(100f);
  533. return outerTable;
  534. }
  535. public static Image convertFileToByteArray(File file) throws Exception {
  536. try {
  537. FileInputStream fis = new FileInputStream(file);
  538. byte[] byteArray = new byte[(int) file.length()];
  539. fis.read(byteArray);
  540. fis.close();
  541. return Image.getInstance(byteArray);
  542. } catch (IOException e) {
  543. return getLocalImage();
  544. }
  545. }
  546. private static Image getLocalImage() {
  547. try {
  548. final ApplicationContext applicationContext = SpringUtil.getApplicationContext();
  549. final Resource[] resources = applicationContext.getResources("classpath:file/black.png");
  550. if (resources == null || resources.length == 0) {
  551. return null;
  552. }
  553. return Image.getInstance(resources[0].getURL());
  554. } catch (IOException | BadElementException e) {
  555. throw new RuntimeException(e);
  556. }
  557. }
  558. public static void dealAccessPBody(Document document, PdfPTable table, Font tableFont, Font tableTitleFont, List<AccessDataVo> data) throws DocumentException {
  559. PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  560. PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
  561. PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
  562. PdfUtil.createPDFCell(tableTitleFont, table, "调阅情况", Element.ALIGN_MIDDLE, 6, 1);
  563. List<AccessDataVo> normalDataList = data.stream().filter(d -> ObjectUtil.equal(0, d.getStatus())).collect(Collectors.toList());
  564. if (ObjectUtil.isNotEmpty(normalDataList)) {
  565. for (int i = 1; i <= normalDataList.size(); i++) {
  566. AccessDataVo accessDataVo = normalDataList.get(i - 1);
  567. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
  568. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
  569. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
  570. PdfUtil.createPDFCell(tableFont, table, "正常", Element.ALIGN_MIDDLE, 6, 1);
  571. }
  572. } else {
  573. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  574. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  575. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
  576. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  577. }
  578. PdfUtil.createPDFCell(tableTitleFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 19, 1);
  579. PdfUtil.createPDFCell(tableTitleFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  580. PdfUtil.createPDFCell(tableTitleFont, table, "区域名称", Element.ALIGN_MIDDLE, 6, 1);
  581. PdfUtil.createPDFCell(tableTitleFont, table, "调阅项目", Element.ALIGN_MIDDLE, 5, 1);
  582. PdfUtil.createPDFCell(tableTitleFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
  583. List<AccessDataVo> exceptionDataList = data.stream().filter(d -> ObjectUtil.equal(1, d.getStatus())).collect(Collectors.toList());
  584. if (ObjectUtil.isNotEmpty(exceptionDataList)) {
  585. for (int i = 1; i <= exceptionDataList.size(); i++) {
  586. AccessDataVo accessDataVo = exceptionDataList.get(i - 1);
  587. PdfUtil.createPDFCell(tableFont, table, String.valueOf(i), Element.ALIGN_MIDDLE, 2, 1);
  588. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAreaName(), Element.ALIGN_MIDDLE, 6, 1);
  589. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getProject(), Element.ALIGN_MIDDLE, 5, 1);
  590. PdfUtil.createPDFCell(tableFont, table, accessDataVo.getAbnormalIllustrate(), Element.ALIGN_MIDDLE, 6, 1);
  591. }
  592. } else {
  593. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  594. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  595. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 5, 1);
  596. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  597. }
  598. }
  599. public static void dealSafeCheckPBody(Document document, PdfPTable table, Font
  600. tableFont, SafeCheckTaskRegisterBookVo data) throws Exception {
  601. PdfUtil.createPDFCell(tableFont, table, "被查单位", Element.ALIGN_MIDDLE, 3, 1);
  602. PdfUtil.createPDFCell(tableFont, table, data.getOrgName(), Element.ALIGN_MIDDLE, 4, 1);
  603. PdfUtil.createPDFCell(tableFont, table, "检查日期", Element.ALIGN_MIDDLE, 3, 1);
  604. PdfUtil.createPDFCell(tableFont, table, data.getDateStr(), Element.ALIGN_MIDDLE, 4, 1);
  605. PdfUtil.createPDFCell(tableFont, table, "检查类型", Element.ALIGN_MIDDLE, 3, 1);
  606. PdfUtil.createPDFCell(tableFont, table, data.getCheckTypeText(), Element.ALIGN_MIDDLE, 4, 1);
  607. PdfUtil.createPDFCell(tableFont, table, "检查名称", Element.ALIGN_MIDDLE, 3, 1);
  608. PdfUtil.createPDFCell(tableFont, table, data.getTaskTitle(), Element.ALIGN_MIDDLE, 4, 1);
  609. PdfUtil.createPDFCell(tableFont, table, "检查单位", Element.ALIGN_MIDDLE, 3, 1);
  610. PdfUtil.createPDFCell(tableFont, table, data.getCheckOrgName(), Element.ALIGN_MIDDLE, 4, 1);
  611. PdfUtil.createPDFCell(tableFont, table, "检查组成员", Element.ALIGN_MIDDLE, 3, 1);
  612. PdfUtil.createPDFCell(tableFont, table, data.getCheckTeam(), Element.ALIGN_MIDDLE, 4, 1);
  613. PdfUtil.createPDFCell(tableFont, table, "主查人", Element.ALIGN_MIDDLE, 3, 1);
  614. PdfUtil.createPDFCell(tableFont, table, data.getCheckUserInfo(), Element.ALIGN_MIDDLE, 4, 1);
  615. PdfUtil.createPDFCell(tableFont, table, "主查人签字", Element.ALIGN_MIDDLE, 3, 1);
  616. List<String> list = new ArrayList<>();
  617. list.add(data.getSignImg());
  618. dealEduImageCell(list, table, 1, 80, 30);
  619. PdfUtil.createPDFCell(tableFont, table, "检查工作情况", Element.ALIGN_MIDDLE, 14, 1);
  620. List<CheckDataVo> normalDataList = data.getCheckDatas().stream().filter(d -> ObjectUtil.equal(Boolean.TRUE, d.getCheckStatus())).collect(Collectors.toList());
  621. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  622. PdfUtil.createPDFCell(tableFont, table, "检查内容", Element.ALIGN_MIDDLE, 10, 1);
  623. PdfUtil.createPDFCell(tableFont, table, "检查情况", Element.ALIGN_MIDDLE, 2, 1);
  624. if (normalDataList.size() > 0) {
  625. for (CheckDataVo checkDataVo : normalDataList) {
  626. PdfUtil.createPDFCell(tableFont, table, String.valueOf(normalDataList.indexOf(checkDataVo) + 1), Element.ALIGN_MIDDLE, 2, 1);
  627. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getPointName(), Element.ALIGN_MIDDLE, 10, 1);
  628. PdfUtil.createPDFCell(tableFont, table, "正常", Element.ALIGN_MIDDLE, 2, 1);
  629. }
  630. } else {
  631. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  632. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 10, 1);
  633. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  634. }
  635. PdfUtil.createPDFCell(tableFont, table, "发现问题情况", Element.ALIGN_MIDDLE, 14, 1);
  636. PdfUtil.createPDFCell(tableFont, table, "序号", Element.ALIGN_MIDDLE, 2, 1);
  637. PdfUtil.createPDFCell(tableFont, table, "检查项目", Element.ALIGN_MIDDLE, 6, 1);
  638. PdfUtil.createPDFCell(tableFont, table, "存在问题", Element.ALIGN_MIDDLE, 6, 1);
  639. final Optional<CheckDataVo> optional = data.getCheckDatas().stream().filter(res -> ObjectUtil.isNotEmpty(res.getResRemark())).findAny();
  640. if (!optional.isPresent()) {
  641. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 2, 1);
  642. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  643. PdfUtil.createPDFCell(tableFont, table, "/", Element.ALIGN_MIDDLE, 6, 1);
  644. document.add(table);
  645. return;
  646. }
  647. int questionIndex = 1;
  648. for (int i = 1; i <= data.getCheckDatas().size(); i++) {
  649. final CheckDataVo checkDataVo = data.getCheckDatas().get(i - 1);
  650. if (ObjectUtil.isEmpty(checkDataVo.getResRemark())) {
  651. continue;
  652. }
  653. PdfUtil.createPDFCell(tableFont, table, String.valueOf(questionIndex++), Element.ALIGN_MIDDLE, 2, 1);
  654. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getItemName(), Element.ALIGN_MIDDLE, 6, 1);
  655. PdfUtil.createPDFCell(tableFont, table, checkDataVo.getResRemark(), Element.ALIGN_MIDDLE, 6, 1);
  656. }
  657. document.add(table);
  658. }
  659. public static void addPageNum(String srcPdfPath, String targetPdfPath, BaseFont fs, Font tableFont) {
  660. try {
  661. // 输出文件 流
  662. FileOutputStream fos = new FileOutputStream(targetPdfPath);
  663. // 读取 源PDF文件,进行一页一页复制,才能触发 添加页码的 页面监听事件
  664. PdfReader reader = new PdfReader(srcPdfPath);
  665. // 获取 源文件总页数
  666. int num = reader.getNumberOfPages();
  667. // 新建文档,默认A4大小
  668. Document document = createDocument(0, 0, 50, 50);
  669. PdfWriter writer = PdfWriter.getInstance(document, fos);
  670. writer.setPageEvent(new PdfPageHelperEvent(num, writer, fs, tableFont));
  671. document.open();
  672. // PDF内容体
  673. PdfContentByte pdfContent = writer.getDirectContent();
  674. // 页面数是从1开始的
  675. for (int i = 1; i <= num; i++) {
  676. document.newPage();
  677. // 设置空页码进行展示
  678. writer.setPageEmpty(false);
  679. PdfImportedPage page = writer.getImportedPage(reader, i);
  680. // 复制好的页面,添加到内容去,触发事件监听
  681. pdfContent.addTemplate(page, 0, 42);
  682. }
  683. document.close();
  684. reader.close();
  685. final File file = new File(srcPdfPath);
  686. file.delete();
  687. } catch (Exception e) {
  688. e.printStackTrace();
  689. }
  690. }
  691. }