这里写自定义目录标题
- controller层
- pdf模板RiskReportTemplate1(页脚、水印)
- 多线程导出pdf数据
- pdf导出工具类(页眉、图片底纹、柱图、饼图、表格、一级二级标题)
- 实体类
- pdf工具类(根据html生成pdf文件)
- 页脚工具类
- 柱图工具类
- 饼图工具类
controller层
java">@PostMapping("/queryRiskReport1")public Object queryRiskReport1(@RequestBody RiskBo riskBo)throws Exception {String corpKey = riskBo.getCorpKey();String reportNo = "18073201";String beginDate=riskBo.getBeginDate();String endDate=riskBo.getEndDate();SysUser user = SecurityUtils.getLoginUser().getUser();String deptName = user.getDept().getDeptName();ChangeRiskIntoVo changeRiskIntoVo = new ChangeRiskIntoVo();String ancestors = user.getDept().getAncestors();if(ancestors.contains(",")){String[] split = ancestors.split(",");changeRiskIntoVo.setDeptPid(split[0]);}else{changeRiskIntoVo.setDeptPid(ancestors);}changeRiskIntoVo.setCorpKey(corpKey);Boolean aBoolean = riskHistoryInfoService.queryAuth(changeRiskIntoVo);String queryDate = DateUtils.getLastYears(1) + "至"+ DateUtils.getLastYears(0);String currentTime = DateUtils.getDateFormatter(new Date());long currentTimeMillis = System.currentTimeMillis();String path = pdfResourceFolder + "risk_report" + File.separator;String pdfTemp = tempPdfFolder + "risk_report" + File.separator;File file = new File(path);if (!file.exists()) file.mkdirs();File file1 = new File(pdfTemp);if (!file1.exists()) file1.mkdirs();String pathUrl = path + user.getUserId() + "-" + currentTimeMillis + ".PDF";String tmpPath1 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "1.PDF";String tmpPath2 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "2.PDF";String tmpPath3 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "3.PDF";String tmpPath4 = pdfTemp + user.getUserId() + "-" + currentTimeMillis + "4.PDF";RiskReportTemplate1 t=new RiskReportTemplate1();String htmlStr= t.createReportTemplate(riskReportService.queryRiskReport1(corpKey,beginDate,endDate), queryDate, user.getUserName(), deptName, reportNo,aBoolean,beginDate,endDate).toString();PdfUtils.htmp2pdf("客户风险分析报告", htmlStr,tmpPath1, false, false);ReportUtils2.addImageWatermark(tmpPath1, tmpPath2, 1, 0);ReportUtils2.pdf_yemei(deptName, tmpPath2, tmpPath3);t.waterMark(tmpPath3,tmpPath4, user.getUserName(), currentTime, user.getDept().getDeptName());t.addPageNum(tmpPath4, pathUrl);return Result.success(pathUrl);}
pdfRiskReportTemplate1_46">pdf模板RiskReportTemplate1(页脚、水印)
java">package com.ruoyi.business.bankInterface.controller.risk.model1;import com.alibaba.fastjson2.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.PdfUtils;
import com.ruoyi.business.bankInterface.controller.risk.utils.ReportUtils2;
import com.ruoyi.business.bankInterface.controller.tax.utils.DateUtils;
import com.ruoyi.business.bankInterface.controller.tax.utils.pdf.PdfNumPageEventTax;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;@Slf4j
public class RiskReportTemplate1 {public static void main(String[]args)throws IOException {StringBuffer buffer = new StringBuffer();BufferedReader bf = new BufferedReader(new FileReader("D:\\test\\RiskReport1.json"));String s = null;while((s = bf.readLine()) != null){ buffer.append(s.trim()); } bf.close();RiskModel2 model = JSONObject.parseObject(buffer.toString(),RiskModel2.class);RiskReportTemplate1 t=new RiskReportTemplate1();String htmlStr= t.createReportTemplate(model, "2021-01-01 "+"至"+" 2024-01-31", "周伯通","中国银行苏州分行","18073201",true,"","").toString();PdfUtils.htmp2pdf("njfh", htmlStr,"D:\\test\\2123456789001.pdf", false, false);ReportUtils2.addImageWatermark("D:\\test\\2123456789001.pdf", "D:\\test\\2123456789002.pdf", 1, 0);ReportUtils2.pdf_yemei("中国银行苏州分行", "D:\\test\\2123456789002.pdf", "D:\\test\\2123456789.pdf");waterMark("D:\\test\\2123456789.pdf","D:\\test\\21234567891.pdf", "测试用户", "2024-03-29", "中国银行");addPageNum("D:\\test\\21234567891.pdf", "D:\\test\\212345678915.pdf");}public StringBuffer createReportTemplate(RiskModel2 riskModel2, String authDate, String person, String bankName, String reportNo,Boolean aBoolean,String beginDate,String endDate) {String begin = authDate.split("至")[0];String end = authDate.split("至")[1];StringBuffer template = new StringBuffer();template.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>客户风险分析报告</title></head>");template.append("<body><div>");String curentDateStr = DateUtils.getDateFormatter(new Date());template.append(getFirstHtml(riskModel2.getK1().getQymc(), riskModel2.getK1().getTyshxydm(),curentDateStr, authDate, bankName, person, reportNo,beginDate,endDate));template.append(ReportUtils2.title("企业概览", 1));template.append(K1.getHtml(riskModel2.getK1()));template.append(K1.score(riskModel2.getK1().getScore()));template.append(ReportUtils2.title("公司治理", 1));template.append(K3.getHtml1(riskModel2.getK3List()));template.append(ReportUtils2.title("风险信息", 1));template.append(K4.getHtml1(riskModel2.getK4List()));template.append(K5.getHtml(riskModel2.getK5List()));template.append(K4.getHtml2(riskModel2.getK4_1List()));template.append(K3.getHtml2(riskModel2.getK3_1List()));template.append(K6.getHtml1(riskModel2.getK6List(),aBoolean));template.append(K7.getHtml(riskModel2.getK7List(), riskModel2.getK7_1List(),aBoolean));template.append(K8.getHtml(riskModel2.getK8List(),aBoolean));template.append(K4.getHtml3(riskModel2.getK4_2List()));template.append(K6.getHtml2(riskModel2.getK6_1List(), "偿债能力", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));template.append(K6.getHtml2(riskModel2.getK6_2List(), "盈利能力", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));template.append(K6.getHtml2(riskModel2.getK6_3List(), "营运能力", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));template.append(ReportUtils2.title("授用信风险", 2));template.append(K6.getHtml2(riskModel2.getK6_4List(), "履约风险", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));template.append(K6.getHtml2(riskModel2.getK6_5List(), "过度授信风险", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));template.append(K6.getHtml2(riskModel2.getK6_6List(), "企业非银融资", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));template.append(K6.getHtml2(riskModel2.getK6_7List(), "其他授信异常", "本风险监测时段中,暂未监测到本企业存在异常。",aBoolean));template.append(ReportUtils2.title("关联风险", 2));template.append(K6.getHtml2(riskModel2.getK6_8List(), "工商信息监测", "本风险监测时段中,未监测到本企业存在企业关联人(企业)发生破产清算注销风险。",aBoolean));template.append(K9.getHtml(riskModel2.getK9List(),aBoolean));template.append(K6.getHtml2(riskModel2.getK6_9List(), "履约风险(征信)", "本风险监测时段中,未监测到本企业存在关联征信五级不良、关联企业在他行未结清业务发生逾期、关联企业非银机构授信异常风险。",aBoolean));template.append(ReportUtils2.title("名单监测", 2));template.append(ReportUtils2.title("系统性风险", 2));template.append(K6.getHtml3(riskModel2.getK6_10List(),aBoolean));template.append("</div>");template.append("</body>");template.append("</html>");return template;}public static StringBuffer getFirstHtml(String companyName, String companyCode, String time, String dataRetrievalCycle, String bankName, String person, String reportNo,String beginDate,String endDate) {StringBuffer firstHtml = new StringBuffer();firstHtml.append("<div align=\"left\"><img style=\"height:30px;width:280px\" src=\"").append(ConstantConfig.RISK_IMAGE + "苏州征信-logo.png").append("\"/></div>");firstHtml.append("<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>");firstHtml.append("<div>");firstHtml.append("<div align=\"right\" style=\"font-size:30px; font-weight: 900; color:#005BAB;\">").append(companyName).append("</div><br/>");firstHtml.append("<div align=\"right\" style=\"font-size:18px; font-weight: 500; color:#808080;\">统一社会信用代码:").append(companyCode).append("</div>");firstHtml.append("</div>");firstHtml.append("<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>");firstHtml.append("<div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">查询人员: ").append(person).append("</div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">查询机构: ").append(bankName).append("</div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">风险周期: ").append(beginDate+"至"+endDate).append("</div>");firstHtml.append("<div align=\"left\" style=\"font-size:16px; font-weight: 100; color:#808080;\">报告生成时间: ").append(time).append("</div>");firstHtml.append("</div><br/><br/><br/><br/><br/>");firstHtml.append("<div>");firstHtml.append("<div align=\"right\" style=\"font-size:20px; font-weight: 500; color:#4169E1;\">报告编号:").append(reportNo).append("</div>");firstHtml.append("</div><br/><br/><br/>");return firstHtml;}public static void addPageNum(String srcPdfPath, String tagetPdfPath) {try {FileOutputStream fos = new FileOutputStream(tagetPdfPath);PdfReader reader = new PdfReader(srcPdfPath);int num = reader.getNumberOfPages();Document document = new Document(PageSize.A4);PdfWriter writer = PdfWriter.getInstance(document, fos);writer.setPageEvent(new PdfNumPageEventTax(num));document.open();PdfContentByte pdfContent = writer.getDirectContent();for (int i = 1; i <= num; i++) {document.newPage();writer.setPageEmpty(false);PdfImportedPage page = writer.getImportedPage(reader, i);pdfContent.addTemplate(page, 0, 3);}document.close();reader.close();}catch (Exception e){e.printStackTrace();}}private static int interval = -5;public static void waterMark(String inputFile, String outputFile, String userName, String time,String bankName) {PdfReader reader = null;PdfStamper stamper = null;FileOutputStream outputStream = null;try {String waterMarkName = "长三角征信链";reader = new PdfReader(inputFile);outputStream = new FileOutputStream(outputFile);stamper = new PdfStamper(reader, outputStream);BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);Rectangle pageRect = null;PdfGState gs = new PdfGState();gs.setFillOpacity(0.15f);gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;JLabel label = new JLabel();FontMetrics metrics;int textH = 0;int textW = 0;label.setText(waterMarkName);metrics = label.getFontMetrics(label.getFont());textH = metrics.getHeight();textW = metrics.stringWidth(label.getText());PdfContentByte under;for (int i = 1; i < total; i++) {if(i==1 || i==total-1) continue;pageRect = reader.getPageSizeWithRotation(i);under = stamper.getOverContent(i);under.saveState();under.setGState(gs);under.beginText();under.setFontAndSize(base, 18);float pageHeight = pageRect.getHeight();float pageWidth = pageRect.getWidth();for (int height = interval + textH; height < pageHeight; height = height + textH * 8) {for (int width = interval + textW; width < pageWidth + textW; width = width + textW * 6) {under.setFontAndSize(base, 12);under.showTextAligned(Element.ALIGN_LEFT, "查询人员:"+userName, width - textW, height -textH , 30);under.setFontAndSize(base, 12);under.showTextAligned(Element.ALIGN_LEFT, "查询时间:"+time, width - textW+12, height -textH*2 , 30);under.setFontAndSize(base, 12);under.showTextAligned(Element.ALIGN_LEFT, "查询机构:"+bankName, width - textW+24, height -textH*3 , 30);under.setFontAndSize(base, 12);under.showTextAligned(Element.ALIGN_LEFT, "苏州企业征信服务有限公司", width - textW+36, height -textH*4 , 30);}}under.endText();}} catch (Exception e) {e.printStackTrace();}finally {try {if(stamper != null){stamper.close();}if(outputStream != null){outputStream.close();}if(reader != null){reader.close();}}catch (Exception e){e.printStackTrace();}}}
}
pdf_383">多线程导出pdf数据
java">public RiskModel2 queryRiskReport1(String corpKey,String beginDate,String endDate){RiskModel2 riskModel2 = new RiskModel2();CompletableFuture<K1> k1 = CompletableFuture.supplyAsync(() ->{K1 k_1 = riskReportMapper.queryK1(corpKey);k_1.setTyshxydm(corpKey);return k_1;}, creditReportExecutor);CompletableFuture<List<K3>> k3List = CompletableFuture.supplyAsync(() ->{List<K3> k_3List = riskReportMapper.queryK3List(corpKey, Arrays.asList("R001","R002","R003"),beginDate,endDate);return k_3List;}, creditReportExecutor);CompletableFuture<List<K4>> k4List = CompletableFuture.supplyAsync(() ->{List<K4> k_4List = riskReportMapper.queryK4List(corpKey,beginDate,endDate);return k_4List;}, creditReportExecutor);CompletableFuture<List<K5>> k5List = CompletableFuture.supplyAsync(() ->{List<K5> k_5List = riskReportMapper.queryK5List(corpKey,beginDate,endDate);return k_5List;}, creditReportExecutor);CompletableFuture<List<K4>> k4_1List = CompletableFuture.supplyAsync(() ->{List<K4> k_4List = riskReportMapper.queryK4_1List(corpKey, "经营风险",beginDate,endDate);return k_4List;}, creditReportExecutor);CompletableFuture<List<K3>> k3_1List = CompletableFuture.supplyAsync(() ->{List<K3> k_3List = riskReportMapper.queryK3List(corpKey, Arrays.asList("R004","R005"),beginDate,endDate);return k_3List;}, creditReportExecutor);CompletableFuture<List<K6>> k6List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "运营风险",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K7>> k7_1List = CompletableFuture.supplyAsync(() ->{List<K7> k_7List = riskReportMapper.queryK7_1List(corpKey, "司法风险",beginDate,endDate);return k_7List;}, creditReportExecutor);CompletableFuture<List<K7>> k7List = CompletableFuture.supplyAsync(() ->{List<K7> k_7List = riskReportMapper.queryK7List(corpKey, "司法风险",beginDate,endDate);return k_7List;}, creditReportExecutor);CompletableFuture<List<K8>> k8List = CompletableFuture.supplyAsync(() ->{List<K8> k_8List = riskReportMapper.queryK8List(corpKey, "行政处罚",beginDate,endDate);return k_8List;}, creditReportExecutor);CompletableFuture<List<K4>> k4_2List = CompletableFuture.supplyAsync(() ->{List<K4> k_4List = riskReportMapper.queryK4_1List(corpKey, "财务风险",beginDate,endDate);return k_4List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_1List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "偿债能力",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_2List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "盈利能力",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_3List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "营运能力",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_4List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "履约风险",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_5List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "过度授信风险",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_6List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "企业非银融资",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_7List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "其他授信异常",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K9>> k9List = CompletableFuture.supplyAsync(() ->{List<K9> k9_1List = riskReportMapper.queryK9List(corpKey, "司法信息监测",beginDate,endDate);return k9_1List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_9List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "履约风险(征信)",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<List<K10>> k10List = CompletableFuture.supplyAsync(() ->{List<K10> k10_1List = riskReportMapper.queryK10List(corpKey, "黑名单",beginDate,endDate);return k10_1List;}, creditReportExecutor);CompletableFuture<List<K6>> k6_10List = CompletableFuture.supplyAsync(() ->{List<K6> k_6List = riskReportMapper.queryK6List(corpKey, "行业风险",beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFuture<Void> allOf = CompletableFuture.allOf(k1, k3List, k4List, k5List, k3_1List, k4_1List, k6List, k7_1List, k7List, k4_2List, k8List, k6_1List, k6_2List, k6_3List, k6_4List, k6_5List, k6_6List, k6_7List, k9List, k6_9List, k10List, k6_10List);try {allOf.get();riskModel2 = RiskModel2.builder().k1(k1.get()).k3List(k3List.get()).k4List(k4List.get()).k5List(k5List.get()).k3_1List(k3_1List.get()).k4_1List(k4_1List.get()).k6List(k6List.get()).k7_1List(k7_1List.get()).k7List(k7List.get()).k4_2List(k4_2List.get()).k8List(k8List.get()).k6_1List(k6_1List.get()).k6_1List(k6_1List.get()).k6_2List(k6_2List.get()).k6_3List(k6_3List.get()).k6_4List(k6_4List.get()).k6_5List(k6_5List.get()).k6_6List(k6_6List.get()).k6_7List(k6_7List.get()).k9List(k9List.get()).k6_9List(k6_9List.get()).k6_10List(k6_10List.get()).k10List(k10List.get()).build();} catch (InterruptedException |ExecutionException e) {e.printStackTrace();log.error("查询企业{}风险报告出错",corpKey);}return riskModel2;}
pdf_537">pdf导出工具类(页眉、图片底纹、柱图、饼图、表格、一级二级标题)
java">package com.ruoyi.business.bankInterface.controller.risk.utils;import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.chart.HistogramChar;
import com.ruoyi.business.bankInterface.controller.risk.utils.chart.Pie2Chart;
import com.ruoyi.business.bankInterface.controller.tax.utils.annotation.ReportField;
import com.ruoyi.business.bankInterface.controller.tax.utils.annotation.ReportFieldAnnotation;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.jfree.ui.RectangleEdge;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.List;public class ReportUtils2 {private static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;public static StringBuffer listHtml(Class<?> clazz, String jsonStr, StringBuffer template, int tableType, int dataType){if(StringUtils.isBlank(jsonStr) || "{}".equals(jsonStr) || "[]".equals(jsonStr)) return template;Map<String, Object> data = null;if(dataType == 1){data = ReportUtils2.changeObjectList(clazz, jsonStr);}else{data = ReportUtils2.changeObject(clazz, jsonStr);}if(data == null || data.size() == 0) return template;List<ReportField> reportFieldList = (List<ReportField>)data.get("nameList");List<Map<String,Object>> valueList = (List<Map<String,Object>>)data.get("valueList");if(tableType == 1){template.append(ReportUtils2.listHtml(reportFieldList, valueList));}else if(tableType == 3){template.append(ReportUtils2.oneTitleHtml(reportFieldList, valueList));}else{template.append(ReportUtils2.twoTitleHtml(reportFieldList, valueList));}return template;}public static Map<String, Object> changeObjectList(Class<?> clazz, String jsonArrStr) {Set<Field> annotatedFields = getAnnotatedFields(clazz, ReportFieldAnnotation.class);List<ReportField> reportFieldList = new ArrayList<>();List<JSONObject> objectList = JSONArray.parseArray(jsonArrStr, JSONObject.class);List<Map<String, String>> valueList = new ArrayList<>(); Map<String, String> value = null;int flag = 0;for(JSONObject jsonObject : objectList){value = new HashMap<>();for (Field field : annotatedFields) {ReportField reportField = new ReportField();ReportFieldAnnotation annotation = field.getAnnotation(ReportFieldAnnotation.class);int dataType = annotation.dataType();String fieldName = field.getName();Object vObj = jsonObject.get(field.getName());if(dataType == 0){if(flag == 0) {reportField.setDesc(annotation.desc()); reportField.setName(fieldName); reportField.setSort(annotation.sort());reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);}value.put(fieldName, Objects.isNull(vObj) ? "" : vObj.toString().replaceAll("<", "").replaceAll(">", ""));}else if(dataType == 1){if("yearList".equals(field.getName())) continue;Object yearListObj = jsonObject.get("yearList");if(Objects.isNull(yearListObj) || Objects.isNull(vObj)) continue;List<String> yearList = JSONArray.parseArray(JSONObject.toJSONString(yearListObj), String.class);List<String> vList = JSONArray.parseArray(JSONObject.toJSONString(vObj), String.class);for(int i=0; i<yearList.size(); i++){String year = yearList.get(i);if(flag == 0) {reportField = new ReportField(); reportField.setDesc(year); reportField.setName(year); reportField.setSort(annotation.sort()); reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);}String v = vList.get(i);value.put(year, v == null ? "" : v.replaceAll("<", "").replaceAll(">", ""));}}}valueList.add(value); flag++;}reportFieldList.sort((t1, t2) -> { Integer counts = t1.getSort(); Integer counts1 = t2.getSort(); return counts.compareTo(counts1); });Map<String, Object> res = new HashMap<>();res.put("nameList", reportFieldList);res.put("valueList", valueList);return res;}public static Map<String, Object> changeObject(Class<?> clazz, String jsonStr) {Set<Field> annotatedFields = getAnnotatedFields(clazz, ReportFieldAnnotation.class);List<ReportField> reportFieldList = new ArrayList<>();JSONObject jsonObject = JSONObject.parseObject(jsonStr);List<Map<String, String>> valueList = new ArrayList<>(); Map<String, String> value = new HashMap<>();for (Field field : annotatedFields) {ReportField reportField = new ReportField();ReportFieldAnnotation annotation = field.getAnnotation(ReportFieldAnnotation.class);int dataType = annotation.dataType();String fieldName = field.getName();Object vObj = jsonObject.get(fieldName);if(dataType == 0){reportField.setDesc(annotation.desc()); reportField.setName(fieldName); reportField.setSort(annotation.sort()); reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);value.put(fieldName, Objects.isNull(vObj) ? "" : vObj.toString().replaceAll("<", "").replaceAll(">", ""));}else if(dataType == 1){Object yearListObj = jsonObject.get("yearList");if(Objects.isNull(yearListObj) || Objects.isNull(vObj)) continue;List<String> yearList = JSONArray.parseArray(JSONObject.toJSONString(yearListObj), String.class);List<String> vList = JSONArray.parseArray(JSONObject.toJSONString(vObj), String.class);for(int i=0; i<yearList.size(); i++){String year = yearList.get(i); String v = vList.get(i);reportField.setDesc(year); reportField.setName(year); reportField.setSort(annotation.sort()); reportField.setWidth(annotation.width()); reportField.setNum(annotation.num()); reportFieldList.add(reportField);value.put(year, v == null ? "" : v.replaceAll("<", "").replaceAll(">", ""));}}}valueList.add(value);Map<String, Object> res = new HashMap<>();reportFieldList.sort((t1, t2) -> { Integer counts = t1.getSort(); Integer counts1 = t2.getSort(); return counts.compareTo(counts1); });res.put("nameList", reportFieldList);res.put("valueList", valueList);return res;}public static Set<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotation) {Set<Field> fields = new HashSet<>();Class<?> superClass = clazz;while (superClass != Object.class) {for (Field field : superClass.getDeclaredFields()) {if (field.isAnnotationPresent(annotation)) {fields.add(field);}}superClass = superClass.getSuperclass();}return fields;}public static StringBuffer listHtml(List<ReportField> reportFieldList, List<Map<String,Object>> value){List<String> keyList = new ArrayList<>();StringBuffer tempHtml= new StringBuffer();tempHtml.append("<div class=\"table_box_b\">");tempHtml.append("<table width=\"100%\" cellspacing=\"0\">");tempHtml.append("<tbody>");tempHtml.append("<tr>"); int j=1; int size = reportFieldList.size();for(ReportField reportField : reportFieldList){if(j==size){tempHtml.append("<th height=\"30px\" width=\"").append(reportField.getWidth()).append("%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-bottom: 1px solid #F4F4F6;border-top:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");} else if(j==1){tempHtml.append("<th height=\"30px\" width=\"").append(reportField.getWidth()).append("%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-top:1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #FFFFFF;\">").append(reportField.getDesc()).append("</th>");}else{tempHtml.append("<th height=\"30px\" width=\"").append(reportField.getWidth()).append("%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-bottom: 1px solid #F4F4F6;border-right: 1px solid #FFFFFF;border-top:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");}keyList.add(reportField.getName()); j++;}tempHtml.append("</tr>");if(value != null && value.size() > 0){for(Map<String, Object> vMap : value) {tempHtml.append("<tr>"); int keySize = keyList.size(); int flag = 1;for (String key : keyList) {if(keySize==flag){tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-left:1px solid #F4F4F6; border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(key)).append("</td>");}else{tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-left:1px solid #F4F4F6; border-bottom:1px solid #F4F4F6;\">").append(vMap.get(key)).append("</td>");}flag++;}tempHtml.append("</tr>");}}tempHtml.append("</tbody></table></div>");return tempHtml;}public static StringBuffer twoTitleHtml(List<ReportField> reportFieldList, List<Map<String,Object>> value){StringBuffer tempHtml= new StringBuffer();for(Map<String, Object> vMap : value){tempHtml.append("<div class=\"table_box_b\">");tempHtml.append("<table width=\"100%\" cellspacing=\"0\" style=\"border-right:1px solid #cdd3e0; border-top:1px solid #cdd3e0; \">");tempHtml.append("<tbody>");int flag = 1;for(ReportField reportField : reportFieldList){if(flag == 1 && flag == reportFieldList.size()){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6; \">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;padding:5px 5px; line-height:12px;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else if(flag == 1 || flag == 2){if(flag == 1){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-top:1px solid #F4F4F6;border-bottom: 1px solid #FFFFFF;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;padding:5px 5px; line-height:12px;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");}else{tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-top:1px solid #F4F4F6;border-bottom: 1px solid #FFFFFF;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;padding:5px 5px; line-height:12px;border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}} else{if(flag%2 == 1 && flag == reportFieldList.size()){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;;padding:5px 5px; line-height:12px;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\" colspan=\"3\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else if(flag%2 == 0){tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-bottom: 1px solid #FFFFFF;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;;padding:5px 5px; line-height:12px;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else{tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"background-color:#EAF2FA; color:#000000; font-size:10px;font-weight: 500;border-bottom: 1px solid #FFFFFF;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"30%\" nowrap=\"nowrap\" align=\"left\" style=\"font-size:10px;;padding:5px 5px; line-height:12px;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");}}flag++;}tempHtml.append("</tbody></table></div>");}return tempHtml;}public static StringBuffer oneTitleHtml(List<ReportField> reportFieldList, List<Map<String,Object>> value){StringBuffer tempHtml= new StringBuffer();for(Map<String, Object> vMap : value){tempHtml.append("<div class=\"table_box_b\">");tempHtml.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" style=\"border-right:1px solid #cdd3e0; border-top:1px solid #cdd3e0;\">");tempHtml.append("<tbody>");int flag = 1;for(ReportField reportField : reportFieldList){if(flag == 1){tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-top:1px solid #F4F4F6;border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"75%\" nowrap=\"nowrap\" align=\"left\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-top:1px solid #F4F4F6;border-bottom:1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}else{tempHtml.append("<tr>");tempHtml.append("<th height=\"30px\" width=\"25%\" nowrap=\"nowrap\" align=\"center\" style=\"").append(ReportLabelUtils.TH_FONT).append("border-bottom: 1px solid #F4F4F6;border-left: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(reportField.getDesc()).append("</th>");tempHtml.append("<td height=\"30px\" width=\"75%\" nowrap=\"nowrap\" align=\"left\" style=\"").append(ReportLabelUtils.TD_FONT).append("border-bottom: 1px solid #F4F4F6;border-right:1px solid #F4F4F6;\">").append(vMap.get(reportField.getName())).append("</td>");tempHtml.append("</tr>");}flag++;}tempHtml.append("</tbody></table></div>");}return tempHtml;}public static StringBuffer title(String content, int level){StringBuffer description = new StringBuffer();if(level == 1) {description.append("<br/><div align=\"left\"><img style=\"height:30px;width:520px;\" src=\"").append(forTitle1(content)).append("\"/></div>");}if(level == 2) {description.append("<br/><div align=\"left\"><img style=\"height:30px;width:520px;\" src=\"").append(forTitle2(content)).append("\"/></div>");}if(level == 3) {description.append("<br/><div>");description.append("<img style=\"height: 15px; width:10px\" src=\"").append(ConstantConfig.RISK_IMAGE + "三级标题.jpg").append("\"/>  <span style=\"font-weight: 900;color:#005BAB;\">").append(content).append("</span>");description.append("</div>");}return description;}public static StringBuffer description(String content, int fontSize, String color, String backgroundColor, int fontWeight){StringBuffer description = new StringBuffer();
description.append("<div align=\"left\" style=\"");if(fontWeight > 0) description.append("font-weight:").append(fontWeight).append(";");if(fontSize > 0) description.append("font-size:").append(fontSize).append("px;");if(StringUtils.isNotBlank(color)) description.append("color:").append(color).append(";");if(StringUtils.isNotBlank(color)) description.append("padding-left:12px;padding-right:12px;padding-bottom:12px;background-color:").append(backgroundColor).append(";");description.append("line-height:").append(1.5).append(";");description.append("letter-spacing:").append(1.2).append("px;");description.append("\">");description.append(" ");description.append(content).append("</div>");
return description;}public static StringBuffer getImgOneHtml(String title, List<String> xData, List<Double> yDataList, List<Color> colorList, String yUnit, int type,int width, int height, double SpaceFill, StringBuffer tempHtml) {if (xData == null || xData.size() == 0) return tempHtml;String path = new HistogramChar<>().getChart2(title, xData, yDataList, colorList, yUnit, type,width, height, SpaceFill);tempHtml.append("<div align=\"center\"><img style=\"height:262px;width:700px\" src=\"").append(path).append("\"/></div>");return tempHtml;}public static StringBuffer getImgPieHtml(List<String> keyList, List<Double> rDataList, Boolean isTitleVisible, String title,Boolean isLegendVisible, RectangleEdge rectangleEdge, int flag, StringBuffer tempHtml) {if (rDataList == null || rDataList.size() == 0) return tempHtml;tempHtml.append("<div align=\"center\">");String path = Pie2Chart.pieChart(keyList, rDataList, isTitleVisible, title, isLegendVisible, rectangleEdge, flag);tempHtml.append("<img style=\"width:700px\" src=\"").append(path).append("\"/>");tempHtml.append("</div>");return tempHtml;}public static void addImageWatermark(String srcPdfPath, String tagetPdfPath, int pageNum, int type) {try {PdfReader reader = new PdfReader(srcPdfPath);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tagetPdfPath));int total = reader.getNumberOfPages();if(pageNum < 0) pageNum = total + pageNum;com.itextpdf.text.Image watermark = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-头部-底纹.png");watermark.setAbsolutePosition(-2, 705); watermark.scaleAbsolute(597, 135); com.itextpdf.text.Image watermark1 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-中间-底纹.png");watermark1.setAbsolutePosition(-2, 155); watermark1.scaleAbsolute(597, 700); com.itextpdf.text.Image watermark_word1 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + (type==0 ? "客户风险分析报告" : "客群风险分析报告") +".png");watermark_word1.setAbsolutePosition(200, 515); watermark_word1.scaleAbsolute(360, 40); com.itextpdf.text.Image watermark_line1 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-矩形-底纹.png");watermark_line1.setAbsolutePosition(200, 485); watermark_line1.scaleAbsolute(360, 2); com.itextpdf.text.Image watermark_line2 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-分割线-底纹.png");watermark_line2.setAbsolutePosition(-2, 153); watermark_line2.scaleAbsolute(597, 1); com.itextpdf.text.Image watermark2 = com.itextpdf.text.Image.getInstance(ConstantConfig.RISK_IMAGE + "首页-底部-底纹.png");watermark2.setAbsolutePosition(-2, -2); watermark2.scaleAbsolute(597, 215); PdfContentByte content = stamper.getOverContent(pageNum);content.addImage(watermark);content.addImage(watermark1);content.addImage(watermark_word1);content.addImage(watermark_line1);content.addImage(watermark_line2);content.addImage(watermark2);PdfImportedPage page = stamper.getImportedPage(reader, pageNum);int rotation = reader.getPageRotation(1);if (rotation == 90 || rotation == 270) {content.addTemplate(page, 0, -1, 1, 0, 0, 610);} else {content.addTemplate(page, 1, 0, 0, 1, 0, 0);}stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();}}public static void pdf_yemei(String companyName, String pdfFilePath, String toFdfPath) {try {String NoFirstYeMeiImage = baseUrl + companyName + "合并后图片.png";getImageAndChar2(ConstantConfig.RISK_IMAGE + "非首页-页眉.jpg", NoFirstYeMeiImage, companyName);PdfReader reader = new PdfReader(pdfFilePath);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(toFdfPath));int total = reader.getNumberOfPages();int pageNum = 1;while(pageNum < total) {pageNum++;if (pageNum <= 1) continue;com.itextpdf.text.Image yemei = com.itextpdf.text.Image.getInstance(NoFirstYeMeiImage);yemei.setAbsolutePosition(0, 807); yemei.scaleAbsolute(597, 33); PdfContentByte content = stamper.getOverContent(pageNum);content.addImage(yemei);PdfImportedPage page = stamper.getImportedPage(reader, pageNum);int rotation = reader.getPageRotation(1);if (rotation == 90 || rotation == 270) {content.addTemplate(page, 0, -1, 1, 0, 0, 610);} else {content.addTemplate(page, 1, 0, 0, 1, 0, 0);}}stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();}}public static void getImageAndChar2(String srcImagePath, String toImagePath, String content){try {BufferedImage image1 = ImageIO.read(new File(srcImagePath));BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());Graphics2D g2d = mergedImage.createGraphics();g2d.drawImage(image1, 0, 0, null);Font font = new Font("黑体", Font.BOLD, 70); g2d.setFont(font); g2d.setColor(Color.white);String text = content; int x = 270; int y = 90; for (int i = 0; i < text.length(); i++) {char c = text.charAt(i);g2d.drawString(String.valueOf(c), x + i * 70, y); }g2d.dispose();ImageIO.write(mergedImage, "png", new File(toImagePath));} catch (Exception e) {e.printStackTrace();}}public static String forTitle1(String content){String toImagePath = baseUrl + System.currentTimeMillis();try {BufferedImage image1 = ImageIO.read(new File(ConstantConfig.RISK_IMAGE + "一级标题.jpg"));BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());Graphics2D g2d = mergedImage.createGraphics();g2d.drawImage(image1, 0, 0, null);Font font = new Font("黑体", Font.BOLD, 50); g2d.setFont(font); g2d.setColor(new Color(0, 91, 171));String text = content; int x = 240; int y = 60; for (int i = 0; i < text.length(); i++) {char c = text.charAt(i);g2d.drawString(String.valueOf(c), x + i * 55, y); }g2d.dispose();ImageIO.write(mergedImage, "jpg", new File(toImagePath));} catch (Exception e) {e.printStackTrace();}return toImagePath;}public static String forTitle2(String content){String toImagePath = baseUrl + System.currentTimeMillis();try {BufferedImage image1 = ImageIO.read(new File(ConstantConfig.RISK_IMAGE + "二级标题.jpg"));BufferedImage mergedImage = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());Graphics2D g2d = mergedImage.createGraphics();g2d.drawImage(image1, 0, 0, null);Font font = new Font("黑体", Font.BOLD, 40); g2d.setFont(font); g2d.setColor(Color.white);String text = content; int x = 50; int y = 50; for (int i = 0; i < text.length(); i++) {char c = text.charAt(i);g2d.drawString(String.valueOf(c), x + i * 45, y); }g2d.dispose();ImageIO.write(mergedImage, "jpg", new File(toImagePath));} catch (Exception e) {e.printStackTrace();}return toImagePath;}}
实体类
java">package com.ruoyi.business.bankInterface.controller.risk.model1;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.controller.risk.utils.ReportLabelUtils;
import com.ruoyi.business.bankInterface.controller.risk.utils.ReportUtils2;
import com.ruoyi.business.bankInterface.controller.tax.utils.annotation.ReportFieldAnnotation;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;import java.util.ArrayList;
import java.util.List;
@Data
public class K3 {@ReportFieldAnnotation(desc = "变更时间", sort = 1)private String bgsj;@ReportFieldAnnotation(desc = "变更事项", sort = 2)private String bgsx;@ReportFieldAnnotation(desc = "变更前", sort = 3)private String bgq;@ReportFieldAnnotation(desc = "变更后", sort = 4)private String bgh;@ReportFieldAnnotation(desc = "风险等级", sort = 5)private String fxdj;private String fxxl;public String getBgq() {if(StringUtils.isNotBlank(bgq)){int start = bgq.indexOf("(");int end = bgq.indexOf(")");if(start > 0 && end > 0){bgq = bgq.substring(0, start)+bgq.substring(end+1);}}return bgq;}public String getBgh() {if(StringUtils.isNotBlank(bgh)){int start = bgh.indexOf("(");int end = bgh.indexOf(")");if(start > 0 && end > 0){bgh = bgh.substring(0, start)+bgh.substring(end+1);}}return bgh;}public String getFxdj() {if(StringUtils.isNotBlank(fxdj)){if("红".equals(fxdj)) fxdj = "严重警告";if("橙".equals(fxdj)) fxdj = "警告";if("黄".equals(fxdj)) fxdj = "关注";if("提示".equals(fxdj)) fxdj = "提示";}return fxdj;}public static StringBuffer getHtml1(List<K3> k3List){StringBuffer k3Html = new StringBuffer();if(k3List == null || k3List.size() == 0){k3Html.append(ReportUtils2.description("监测本公司近一年的注册资本、企业股东或高管、企业法定代表人变更情况,皆未发现有异常更改记录。", 12, ReportLabelUtils.NO_FX_COLOR, ReportLabelUtils.NO_FX_BACKGROUND_COLOR, 0));}else{k3Html.append(ReportUtils2.description("监测本公司近一年的注册资本、企业股东或高管、企业法定代表人变更情况,发现以下风险:", 12, ReportLabelUtils.FX_COLOR, ReportLabelUtils.FX_BACKGROUND_COLOR, 0));ReportUtils2.listHtml(K3.class, JSONArray.toJSONString(changeK3List(k3List)), k3Html, 1, 1);}return k3Html;}public static StringBuffer getHtml2(List<K3> k3List){StringBuffer k3Html = new StringBuffer();k3Html.append(ReportUtils2.title("经营资质", 3));if(k3List == null || k3List.size() == 0){k3Html.append(ReportUtils2.description("本风险监测时段中,未监测到企业存在经营资质风险。", 12, ReportLabelUtils.NO_FX_COLOR, ReportLabelUtils.NO_FX_BACKGROUND_COLOR, 0));}else{StringBuffer text = new StringBuffer();text.append("在本风险监测时段中,监测到"+k3List.size()+"条经营资质风险,其中");if (k3List.stream().filter(item->"R004".equals(item.getFxxl())).count()>0)text.append("企业工商信息变更风险"+k3List.stream().filter(item->"R004".equals(item.getFxxl())).count()+"条。");if (k3List.stream().filter(item->"R005".equals(item.getFxxl())).count()>0)text.append("企业经营期限到期或状态为‘非存续’风险"+k3List.stream().filter(item->"R005".equals(item.getFxxl())).count()+"条。");k3Html.append(ReportUtils2.description(
text.toString(), 12, ReportLabelUtils.FX_COLOR, ReportLabelUtils.FX_BACKGROUND_COLOR, 0));ReportUtils2.listHtml(K3.class, JSONArray.toJSONString(k3List), k3Html, 1, 1);}return k3Html;}private static List<K3> changeK3List(List<K3> k3List){if(k3List == null || k3List.size() == 0) return k3List;List<K3> k3NewList = new ArrayList<>();for(K3 k3 : k3List){try {if("企业注册资本抽离".equals(k3.getBgsx()) && Double.valueOf(k3.getBgq()) <= Double.valueOf(k3.getBgh())) continue;k3NewList.add(k3);}catch (Exception e){}}return k3NewList;}}
pdfhtmlpdf_1192">pdf工具类(根据html生成pdf文件)
java">package com.ruoyi.business.bankInterface.controller.risk;import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.ruoyi.business.bankInterface.controller.tax.utils.DateUtils;
import com.ruoyi.business.bankInterface.domain.utils.AsianFontProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.List;public class PdfUtils {
public static final String imgPrefixUrl="D:\\test\\images\\";private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
public static boolean mergePdfFiles(List<String> files, String newfile) {boolean retValue = false;Document document = null;OutputStream os = null;PdfReader reader1 = null;PdfCopy copy = null;PdfReader reader = null;try {reader1 = new PdfReader(files.get(0));document = new Document(reader1.getPageSize(1));os = new FileOutputStream(newfile);copy = new PdfCopy(document, os);document.open();for (int i = 0; i < files.size(); i++) {reader = new PdfReader(files.get(i));int n = reader.getNumberOfPages();for (int j = 1; j <= n; j++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, j);copy.addPage(page);}}retValue = true;} catch (Exception e) {e.printStackTrace();logger.info("合并文件.error:"+e.getMessage());} finally {try {if(reader != null){reader.close();}if(copy != null){copy.close();}if(os != null){os.close();}if(reader1 != null){reader1.close();}if(document != null){document.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}logger.info("合并pdf文件完成!");return retValue;}public static boolean isEncryption(String oFile) {boolean retValue = false;PdfReader reader = null;PdfStamper stamper = null;try {String waterMarkName = "长三角征信链";reader = new PdfReader(oFile);stamper = new PdfStamper(reader, new FileOutputStream(oFile+"123"));BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);Rectangle pageRect = null;PdfGState gs = new PdfGState();gs.setFillOpacity(0.2f);gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;JLabel label = new JLabel();FontMetrics metrics;int textH = 0;int textW = 0;label.setText(waterMarkName);metrics = label.getFontMetrics(label.getFont());textH = metrics.getHeight();textW = metrics.stringWidth(label.getText());PdfContentByte under;for (int i = 1; i < total; i++) {pageRect = reader.getPageSizeWithRotation(i);under = stamper.getOverContent(i);under.saveState();under.setGState(gs);under.beginText();under.setFontAndSize(base, 15);for (int height = interval + textH; height < pageRect.getHeight();height = height + textH*5) {for (int width = interval + textW; width < pageRect.getWidth() + textW;width = width + textW*2) {under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW,height - textH, 30);}}under.endText();}} catch (Exception e) {retValue = true;}finally {try {if(stamper != null){stamper.close();}if(reader != null){reader.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}return retValue;}public static byte[] fileToByteArray(File file) {byte[] imagebs = null;FileInputStream fis = null;ByteArrayOutputStream baos = null;try {fis = new FileInputStream(file);baos = new ByteArrayOutputStream();int len;byte[] buffer = new byte[1024];while ((len = fis.read(buffer)) != -1) {baos.write(buffer, 0, len);}imagebs = baos.toByteArray();baos.close();fis.close();} catch (Exception e) {e.printStackTrace();}finally {try {if(baos != null){baos.close();}if(fis != null){fis.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}return imagebs;}public static void getFile(byte[] bfile, String filePath) {BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null;try {String path = filePath.substring(0,filePath.lastIndexOf(File.separator));File dir = new File(path);if(!dir.exists()){dir.mkdirs();}file = new File(filePath); fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(bfile);} catch (Exception e) {e.printStackTrace();} finally {if (bos != null) {try {bos.close();} catch (Exception e1) {e1.printStackTrace();}}if (fos != null) {try {fos.close();} catch (Exception e1) {e1.printStackTrace();}}}}public static void renameFile(String oldname,String newname){logger.info("重命名文件开始oldname=" + oldname + "&newname=" + newname);if(!oldname.equals(newname)){File oldfile=new File(oldname);File newfile=new File(newname);if(!oldfile.exists()){logger.info("重命名文件不存在!");return;}if(newfile.exists())logger.info(newname+"已经存在!");else{oldfile.renameTo(newfile);}}else{logger.info("新文件名和旧文件名相同...");}}private static int interval = -5;public static void waterMark(String inputFile,String outputFile,String userName,String time) {PdfReader reader = null;PdfStamper stamper = null;FileOutputStream outputStream = null;try {String waterMarkName = "长三角";reader = new PdfReader(inputFile);outputStream = new FileOutputStream(outputFile);stamper = new PdfStamper(reader, outputStream);BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);Rectangle pageRect = null;PdfGState gs = new PdfGState();gs.setFillOpacity(0.15f);gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;JLabel label = new JLabel();FontMetrics metrics;int textH = 0;int textW = 0;label.setText(waterMarkName);metrics = label.getFontMetrics(label.getFont());textH = metrics.getHeight();textW = metrics.stringWidth(label.getText());PdfContentByte under;for (int i = 1; i < total; i++) {pageRect = reader.getPageSizeWithRotation(i);under = stamper.getOverContent(i);under.saveState();under.setGState(gs);under.beginText();under.setFontAndSize(base, 18);float pageHeight = pageRect.getHeight();float pageWidth = pageRect.getWidth();for (int height = interval + textH; height < pageHeight; height = height + textH * 8) {for (int width = interval + textW; width < pageWidth + textW; width = width + textW * 3) {under.setFontAndSize(base, 18);under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height -textH , 30);under.setFontAndSize(base, 12);under.showTextAligned(Element.ALIGN_LEFT, userName, width - textW+12, height -textH*2 , 30);under.setFontAndSize(base, 12);under.showTextAligned(Element.ALIGN_LEFT, time, width - textW+24, height -textH*3 , 30);}}under.endText();}} catch (Exception e) {e.printStackTrace();}finally {try {if(stamper != null){stamper.close();}if(outputStream != null){outputStream.close();}if(reader != null){reader.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}}public static void main(String[] args) {
System.out.println("endStr=" + "xxx.pdf".substring(0, "xxx.pdf".length() - 4));
}public static void zipToFile(String sourceFile, String toFolder) throws Exception {InputStream inputStream = null;OutputStream outputStream = null;ZipFile zfile = null;try {String toDisk = toFolder;zfile = new ZipFile(sourceFile);logger.info("要解压的文件是:"+ zfile.getName());Enumeration zList = zfile.entries();ZipEntry ze = null;byte[] buf = new byte[1024];while (zList.hasMoreElements()) {ze = (ZipEntry) zList.nextElement();if (ze.isDirectory()) {logger.info("打开zip文件里的文件夹:"+ ze.getName() +"skipped...");continue;}logger.info("zip包里的文件:"+ ze.getName() +" "+"大小为:" + ze.getSize() +"KB");outputStream = new BufferedOutputStream(new FileOutputStream(getRealFileName(toDisk, ze.getName())));inputStream = new BufferedInputStream(zfile.getInputStream(ze));int readLen = 0;while ((readLen = inputStream.read(buf, 0, 1024)) != -1) {outputStream.write(buf, 0, readLen);}inputStream.close();outputStream.close();logger.info("已经解压出:"+ ze.getName());}zfile.close();}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}finally {try {if(inputStream != null){inputStream.close();}if(outputStream != null){outputStream.close();}if(zfile != null){zfile.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}}private static File getRealFileName(String zippath, String absFileName){String[] dirs = absFileName.split("/", absFileName.length());File ret = new File(zippath);if (dirs.length > 1) {for (int i = 0; i < dirs.length - 1; i++) {ret = new File(ret, dirs[i]);}}if (!ret.exists()) {ret.mkdirs();}ret = new File(ret, dirs[dirs.length - 1]);return ret;}public static void htmp2pdf(String creditName, String htmlTemplate,String pdfName,boolean hasBackground,boolean isTrue){String path = pdfName.substring(0,pdfName.lastIndexOf(File.separator));File dir = new File(path);if(!dir.exists()){dir.mkdirs();}Document document = null;PdfWriter pdfWriter = null;FileOutputStream fileOutputStream = null;ByteArrayInputStream byteArrayInputStream = null;try {document = new Document(PageSize.A4);fileOutputStream = new FileOutputStream(pdfName);pdfWriter = PdfWriter.getInstance(document, fileOutputStream);document.open();document.addAuthor("Soren");document.addCreator("Soren");document.addCreationDate();document.addTitle("测试生成pdf");if(hasBackground){Image image = Image.getInstance(imgPrefixUrl+"background.jpg");image.setAbsolutePosition(0, 80);image.scaleAbsolute(595, 842);document.add(image);}htmlTemplate = htmlTemplate.replaceAll("###TITLE###", "长三角报告");htmlTemplate = htmlTemplate.replaceAll("###INTRODUCE###", "该报告来源:"+creditName);htmlTemplate = htmlTemplate.replaceAll("###date###", "日期:"+ DateUtils.convertYMD(new Date()));XMLWorkerHelper helper = XMLWorkerHelper.getInstance();byteArrayInputStream = new ByteArrayInputStream(htmlTemplate.getBytes("UTF-8"));helper.parseXHtml(pdfWriter, document, byteArrayInputStream, Charset.forName("utf8"), new AsianFontProvider());logger.info("pdf文件转换完成!");
} catch (FileNotFoundException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {if(document!=null){document.close();}if(fileOutputStream!=null){fileOutputStream.close();}if(byteArrayInputStream!=null){byteArrayInputStream.close();}if(pdfWriter!=null){pdfWriter.close();}}catch (Exception e){e.printStackTrace();logger.info(e.getMessage());}}}public static boolean deleteFile(String fileName) {File file = new File(fileName);if (file.exists() && file.isFile()) {if (file.delete()) {logger.info("删除单个文件" + fileName + "成功!");return true;} else {logger.info("删除单个文件" + fileName + "失败!");return false;}} else {logger.info("删除单个文件失败:" + fileName + "不存在!");return false;}}
}
页脚工具类
java">package com.ruoyi.business.bankInterface.controller.tax.utils.pdf;import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.*;
public class PdfNumPageEventTax extends PdfPageEventHelper {private int total;PdfNumPageEventTax(){}public PdfNumPageEventTax(int num){this.total = num;}@Overridepublic void onEndPage(PdfWriter writer, Document document) {try {if(writer.getPageNumber() ==1) return;PdfContentByte pdfContent = writer.getDirectContent();pdfContent.saveState();pdfContent.beginText();int footerFontSize = 10;BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);Font fontDetail = new Font(baseFont, footerFontSize, Font.NORMAL);pdfContent.setFontAndSize(baseFont, footerFontSize);String footerNum = String.format("第%d页 共"+total+"页", writer.getPageNumber());Phrase phrase = new Phrase(footerNum, fontDetail);float x = ( document.left() + document.right() ) / 2 +10;float y = document.bottom(-20) ;ColumnText.showTextAligned(pdfContent, Element.ALIGN_CENTER, phrase, x, y, 0);pdfContent.endText();pdfContent.restoreState();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onStartPage(PdfWriter writer, Document document) {
super.onStartPage(writer, document);}
}
柱图工具类
java">package com.ruoyi.business.bankInterface.controller.risk.utils.chart;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.ColorEnum;
import org.apache.commons.lang3.StringUtils;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.CategorySeries;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.style.Styler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HistogramChar <C extends Chart<?, ?>> {private static final Logger logger = LoggerFactory.getLogger(HistogramChar.class);private static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;public static void main(String[] args) throws IOException {List<String> titleList = JSONArray.parseArray("[\"严重警告\", \"警告\", \"关注\", \"提示\"]", String.class);List<Double> yData1 = JSONArray.parseArray("[6.0, 3.0, 1.0, 5.0]", Double.class);String zhuImg = new HistogramChar().getChart2("风险等级分布", titleList, yData1, ColorEnum.lColorList2, "%", 0,700, 263, 0.2);logger.info("柱状图" + zhuImg);}public String getChart(String title, List<String> titleList, List<String> xData, List<List<Double>> yDataList, List<Color> colorList, String yUnit, int type,int width, int height, double SpaceFill) {File file = new File(baseUrl);if (!file.exists()){file.mkdirs();}try {String path = baseUrl + titleList.get(0)+ System.currentTimeMillis()+".png";CategoryChart chart = new CategoryChartBuilder().width(width).height(height).theme(Styler.ChartTheme.Matlab).build();chart = chartTitle(chart, title);chart.getStyler().setPlotGridLinesVisible(true);chart.getStyler().setPlotGridLinesColor(new Color(184, 200,240));chart.getStyler().setPlotBorderColor(new Color(236, 238,240));chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);chart.getStyler().setXAxisLabelRotation(0);if(xData != null && xData.size() > 6){chart.getStyler().setXAxisLabelRotation(40);}chart.setYAxisGroupTitle(0, yUnit);chart.getStyler().setYAxisGroupPosition(0, Styler.YAxisPosition.Left);chart.getStyler().setLegendFont(new Font("宋体", Font.BOLD, 25));chart.getStyler().setAxisTitleFont(new Font("宋体", Font.BOLD, 25));chart.getStyler().setLabelsFont(new Font("宋体", Font.BOLD, 25));chart.getStyler().setLegendBorderColor(Color.white);chart.getStyler().setAvailableSpaceFill(SpaceFill);chart.getStyler().setYAxisDecimalPattern("#");chart.getStyler().setChartPadding(10);chart.getStyler().setPlotBorderVisible(true);
chart.getStyler().setAxisTickMarksColor(Color.white);if(type == 0 && titleList != null && titleList.size() > 0){chart.getStyler().setOverlapped(false);for(int i=0; i < titleList.size(); i++){CategorySeries series1 = chart.addSeries(titleList.get(i), xData, yDataList.get(i));series1.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Bar);series1.setFillColor(colorList.get(i));series1.setYAxisGroup(0);}}if(type == 1 && titleList != null && titleList.size() > 0){chart.getStyler().setOverlapped(true);for(int i=0; i < titleList.size(); i++){CategorySeries series2 = chart.addSeries(titleList.get(i), xData, yDataList.get(i));series2.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Line);series2.setYAxisGroup(0);series2.setLineColor(colorList.get(i));series2.setMarkerColor(colorList.get(i));}}BitmapEncoder.saveBitmap(chart, path, BitmapEncoder.BitmapFormat.PNG);return path;}catch (Exception e){e.printStackTrace();}return null;}public String getChart2(String title, List<String> xDataList, List<Double> yDataList, List<Color> colorList, String yUnit, int type,int width, int height, double SpaceFill) {File file = new File(baseUrl);if (!file.exists()){file.mkdirs();}try {String path = baseUrl + xDataList.get(0)+ System.currentTimeMillis()+".png";CategoryChart chart = new CategoryChartBuilder().width(width).height(height).theme(Styler.ChartTheme.Matlab).build();chart = chartTitle(chart, title);chart = chartLegend(chart);chart = chartGridOrBorder(chart);chart = chartXY(chart, xDataList.size() > 6 ? true : false, yUnit);chart.getStyler().setAvailableSpaceFill(SpaceFill);chart.getStyler().setOverlapped(true);if(type == 0 && xDataList != null && xDataList.size() > 0){for(int i=0; i < xDataList.size(); i++){List<Double> yData = new ArrayList<>();for(int y=0; y < yDataList.size(); y++){if(y==i){yData.add(yDataList.get(i));}else{yData.add(0.0);}}CategorySeries series1 = chart.addSeries(xDataList.get(i), xDataList, yData);series1.setChartCategorySeriesRenderStyle(CategorySeries.CategorySeriesRenderStyle.Bar);series1.setFillColor(colorList.get(i));series1.setYAxisGroup(0);}}BitmapEncoder.saveBitmap(chart, path, BitmapEncoder.BitmapFormat.PNG);return path;}catch (Exception e){e.printStackTrace();}return null;}private CategoryChart chartTitle(CategoryChart chart, String title){if(StringUtils.isNotBlank(title)){chart.setTitle(title);chart.getStyler().setChartTitleFont(new Font("宋体", Font.BOLD, 25));chart.getStyler().setChartTitleBoxBackgroundColor(new Color(234,242,250));chart.getStyler().setChartTitleBoxVisible(true);chart.getStyler().setChartTitlePadding(15);}return chart;}private CategoryChart chartLegend(CategoryChart chart){chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);chart.getStyler().setLegendFont(new Font("宋体", Font.PLAIN, 20));chart.getStyler().setLegendBorderColor(Color.white);return chart;}private CategoryChart chartGridOrBorder(CategoryChart chart){chart.getStyler().setPlotGridLinesVisible(true);chart.getStyler().setPlotGridLinesColor(new Color(184, 200,240));chart.getStyler().setPlotBorderColor(new Color(236, 238,240));chart.getStyler().setPlotBorderVisible(true);chart.getStyler().setChartPadding(10);return chart;}private CategoryChart chartXY(CategoryChart chart, Boolean isRotation, String yUnit){chart.getStyler().setAxisTickMarksColor(Color.white);chart.getStyler().setAxisTitleFont(new Font("宋体", Font.PLAIN, 25));chart.getStyler().setAxisTickLabelsFont(new Font("宋体", Font.PLAIN, 20));chart.getStyler().setXAxisLabelRotation(0);if(isRotation){chart.getStyler().setXAxisLabelRotation(40);}chart.setYAxisGroupTitle(0, yUnit);chart.getStyler().setYAxisGroupPosition(0, Styler.YAxisPosition.Left);chart.getStyler().setYAxisDecimalPattern("#");
return chart;}
}
饼图工具类
java">package com.ruoyi.business.bankInterface.controller.risk.utils.chart;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.ColorEnum;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleEdge;import java.awt.*;
import java.io.File;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;public class Pie2Chart {private static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;public static void main(String[] args) throws Exception{List<String> keyList = Arrays.asList("无任何司法异常", "涉及刑事诉讼且为被告人", "涉重大经济纠纷", "涉及经济纠纷", "涉及劳动纠纷","近一年立案数量大于3", "当前存在执行公开信息", "新增查封信息", "失信被执行");List<Double> rDataList = JSONArray.parseArray("[40, 15, 1, 8, 17, 5, 10, 2, 2]", Double.class);Boolean isTitleVisible = true;String title = "司法风险分布";Boolean isLegendVisible = false;RectangleEdge rectangleEdge = RectangleEdge.BOTTOM;String path = pieChart(keyList, rDataList, isTitleVisible, title, isLegendVisible, rectangleEdge, 1);System.out.println(path);}public static String pieChart(List<String> keyList, List<Double> rDataList, Boolean isTitleVisible, String title,Boolean isLegendVisible, RectangleEdge rectangleEdge, int flag){String path = baseUrl + System.currentTimeMillis()+".png";try {DefaultPieDataset dataset = new DefaultPieDataset();for(int i=0; i<keyList.size(); i++){dataset.setValue(keyList.get(i), rDataList.get(i));}JFreeChart chart = ChartFactory.createPieChart(title, dataset, true, false,false);chart = chartLegend(chart, isLegendVisible, rectangleEdge);chart = chartTitle(chart, isTitleVisible);chart = chartPiePlot(keyList, chart, flag);ChartUtilities.saveChartAsPNG(new File(path), chart, 700, 350);}catch (Exception e){e.printStackTrace();}return path;}private static JFreeChart chartLegend(JFreeChart chart, Boolean isLegendVisible, RectangleEdge rectangleEdge){if(isLegendVisible){Font font = new Font("宋体", Font.PLAIN, 12);chart.getLegend().setItemFont(font);chart.getLegend().setBorder(0, 0, 0, 0);chart.getLegend().setMargin(10, 30, 10, 0);chart.getLegend().setPosition(rectangleEdge);}else{chart.getLegend().setVisible(false);}return chart;}private static JFreeChart chartTitle(JFreeChart chart, Boolean isTitleVisible){if(isTitleVisible){Font titleFont = new Font("宋体", Font.BOLD, 25);chart.getTitle().setFont(titleFont); chart.getTitle().setMargin(10, 30, 10, 0);chart.getTitle().setBackgroundPaint(Color.WHITE);}return chart;}private static JFreeChart chartPiePlot(List<String> keyList, JFreeChart chart, int flag){PiePlot piePlot = (PiePlot)chart.getPlot();for(int i=0; i<keyList.size(); i++){piePlot.setSectionPaint(keyList.get(i), ColorEnum.zhColorList.get(i));}piePlot.setBackgroundPaint(Color.WHITE);piePlot.setShadowPaint(Color.WHITE);piePlot.setOutlinePaint(Color.WHITE);if(flag == 0){PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0}\n{2}", new DecimalFormat("0"), new DecimalFormat("0%") );piePlot.setLabelGenerator(labelGenerator);}else if(flag == 1){PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{2}", new DecimalFormat("0"), new DecimalFormat("0%") );piePlot.setLabelGenerator(labelGenerator);}else if(flag == 2){PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0}\n企业占比{1}%", new DecimalFormat("0"), new DecimalFormat("0%"));piePlot.setLabelGenerator(labelGenerator);}else{piePlot.setLabelGenerator(null);}Font font = new Font("宋体", Font.PLAIN, 12);piePlot.setLabelFont(font);piePlot.setIgnoreNullValues(true);piePlot.setLegendItemShape(new Rectangle(10, 10));piePlot.setLabelOutlinePaint(Color.WHITE);piePlot.setLabelShadowPaint(Color.WHITE);piePlot.setLabelBackgroundPaint(Color.WHITE);piePlot.setLabelLinkStroke(new BasicStroke(0.5F)); piePlot.setLabelLinkPaint(Color.lightGray);piePlot.setLabelGap(0.01);return chart;}
}package com.ruoyi.business.bankInterface.controller.risk.utils.chart;import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.business.bankInterface.ConstantConfig;
import com.ruoyi.business.bankInterface.controller.risk.utils.ColorEnum;
import org.apache.commons.lang3.StringUtils;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.PieChart;
import org.knowm.xchart.PieChartBuilder;
import org.knowm.xchart.PieSeries;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.style.PieStyler;
import org.knowm.xchart.style.Styler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;public class PieChar <C extends Chart<?, ?>> {private static final Logger logger = LoggerFactory.getLogger(PieChar.class);protected static String baseUrl = ConstantConfig.IMAGE_TEMP_FOLDER;public static void main(String[] args) throws IOException {List<String> titleList = Arrays.asList("无任何司法异常", "涉及刑事诉讼且为被告人", "涉重大经济纠纷", "涉及经济纠纷", "涉及劳动纠纷","近一年立案数量大于3", "当前存在执行公开信息", "新增查封信息", "失信被执行");List<Double> rDataList = JSONArray.parseArray("[40, 15, 1, 8, 17, 5, 10, 2, 2]", Double.class);String hImg = new PieChar().getChart("司法风险分布",titleList, rDataList, ColorEnum.zhColorList, false, Styler.LegendPosition.OutsideE, Styler.LegendLayout.Vertical);logger.info("环形图" + hImg);}public String getChart(String title, List<String> titleList, List<Double> rData, List<Color> colorList,Boolean legendVisible, Styler.LegendPosition legendPosition, Styler.LegendLayout legendLayout){File file = new File(baseUrl);if (!file.exists()){file.mkdirs();}try {String path = baseUrl + titleList.get(0)+ System.currentTimeMillis()+".png";PieChart chart = new PieChartBuilder().width(1200).height(450).theme(Styler.ChartTheme.Matlab).build();chart = chartTitle(chart, title);chart = legendVisible(chart, legendVisible, legendPosition, legendLayout);chart = chartLabels(chart, true);chart.getStyler().setSliceBorderWidth(0.3);chart.getStyler().setSliceBorderWidth(2);chart.getStyler().setDefaultSeriesRenderStyle(PieSeries.PieSeriesRenderStyle.Pie);chart.getStyler().setLabelType(PieStyler.LabelType.Percentage);chart.getStyler().setPlotContentSize(0.8);chart.getStyler().setSumVisible(false);chart.getStyler().setPlotBorderVisible(false);for(int i=0; i < titleList.size(); i++){PieSeries series =chart.addSeries(titleList.get(i), rData.get(i));series.setFillColor(colorList.get(i));
}BitmapEncoder.saveBitmap(chart, path, BitmapEncoder.BitmapFormat.PNG);return path;}catch (Exception e){e.printStackTrace();}return null;}private PieChart legendVisible(PieChart chart, Boolean legendVisible, Styler.LegendPosition legendPosition, Styler.LegendLayout legendLayout){
chart.getStyler().setLegendBorderColor(Color.white);chart.getStyler().setLegendVisible(false);if(legendVisible){chart.getStyler().setLegendVisible(true);chart.getStyler().setLegendPosition(legendPosition);chart.getStyler().setLegendLayout(legendLayout);chart.getStyler().setLegendFont(new Font("宋体", Font.PLAIN, 12));}return chart;}private PieChart chartTitle(PieChart chart, String title){if(StringUtils.isNotBlank(title)){chart.setTitle(title);chart.getStyler().setChartTitleFont(new Font("宋体", Font.BOLD, 25));chart.getStyler().setChartTitleBoxBackgroundColor(new Color(234,242,250));chart.getStyler().setChartTitleBoxVisible(true);chart.getStyler().setChartTitlePadding(15);}return chart;}private PieChart chartLabels(PieChart chart, Boolean labelsVisible){chart.getStyler().setLabelsDistance(0.8);chart.getStyler().setLabelsVisible(false);if(labelsVisible){chart.getStyler().setLabelsFont(new Font("宋体", Font.CENTER_BASELINE, 12));chart.getStyler().setLabelsFontColor(Color.black);chart.getStyler().setLabelsVisible(true);chart.getStyler().setLabelsFontColorAutomaticEnabled(true);chart.getStyler().setLabelsFontColorAutomaticLight(Color.black);chart.getStyler().setLabelsFontColorAutomaticDark(Color.orange);chart.getStyler().setForceAllLabelsVisible(true);}return chart;}
}