From 029cc69974cd48c0130e1492a33a11415608d911 Mon Sep 17 00:00:00 2001 From: "15881625488@163.com" <15881625488@163.com> Date: Sat, 17 Jan 2026 15:33:04 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A[=E8=B5=84=E6=BA=90=E5=BA=93?= =?UTF-8?q?=E7=AE=A1=E7=90=86]=E9=BA=BB=E7=B2=BE=E8=8D=AF=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hxhq/business/controller/MjyController.java | 4 +- .../com/hxhq/business/utils/Pdf/PdfBaseUtil.java | 403 -------------------- .../com/hxhq/business/utils/Pdf/PdfExportUtil.java | 66 ---- .../com/hxhq/business/utils/Pdf/Resource/Drug.java | 152 -------- .../hxhq/business/utils/Pdf/Template/Sp/Sp001.java | 61 --- .../com/hxhq/business/utils/pdf/PdfBaseUtil.java | 411 +++++++++++++++++++++ .../com/hxhq/business/utils/pdf/PdfExportUtil.java | 66 ++++ .../com/hxhq/business/utils/pdf/resource/Drug.java | 170 +++++++++ .../hxhq/business/utils/pdf/template/sp/Sp001.java | 60 +++ 9 files changed, 709 insertions(+), 684 deletions(-) delete mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java delete mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java delete mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java delete mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java create mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java create mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java create mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java create mode 100644 hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java index dd9f32e..dc24bc4 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java @@ -8,7 +8,7 @@ import com.hxhq.business.dto.mjy.ListDto; import com.hxhq.business.form.mjy.*; import com.hxhq.business.service.IMjyJcgjService; import com.hxhq.business.service.IMjyTzService; -import com.hxhq.business.utils.Pdf.PdfExportUtil; +import com.hxhq.business.utils.pdf.PdfExportUtil; import com.hxhq.common.security.annotation.RequiresPermissions; import com.hxhq.common.security.utils.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -112,7 +112,7 @@ public class MjyController extends BaseController { mjyTz.setMjyId(id); List mjyTzList = mjyTzService.queryList(mjyTz); return AjaxResult.success(localFilePrefix + PdfExportUtil.export( - "com.hxhq.business.utils.Pdf.Resource.Drug", + "com.hxhq.business.utils.pdf.resource.Drug", "exportDetail", mjyService.queryInfo(id), mjyTzList, diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java deleted file mode 100644 index 3079ec7..0000000 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java +++ /dev/null @@ -1,403 +0,0 @@ -package com.hxhq.business.utils.Pdf; - -import com.hxhq.common.core.exception.ServiceException; -import com.hxhq.common.core.text.Convert; -import com.hxhq.common.core.utils.DateUtils; -import com.itextpdf.text.*; -import com.itextpdf.text.pdf.*; -import com.itextpdf.text.pdf.draw.LineSeparator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Paths; -import java.text.SimpleDateFormat; -import java.util.List; -import java.util.*; - -/** - * pdf导出基类 - * - * @author tanfei - */ -@Component -public class PdfBaseUtil { - - private static final Logger logger = LoggerFactory.getLogger(PdfBaseUtil.class); - - /** - * 生成PDF文件完整路径(按年月日创建文件夹) - * - * @return 完整文件路径, - */ - public static String getFilePath(String localFilePath) { - // 1. 获取当前年月日并创建文件夹 - Date now = new Date(); - SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); - SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); - SimpleDateFormat dayFormat = new SimpleDateFormat("dd"); - SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss"); - - String year = yearFormat.format(now); - String month = monthFormat.format(now); - String day = dayFormat.format(now); - String timestamp = timestampFormat.format(now) + UUID.randomUUID(); - - // 2. 构建文件夹路径 - String folderPath = localFilePath + File.separator + year + File.separator + month + File.separator + day; - - // 3. 确保目录存在 - File dir = new File(folderPath); - if (!dir.exists()) { - dir.mkdirs(); - } - // 4. 生成文件名 - String fileName = "document_" + timestamp + ".pdf"; - // 5. 完整文件路径 - return Paths.get(folderPath, fileName).toString(); - } - - /** - * 添加带下划线的标题 - * - * @param document PDF文档对象 - * @param titleText 标题文本 - * @param fontSize 字体大小 - */ - public static void addUnderlinedTitle(Document document, String titleText, float fontSize, Boolean hasLine) - throws DocumentException, IOException { - - // 创建中文字体 - BaseFont baseFont = BaseFont.createFont( - "STSong-Light", - "UniGB-UCS2-H", - BaseFont.EMBEDDED - ); - - Font titleFont = new Font(baseFont, fontSize, Font.NORMAL, BaseColor.BLACK); - Font linFont = new Font(baseFont, fontSize, Font.NORMAL, BaseColor.BLACK); - - // 创建标题段落 - Paragraph titleParagraph = new Paragraph(titleText, titleFont); - titleParagraph.setAlignment(Element.ALIGN_LEFT); - titleParagraph.setSpacingAfter(5); - document.add(titleParagraph); - - if (hasLine) { - // 添加下划线 - LineSeparator underline = new LineSeparator(linFont); - underline.setPercentage(100); // 宽度占50% - underline.setLineWidth(1); // 线粗 - underline.setAlignment(Element.ALIGN_LEFT); - // 添加到文档 - document.add(underline); - // 添加一些间距 - document.add(new Paragraph(titleText)); // 空行 - } - } - - /** - * 初始化 - * - * @param document - * @param fos - * @param filePath - * @param signText - */ - public static Document init(Document document, FileOutputStream fos, String filePath, String signText, String headerText) { - try { - // 创建PDF文档 设置文档边距,避免内容遮挡页眉页脚 - float topMargin = 70; // 顶部边距(为页眉留出空间) - float bottomMargin = 60; // 底部边距(为页脚留出空间) - float leftMargin = 50; // 左边距 - float rightMargin = 50; // 右边距 - - document = new Document(PageSize.A4, - leftMargin, rightMargin, topMargin, bottomMargin); - - fos = new FileOutputStream(filePath); - PdfWriter writer = PdfWriter.getInstance(document, fos); - // 设置页面事件,每页添加文字页眉 - writer.setPageEvent(new PdfBaseUtil.TextHeaderEvent(signText, headerText)); - // 5. 设置PDF属性 - document.addTitle("华西海圻"); - document.addAuthor("华西海圻"); - document.addCreator("华西海圻"); - document.addCreationDate(); - document.open(); - return document; - } catch (Exception e) { - logger.error("生成失败", e); - throw new ServiceException("生成失败: " + e.getMessage()); - } - } - - /** - * 创建表单 - * - * @param document - * @param data - * @param count - * @throws DocumentException - * @throws IOException - */ - public static void addFormTableColumns(Document document, Map data, Integer count) - throws DocumentException, IOException { - - // 创建3列表格 - PdfPTable table = new PdfPTable(count); - table.setWidthPercentage(100); - table.setSpacingAfter(10); - - // 设置等宽列 - float[] widths = new float[count]; - for (int i = 0; i < count; i++) { - widths[i] = 1.0f; // 等宽 - } - table.setWidths(widths); - - // 创建字体 - BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); - Font labelFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.BLACK); - Font valueFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.BLACK); - - // 将数据转换为List - List> entries = new ArrayList<>(data.entrySet()); - - // 每3个字段一组,每组一行 - for (int i = 0; i < entries.size(); i += count) { - // 创建一行 - for (int j = 0; j < count; j++) { - int index = i + j; - - if (index < entries.size()) { - Map.Entry entry = entries.get(index); - - // 创建单元格内容 - Phrase cellContent = new Phrase(); - cellContent.add(new Chunk(entry.getKey() + ":", labelFont)); - cellContent.add(new Chunk(com.hxhq.common.core.utils.StringUtils.isBlank(entry.getValue()) ? "" : entry.getValue(), valueFont)); - - PdfPCell cell = new PdfPCell(cellContent); - cell.setBorder(Rectangle.NO_BORDER); // 无边框 - cell.setPadding(4); - cell.setHorizontalAlignment(Element.ALIGN_LEFT); - cell.setVerticalAlignment(Element.ALIGN_MIDDLE); - cell.setMinimumHeight(20); // 最小高度 - - table.addCell(cell); - } else { - // 添加空单元格保持布局 - PdfPCell emptyCell = new PdfPCell(); - emptyCell.setBorder(Rectangle.NO_BORDER); - emptyCell.setMinimumHeight(20); - table.addCell(emptyCell); - } - } - } - - document.add(table); - } - - /** - * 创建表格 - * - * @param content - * @param font - * @return - */ - public static PdfPCell createCell(String content, Font font) { - PdfPCell cell = new PdfPCell(new Phrase(content, font)); - cell.setHorizontalAlignment(Element.ALIGN_CENTER); - cell.setVerticalAlignment(Element.ALIGN_MIDDLE); - cell.setPadding(6); - cell.setMinimumHeight(25); - return cell; - } - - - /** - * 页面事件处理类 - 每页添加文字页眉 - */ - static class TextHeaderEvent extends PdfPageEventHelper { - private Font footerFont; - private String signText; - private String headerText; - private BaseFont baseFont; - private int totalPages = 0; - private PdfTemplate total; - - public TextHeaderEvent(String signText, String headerText) throws Exception { - try { - this.signText = signText; - this.headerText = headerText; - // 创建字体(支持中文) - this.baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); - footerFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.GRAY); - } catch (Exception e) { - footerFont = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.GRAY); - } - } - - @Override - public void onOpenDocument(PdfWriter writer, Document document) { - total = writer.getDirectContent().createTemplate(100, 16); - } - - @Override - public void onCloseDocument(PdfWriter writer, Document document) { - totalPages = writer.getPageNumber(); - String totalPagesStr = String.valueOf(totalPages); - - // 重新创建合适宽度的模板 - BaseFont baseFont = footerFont.getBaseFont(); - float actualWidth = baseFont.getWidthPoint(totalPagesStr, footerFont.getSize()); - - total.beginText(); - total.setFontAndSize(baseFont, footerFont.getSize()); - total.setTextMatrix(0, 0); - total.showText(totalPagesStr); - total.endText(); - } - - @Override - public void onEndPage(PdfWriter writer, Document document) { - try { - PdfContentByte cb = writer.getDirectContent(); - - // 获取页面尺寸 - Rectangle pageSize = document.getPageSize(); - float pageWidth = pageSize.getWidth(); - float pageHeight = pageSize.getHeight(); - - // 设置页眉参数 - float topMargin = 30; // 顶部边距 - float fontSize = 12; // 字体大小 - - // 计算文字宽度(用于居中) - float textWidth = baseFont.getWidthPoint(signText, fontSize); - - // 计算居中位置 - float textX = (pageWidth - textWidth) / 2; - float textY = pageHeight - topMargin; - - // 获取画布 - PdfContentByte canvas = writer.getDirectContent(); - - int currentPage = writer.getPageNumber(); - float y = document.bottom() - 20; - - // 在每一页都重新计算,确保位置准确 - String pageText = "第 " + currentPage + " 页 / 共 "; - BaseFont baseFont = footerFont.getBaseFont(); - - // 计算页面文本宽度 - float pageTextWidth = baseFont.getWidthPoint(pageText, footerFont.getSize()); - - // 临时用估计的总页数计算位置(等文档关闭后会被替换) - String tempTotal = String.valueOf(writer.getPageNumber() + 5); // 估计值 - float totalWidth = baseFont.getWidthPoint(tempTotal, footerFont.getSize()); - - // 计算起始位置 - float totalTextWidth = pageTextWidth + totalWidth + - baseFont.getWidthPoint(" 页", footerFont.getSize()); - float startX = (document.getPageSize().getWidth() - totalTextWidth) / 2; - - // 写入当前页信息 - ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, - new Phrase(pageText, footerFont), startX, y, 0); - - // 添加总页数模板 - cb.addTemplate(total, startX + pageTextWidth, y); - - // 写入"页"字 - ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, - new Phrase(" 页", footerFont), startX + pageTextWidth + totalWidth, y, 0); - - PdfContentByte content = writer.getDirectContentUnder(); // 在水印层添加 - - // 设置透明度 - PdfGState gs = new PdfGState(); - float opacity = Convert.toFloat("0.3"); - gs.setFillOpacity(opacity); - content.setGState(gs); - - // 设置字体和颜色 - content.setColorFill(BaseColor.RED); - content.setFontAndSize(baseFont, fontSize); - - float width = pageSize.getWidth(); - float height = pageSize.getHeight(); - - // 重复铺满水印 - addRepeatedWatermark(content, width, height, signText); - -// // 添加页眉文字 -// canvas.beginText(); -// canvas.setFontAndSize(baseFont, fontSize); -// canvas.setTextMatrix(textX, textY); -// canvas.showText(headerText); -// canvas.endText(); -// -// // 添加页眉分隔线(可选) -// addHeaderLine(canvas, pageWidth, textY - 10); - - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * 添加水印 - * - * @param content - * @param width - * @param height - * @param signText - */ - private void addRepeatedWatermark(PdfContentByte content, float width, float height, String signText) { - // 计算文字尺寸 - float fontSize = Convert.toFloat("8"); - float textWidth = baseFont.getWidthPoint(signText, fontSize); - float textHeight = baseFont.getAscentPoint(signText, fontSize) - - baseFont.getDescentPoint(signText, fontSize); - - // 计算间距 - float stepX = textWidth + 150; - float stepY = textHeight + 100; - - // 添加重复水印 - for (float x = textWidth / 2; x < width; x += stepX) { - for (float y = textHeight / 2; y < height; y += stepY) { - content.beginText(); - content.showTextAligned( - Element.ALIGN_CENTER, - signText, - x, - y, - fontSize - ); - content.endText(); - } - } - } - - /** - * 添加页眉线 - * - * @param canvas - * @param pageWidth - * @param yPos - */ - private void addHeaderLine(PdfContentByte canvas, float pageWidth, float yPos) { - canvas.setLineWidth(0.5f); - canvas.moveTo(50, yPos); - canvas.lineTo(pageWidth - 50, yPos); - canvas.stroke(); - } - } -} \ No newline at end of file diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java deleted file mode 100644 index 6d8eab8..0000000 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.hxhq.business.utils.Pdf; - -import com.hxhq.common.core.exception.ServiceException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.lang.reflect.Method; - -/** - * pdf导出基类 - * @author tanfei - */ -@Component -public class PdfExportUtil { - - private static final Logger logger = LoggerFactory.getLogger(PdfExportUtil.class); - - public static Object invokeMethod(String className, String methodName, Object... params) - throws Exception { - // 加载类 - Class clazz = Class.forName(className); - // 创建实例 - Object instance = clazz.getDeclaredConstructor().newInstance(); - // 改为不预先获取参数类型,而是在查找方法时动态匹配 - Method method = null; - for (Method m : clazz.getMethods()) { - if (m.getName().equals(methodName)) { - method = m; - } - } - - if (method == null) { - throw new NoSuchMethodException("找不到匹配的方法: " + methodName); - } - // 调用方法 - return method.invoke(instance, params); - } - - public static void main(String[] args) throws Exception { - String result = export( - "com.hxhq.business.utils.Pdf.Template.Sp.Sp001", - "exportDetail", - "D:/hxhq/uploadPath" - ); - } - - /** - * 导出 - * - * @param className - * @param methodName - * @param params - * @return - */ - public static String export(String className, String methodName, Object... params) { - try { - String path= (String) invokeMethod(className, methodName, params); - return path.replace(params[params.length - 1].toString().replace("/","\\" ), "") - .replace("\\", "/"); - } catch (Exception ex) { - logger.error(ex.getMessage()); - throw new ServiceException(ex.getMessage()); - } - } -} \ No newline at end of file diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java deleted file mode 100644 index 03025fd..0000000 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java +++ /dev/null @@ -1,152 +0,0 @@ -package com.hxhq.business.utils.Pdf.Resource; - -import com.alibaba.fastjson2.JSONObject; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.hxhq.business.domain.Mjy; -import com.hxhq.business.domain.MjyJcgj; -import com.hxhq.business.domain.MjyTz; -import com.hxhq.business.domain.StudyMethodRead; -import com.hxhq.business.utils.Pdf.PdfBaseUtil; -import com.hxhq.common.core.utils.StringUtils; -import com.hxhq.common.security.utils.SecurityUtils; -import com.hxhq.system.api.domain.SysUser; -import com.hxhq.system.api.model.LoginUser; -import com.itextpdf.text.*; -import com.itextpdf.text.pdf.BaseFont; -import com.itextpdf.text.pdf.PdfPCell; -import com.itextpdf.text.pdf.PdfPTable; -import com.itextpdf.text.pdf.draw.LineSeparator; -import org.apache.http.client.utils.DateUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.*; -import java.util.List; - -/** - * 麻精药详情 - * - * @author tanfei - */ -public class Drug { - private static final Logger logger = LoggerFactory.getLogger(Drug.class.getName()); - - /** - * 导出 - * - * @param mjy - * @return - */ - public String exportDetail(Mjy mjy, List mjyTzList, List mjyJcgjList, String localFilePath) { - Document document = null; - FileOutputStream fos = null; - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String filePath = PdfBaseUtil.getFilePath(localFilePath); - try { - String sign="华西海圻"; - LoginUser loginUser=SecurityUtils.getLoginUser(); - if(loginUser!=null){ - sign=loginUser.getSysUser().getNickName(); - } - document = PdfBaseUtil.init(document, fos, filePath, sign+sdf.format(new Date()), mjy.getMc()); - // 基本信息 - PdfBaseUtil.addUnderlinedTitle(document, "基本信息", 10,true); - Map formData1 = new LinkedHashMap<>(); - formData1.put("名称", mjy.getMc()); - formData1.put("编号", mjy.getBh()); - formData1.put("浓度", mjy.getNd() + mjy.getNddw()); - formData1.put("库存量", mjy.getKc() + mjy.getKcdw()); - formData1.put("失效日期", sdf.format(mjy.getSxrq())); - formData1.put("存储条件", mjy.getCctj()); - formData1.put("存储位置", mjy.getCcwz()); - PdfBaseUtil.addFormTableColumns(document, formData1, 2); - // 表单信息 - PdfBaseUtil.addUnderlinedTitle(document, "表单信息", 10,true); - Map formData3 = new LinkedHashMap<>(); - formData3.put("所属表单", ""); - formData3.put("所属试验信息", mjy.getMdIds()); - formData3.put("表单所属人", mjy.getBdId().toString()); - PdfBaseUtil.addFormTableColumns(document, formData3, 2); - PdfBaseUtil.addUnderlinedTitle(document, "麻精药台账", 10,true); - // 9. 表头 - BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); - Font headerFont = new Font(bfChinese, 10, Font.NORMAL); - Font contentFont = new Font(bfChinese, 10, Font.NORMAL); - // 8. 创建表格 - PdfPTable table = new PdfPTable(7); - table.setWidthPercentage(100); - String[] headers = {"使用人", "领取/归还人", "发放/接收人", "操作类型", "操作量", "备注/原因", "操作时间"}; - for (String header : headers) { - PdfPCell cell = new PdfPCell(new Phrase(header, headerFont)); - cell.setHorizontalAlignment(Element.ALIGN_CENTER); - cell.setBackgroundColor(BaseColor.WHITE); - cell.setPadding(8); - cell.setBorderWidth(1); - table.addCell(cell); - } - - // 台账 - int rowNum = 0; - for (MjyTz mjyTz : mjyTzList) { - // 交替行颜色 - if (rowNum % 2 == 0) { - table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); - } else { - table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); - } - table.addCell(PdfBaseUtil.createCell(mjyTz.getQmrMc(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getLqrMc(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getGhrMc(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getQmyy(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getCzl()+mjyTz.getCzldw(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getRemark(), contentFont)); - table.addCell(PdfBaseUtil.createCell(sdf.format(mjyTz.getCreateTime()), contentFont)); - rowNum++; - } - document.add(table); - PdfBaseUtil.addUnderlinedTitle(document, "稽查轨迹", 10,true); - - //稽查轨迹 - for (MjyJcgj mjyJcgj : mjyJcgjList) { - PdfBaseUtil.addUnderlinedTitle(document, sdf.format(mjyJcgj.getCreateTime())+" "+mjyJcgj.getJcmc() , 10,false); - StringJoiner result = new StringJoiner(", "); - ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonArray = mapper.readTree(mjyJcgj.getJcnr()); - for (JsonNode node : jsonArray) { - String name = node.get("name").asText(); - String value = node.get("value").asText(); - result.add(name + ":" + value); - } - if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ - result.add("备注:"+mjyJcgj.getRemark()); - } - if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ - result.add("签名人:"+mjyJcgj.getQmrMc()); - } - PdfBaseUtil.addUnderlinedTitle(document, result.toString(), 10,false); - } - logger.info("生成成功:{}", filePath); - } catch (Exception e) { - logger.error("生成失败", e); - throw new RuntimeException("生成失败: " + e.getMessage()); - } finally { - if (document != null) { - document.close(); - } - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - logger.error("关闭文件流失败", e); - } - } - } - return filePath; - } - - -} diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java deleted file mode 100644 index eeac752..0000000 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.hxhq.business.utils.Pdf.Template.Sp; - -import com.hxhq.business.utils.Pdf.PdfBaseUtil; -import com.hxhq.common.security.utils.SecurityUtils; -import com.hxhq.system.api.domain.SysUser; -import com.hxhq.system.api.model.LoginUser; -import com.itextpdf.text.Document; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * 色谱001 - * @author tanfei - */ -public class Sp001 { - private static final Logger logger = LoggerFactory.getLogger(Sp001.class.getName()); - - /**导出 - * @return - */ - public static String exportDetail(String localFilePath) { - Document document = null; - FileOutputStream fos = null; - String filePath = PdfBaseUtil.getFilePath(localFilePath); - try { - String sign="华西海圻"; - LoginUser loginUser=SecurityUtils.getLoginUser(); - if(loginUser!=null){ - sign=loginUser.getSysUser().getNickName(); - } - document=PdfBaseUtil.init(document,fos,filePath,sign,"华西海圻"); - Map formData = new LinkedHashMap<>(); - formData.put("姓名","张三"); - formData.put("性别", "男"); - formData.put("手机","15882062878"); - // 生成table - PdfBaseUtil.addFormTableColumns(document, formData,3); - logger.info("生成成功:{}", filePath); - } catch (Exception e) { - logger.error("生成失败", e); - throw new RuntimeException("生成失败: " + e.getMessage()); - } finally { - if (document != null) { - document.close(); - } - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - logger.error("关闭文件流失败", e); - } - } - } - return filePath; - } - -} diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java new file mode 100644 index 0000000..da75c30 --- /dev/null +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java @@ -0,0 +1,411 @@ +package com.hxhq.business.utils.pdf; + +import com.hxhq.common.core.exception.ServiceException; +import com.hxhq.common.core.text.Convert; +import com.itextpdf.text.*; +import com.itextpdf.text.pdf.*; +import com.itextpdf.text.pdf.draw.LineSeparator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Paths; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.*; + +/** + * pdf导出基类 + * + * @author tanfei + */ +@Component +public class PdfBaseUtil { + + private static final Logger logger = LoggerFactory.getLogger(PdfBaseUtil.class); + + /** + * 生成PDF文件完整路径(按年月日创建文件夹) + * + * @return 完整文件路径, + */ + public static String getFilePath(String localFilePath) { + // 1. 获取当前年月日并创建文件夹 + Date now = new Date(); + SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); + SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); + SimpleDateFormat dayFormat = new SimpleDateFormat("dd"); + SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss"); + + String year = yearFormat.format(now); + String month = monthFormat.format(now); + String day = dayFormat.format(now); + String timestamp = timestampFormat.format(now) + UUID.randomUUID(); + + // 2. 构建文件夹路径 + String folderPath = localFilePath + File.separator + year + File.separator + month + File.separator + day; + + // 3. 确保目录存在 + File dir = new File(folderPath); + if (!dir.exists()) { + dir.mkdirs(); + } + // 4. 生成文件名 + String fileName = "document_" + timestamp + ".pdf"; + // 5. 完整文件路径 + return Paths.get(folderPath, fileName).toString(); + } + + /** + * 添加带下划线的标题 + * + * @param document PDF文档对象 + * @param titleText 标题文本 + * @param fontSize 字体大小 + */ + public static void addUnderlinedTitle(Document document, String titleText, float fontSize, Boolean hasLine) + throws DocumentException, IOException { + + // 创建中文字体 + BaseFont baseFont = BaseFont.createFont( + "STSong-Light", + "UniGB-UCS2-H", + BaseFont.EMBEDDED + ); + + Font titleFont = new Font(baseFont, fontSize, Font.NORMAL, BaseColor.BLACK); + Font linFont = new Font(baseFont, fontSize, Font.NORMAL, BaseColor.BLACK); + + // 创建标题段落 + Paragraph titleParagraph = new Paragraph(titleText, titleFont); + titleParagraph.setAlignment(Element.ALIGN_LEFT); + titleParagraph.setSpacingAfter(5); + document.add(titleParagraph); + + if (hasLine) { + // 添加下划线 + LineSeparator underline = new LineSeparator(linFont); + // 宽度占50% + underline.setPercentage(100); + // 线粗 + underline.setLineWidth(0.5f); + underline.setAlignment(Element.ALIGN_LEFT); + // 添加到文档 + document.add(underline); + // 添加一些间距 + document.add(new Paragraph(titleText)); + } + } + + /** + * 初始化 + * + * @param document + * @param fos + * @param filePath + * @param signText + */ + public static Document init(Document document, FileOutputStream fos, String filePath, String signText, String headerText) { + try { + // 创建PDF文档 设置文档边距,避免内容遮挡页眉页脚 + // 顶部边距(为页眉留出空间) + float topMargin = 70; + // 底部边距(为页脚留出空间) + float bottomMargin = 60; + // 左边距 + float leftMargin = 50; + // 右边距 + float rightMargin = 50; + + document = new Document(PageSize.A4, + leftMargin, rightMargin, topMargin, bottomMargin); + + fos = new FileOutputStream(filePath); + PdfWriter writer = PdfWriter.getInstance(document, fos); + // 设置页面事件,每页添加文字页眉 + writer.setPageEvent(new PdfBaseUtil.TextHeaderEvent(signText, headerText)); + // 5. 设置PDF属性 + document.addTitle("华西海圻"); + document.addAuthor("华西海圻"); + document.addCreator("华西海圻"); + document.addCreationDate(); + document.open(); + return document; + } catch (Exception e) { + logger.error("生成失败", e); + throw new ServiceException("生成失败: " + e.getMessage()); + } + } + + /** + * 创建表单 + * + * @param document + * @param data + * @param count + * @throws DocumentException + * @throws IOException + */ + public static void addFormTableColumns(Document document, Map data, Integer count) + throws DocumentException, IOException { + + // 创建3列表格 + PdfPTable table = new PdfPTable(count); + table.setWidthPercentage(100); + table.setSpacingAfter(10); + + // 设置等宽列 + float[] widths = new float[count]; + for (int i = 0; i < count; i++) { + // 等宽 + widths[i] = 1.0f; + } + table.setWidths(widths); + + // 创建字体 + BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); + Font labelFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.BLACK); + Font valueFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.BLACK); + + // 将数据转换为List + List> entries = new ArrayList<>(data.entrySet()); + + // 每3个字段一组,每组一行 + for (int i = 0; i < entries.size(); i += count) { + // 创建一行 + for (int j = 0; j < count; j++) { + int index = i + j; + + if (index < entries.size()) { + Map.Entry entry = entries.get(index); + + // 创建单元格内容 + Phrase cellContent = new Phrase(); + cellContent.add(new Chunk(entry.getKey() + ":", labelFont)); + cellContent.add(new Chunk(com.hxhq.common.core.utils.StringUtils.isBlank(entry.getValue()) ? "" : entry.getValue(), valueFont)); + + PdfPCell cell = new PdfPCell(cellContent); + // 无边框 + cell.setBorder(Rectangle.NO_BORDER); + cell.setPadding(4); + cell.setHorizontalAlignment(Element.ALIGN_LEFT); + cell.setVerticalAlignment(Element.ALIGN_MIDDLE); + // 最小高度 + cell.setMinimumHeight(20); + + table.addCell(cell); + } else { + // 添加空单元格保持布局 + PdfPCell emptyCell = new PdfPCell(); + emptyCell.setBorder(Rectangle.NO_BORDER); + emptyCell.setMinimumHeight(20); + table.addCell(emptyCell); + } + } + } + + document.add(table); + } + + /** + * 创建表格 + * + * @param content + * @param font + * @return + */ + public static PdfPCell createCell(String content, Font font) { + PdfPCell cell = new PdfPCell(new Phrase(content, font)); + cell.setHorizontalAlignment(Element.ALIGN_CENTER); + cell.setVerticalAlignment(Element.ALIGN_MIDDLE); + cell.setPadding(6); + cell.setMinimumHeight(25); + return cell; + } + + + /** + * 页面事件处理类 - 每页添加文字页眉 + */ + static class TextHeaderEvent extends PdfPageEventHelper { + private Font footerFont; + private String signText; + private String headerText; + private BaseFont baseFont; + private int totalPages = 0; + private PdfTemplate total; + + public TextHeaderEvent(String signText, String headerText) throws Exception { + try { + this.signText = signText; + this.headerText = headerText; + // 创建字体(支持中文) + this.baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); + footerFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.GRAY); + } catch (Exception e) { + footerFont = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.GRAY); + } + } + + @Override + public void onOpenDocument(PdfWriter writer, Document document) { + total = writer.getDirectContent().createTemplate(100, 16); + } + + @Override + public void onCloseDocument(PdfWriter writer, Document document) { + totalPages = writer.getPageNumber(); + String totalPagesStr = String.valueOf(totalPages); + + // 重新创建合适宽度的模板 + BaseFont baseFont = footerFont.getBaseFont(); + float actualWidth = baseFont.getWidthPoint(totalPagesStr, footerFont.getSize()); + + total.beginText(); + total.setFontAndSize(baseFont, footerFont.getSize()); + total.setTextMatrix(0, 0); + total.showText(totalPagesStr); + total.endText(); + } + + @Override + public void onEndPage(PdfWriter writer, Document document) { + try { + PdfContentByte cb = writer.getDirectContent(); + + // 获取页面尺寸 + Rectangle pageSize = document.getPageSize(); + float pageWidth = pageSize.getWidth(); + float pageHeight = pageSize.getHeight(); + + // 设置页眉参数 + float topMargin = 30; // 顶部边距 + float fontSize = 12; // 字体大小 + + // 计算文字宽度(用于居中) + float textWidth = baseFont.getWidthPoint(signText, fontSize); + + // 计算居中位置 + float textX = (pageWidth - textWidth) / 2; + float textY = pageHeight - topMargin; + + // 获取画布 + PdfContentByte canvas = writer.getDirectContent(); + + int currentPage = writer.getPageNumber(); + float y = document.bottom() - 20; + + // 在每一页都重新计算,确保位置准确 + String pageText = "第 " + currentPage + " 页 / 共 "; + BaseFont baseFont = footerFont.getBaseFont(); + + // 计算页面文本宽度 + float pageTextWidth = baseFont.getWidthPoint(pageText, footerFont.getSize()); + + // 临时用估计的总页数计算位置(等文档关闭后会被替换) + String tempTotal = String.valueOf(writer.getPageNumber() + 5); // 估计值 + float totalWidth = baseFont.getWidthPoint(tempTotal, footerFont.getSize()); + + // 计算起始位置 + float totalTextWidth = pageTextWidth + totalWidth + + baseFont.getWidthPoint(" 页", footerFont.getSize()); + float startX = (document.getPageSize().getWidth() - totalTextWidth) / 2; + + // 写入当前页信息 + ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, + new Phrase(pageText, footerFont), startX, y, 0); + + // 添加总页数模板 + cb.addTemplate(total, startX + pageTextWidth, y); + + // 写入"页"字 + ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, + new Phrase(" 页", footerFont), startX + pageTextWidth + totalWidth, y, 0); + + PdfContentByte content = writer.getDirectContentUnder(); // 在水印层添加 + + // 设置透明度 + PdfGState gs = new PdfGState(); + float opacity = Convert.toFloat("0.3"); + gs.setFillOpacity(opacity); + content.setGState(gs); + + // 设置字体和颜色 + content.setColorFill(BaseColor.RED); + content.setFontAndSize(baseFont, fontSize); + + float width = pageSize.getWidth(); + float height = pageSize.getHeight(); + + // 重复铺满水印 + addRepeatedWatermark(content, width, height, signText); + +// // 添加页眉文字 +// canvas.beginText(); +// canvas.setFontAndSize(baseFont, fontSize); +// canvas.setTextMatrix(textX, textY); +// canvas.showText(headerText); +// canvas.endText(); +// +// // 添加页眉分隔线(可选) +// addHeaderLine(canvas, pageWidth, textY - 10); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 添加水印 + * + * @param content + * @param width + * @param height + * @param signText + */ + private void addRepeatedWatermark(PdfContentByte content, float width, float height, String signText) { + // 计算文字尺寸 + float fontSize = Convert.toFloat("8"); + float textWidth = baseFont.getWidthPoint(signText, fontSize); + float textHeight = baseFont.getAscentPoint(signText, fontSize) - + baseFont.getDescentPoint(signText, fontSize); + + // 计算间距 + float stepX = textWidth + 150; + float stepY = textHeight + 100; + + // 添加重复水印 + Integer two=2; + for (float x = textWidth / two; x < width; x += stepX) { + for (float y = textHeight / two; y < height; y += stepY) { + content.beginText(); + content.showTextAligned( + Element.ALIGN_CENTER, + signText, + x, + y, + fontSize + ); + content.endText(); + } + } + } + + /** + * 添加页眉线 + * + * @param canvas + * @param pageWidth + * @param yPos + */ + private void addHeaderLine(PdfContentByte canvas, float pageWidth, float yPos) { + canvas.setLineWidth(0.5f); + canvas.moveTo(50, yPos); + canvas.lineTo(pageWidth - 50, yPos); + canvas.stroke(); + } + } +} \ No newline at end of file diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java new file mode 100644 index 0000000..679dc40 --- /dev/null +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java @@ -0,0 +1,66 @@ +package com.hxhq.business.utils.pdf; + +import com.hxhq.common.core.exception.ServiceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +/** + * pdf导出基类 + * @author tanfei + */ +@Component +public class PdfExportUtil { + + private static final Logger logger = LoggerFactory.getLogger(PdfExportUtil.class); + + public static Object invokeMethod(String className, String methodName, Object... params) + throws Exception { + // 加载类 + Class clazz = Class.forName(className); + // 创建实例 + Object instance = clazz.getDeclaredConstructor().newInstance(); + // 改为不预先获取参数类型,而是在查找方法时动态匹配 + Method method = null; + for (Method m : clazz.getMethods()) { + if (m.getName().equals(methodName)) { + method = m; + } + } + + if (method == null) { + throw new NoSuchMethodException("找不到匹配的方法: " + methodName); + } + // 调用方法 + return method.invoke(instance, params); + } + + public static void main(String[] args) throws Exception { + String result = export( + "com.hxhq.business.utils.pdf.template.sp.Sp001", + "exportDetail", + "D:/hxhq/uploadPath" + ); + } + + /** + * 导出 + * + * @param className + * @param methodName + * @param params + * @return + */ + public static String export(String className, String methodName, Object... params) { + try { + String path= (String) invokeMethod(className, methodName, params); + return path.replace(params[params.length - 1].toString().replace("/","\\" ), "") + .replace("\\", "/"); + } catch (Exception ex) { + logger.error(ex.getMessage()); + throw new ServiceException(ex.getMessage()); + } + } +} \ No newline at end of file diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java new file mode 100644 index 0000000..59c3d07 --- /dev/null +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java @@ -0,0 +1,170 @@ +package com.hxhq.business.utils.pdf.resource; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.hxhq.business.domain.Mjy; +import com.hxhq.business.domain.MjyJcgj; +import com.hxhq.business.domain.MjyTz; +import com.hxhq.business.dto.mjy.DetailDto; +import com.hxhq.business.utils.pdf.PdfBaseUtil; +import com.hxhq.common.core.utils.StringUtils; +import com.hxhq.common.security.utils.SecurityUtils; +import com.hxhq.system.api.model.LoginUser; +import com.itextpdf.text.*; +import com.itextpdf.text.pdf.BaseFont; +import com.itextpdf.text.pdf.PdfPCell; +import com.itextpdf.text.pdf.PdfPTable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.List; + +/** + * 麻精药详情 + * + * @author tanfei + */ +public class Drug { + private static final Logger logger = LoggerFactory.getLogger(Drug.class.getName()); + + /** + * 导出 + * + * @param mjy + * @return + */ + public String exportDetail(DetailDto mjy, List mjyTzList, List mjyJcgjList, String localFilePath) { + Document document = null; + FileOutputStream fos = null; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String filePath = PdfBaseUtil.getFilePath(localFilePath); + try { + String sign="华西海圻"; + LoginUser loginUser=SecurityUtils.getLoginUser(); + if(loginUser!=null){ + sign=loginUser.getSysUser().getNickName(); + } + document = PdfBaseUtil.init(document, fos, filePath, sign+sdf.format(new Date()), mjy.getMc()); + // 基本信息 + PdfBaseUtil.addUnderlinedTitle(document, "基本信息", 10,true); + Map formData1 = new LinkedHashMap<>(); + formData1.put("名称", mjy.getMc()); + formData1.put("编号", mjy.getBh()); + formData1.put("浓度", mjy.getNd() + mjy.getNddw()); + formData1.put("库存量", mjy.getKc() + mjy.getKcdw()); + formData1.put("失效日期", sdf.format(mjy.getSxrq())); + formData1.put("存储条件", mjy.getCctj()); + formData1.put("存储位置", mjy.getCcwz()); + PdfBaseUtil.addFormTableColumns(document, formData1, 2); + // 表单信息 + PdfBaseUtil.addUnderlinedTitle(document, "表单信息", 10,true); + Map formData3 = new LinkedHashMap<>(); + formData3.put("所属表单", mjy.getBdMc()); + formData3.put("所属试验信息", mjy.getStudyMc()); + formData3.put("表单所属人", mjy.getBdgsrMc()); + PdfBaseUtil.addFormTableColumns(document, formData3, 2); + PdfBaseUtil.addUnderlinedTitle(document, "麻精药台账", 10,true); + // 台账 + addTz(document,mjyTzList); + //稽查轨迹 + addJcgj(document,mjyJcgjList); + logger.info("生成成功:{}", filePath); + } catch (Exception e) { + logger.error("生成失败", e); + throw new RuntimeException("生成失败: " + e.getMessage()); + } finally { + if (document != null) { + document.close(); + } + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + logger.error("关闭文件流失败", e); + } + } + } + return filePath; + } + + /** + * 台账 + * @param document + * @param mjyTzList + * @throws IOException + * @throws DocumentException + */ + public void addTz(Document document,List mjyTzList) throws IOException, DocumentException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + // 9. 表头 + BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); + Font headerFont = new Font(bfChinese, 10, Font.NORMAL); + Font contentFont = new Font(bfChinese, 10, Font.NORMAL); + // 8. 创建表格 + PdfPTable table = new PdfPTable(7); + table.setWidthPercentage(100); + String[] headers = {"使用人", "领取/归还人", "发放/接收人", "操作类型", "操作量", "备注/原因", "操作时间"}; + for (String header : headers) { + PdfPCell cell = new PdfPCell(new Phrase(header, headerFont)); + cell.setHorizontalAlignment(Element.ALIGN_CENTER); + cell.setPadding(8); + cell.setBorderWidth(1); + table.addCell(cell); + } + int rowNum = 0; + for (MjyTz mjyTz : mjyTzList) { + // 交替行颜色 + if (rowNum % 2 == 0) { + table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); + } else { + table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); + } + table.addCell(PdfBaseUtil.createCell(mjyTz.getQmrMc(), contentFont)); + table.addCell(PdfBaseUtil.createCell(StringUtils.isNoneBlank(mjyTz.getLqrMc())?mjyTz.getLqrMc():""+(StringUtils.isNoneBlank(mjyTz.getGhrMc())?mjyTz.getGhrMc():""), contentFont)); + table.addCell(PdfBaseUtil.createCell(StringUtils.isNoneBlank(mjyTz.getFfrMc())?mjyTz.getFfrMc():""+(StringUtils.isNoneBlank(mjyTz.getJsrMc())?mjyTz.getJsrMc():""), contentFont)); + table.addCell(PdfBaseUtil.createCell(mjyTz.getQmyy(), contentFont)); + table.addCell(PdfBaseUtil.createCell(mjyTz.getCzl()+mjyTz.getCzldw(), contentFont)); + table.addCell(PdfBaseUtil.createCell(mjyTz.getRemark(), contentFont)); + table.addCell(PdfBaseUtil.createCell(sdf.format(mjyTz.getCreateTime()), contentFont)); + rowNum++; + } + document.add(table); + PdfBaseUtil.addUnderlinedTitle(document, "稽查轨迹", 10,true); + } + + /** + * 稽查轨迹 + * @param document + * @param mjyJcgjList + * @throws IOException + * @throws DocumentException + */ + public void addJcgj(Document document,List mjyJcgjList) throws IOException, DocumentException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + for (MjyJcgj mjyJcgj : mjyJcgjList) { + PdfBaseUtil.addUnderlinedTitle(document, sdf.format(mjyJcgj.getCreateTime())+" "+mjyJcgj.getJcmc() , 10,false); + StringJoiner result = new StringJoiner(", "); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonArray = mapper.readTree(mjyJcgj.getJcnr()); + for (JsonNode node : jsonArray) { + String name = node.get("name").asText(); + String value = node.get("value").asText(); + result.add(name + ":" + value); + } + if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ + result.add("备注:"+mjyJcgj.getRemark()); + } + if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ + result.add("签名人:"+mjyJcgj.getQmrMc()); + } + PdfBaseUtil.addUnderlinedTitle(document, result.toString(), 10,false); + } + } + + +} diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java new file mode 100644 index 0000000..125e2d8 --- /dev/null +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java @@ -0,0 +1,60 @@ +package com.hxhq.business.utils.pdf.template.sp; + +import com.hxhq.business.utils.pdf.PdfBaseUtil; +import com.hxhq.common.security.utils.SecurityUtils; +import com.hxhq.system.api.model.LoginUser; +import com.itextpdf.text.Document; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * 色谱001 + * @author tanfei + */ +public class Sp001 { + private static final Logger logger = LoggerFactory.getLogger(Sp001.class.getName()); + + /**导出 + * @return + */ + public static String exportDetail(String localFilePath) { + Document document = null; + FileOutputStream fos = null; + String filePath = PdfBaseUtil.getFilePath(localFilePath); + try { + String sign="华西海圻"; + LoginUser loginUser=SecurityUtils.getLoginUser(); + if(loginUser!=null){ + sign=loginUser.getSysUser().getNickName(); + } + document=PdfBaseUtil.init(document,fos,filePath,sign,"华西海圻"); + Map formData = new LinkedHashMap<>(); + formData.put("姓名","张三"); + formData.put("性别", "男"); + formData.put("手机","15882062878"); + // 生成table + PdfBaseUtil.addFormTableColumns(document, formData,3); + logger.info("生成成功:{}", filePath); + } catch (Exception e) { + logger.error("生成失败", e); + throw new RuntimeException("生成失败: " + e.getMessage()); + } finally { + if (document != null) { + document.close(); + } + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + logger.error("关闭文件流失败", e); + } + } + } + return filePath; + } + +}