php 移动网站开发,怎么做直播网站超管,o2o是什么意思通俗讲,网站建设需求表模板这里写自定义目录标题 controller层pdf模板RiskReportTemplate1(页脚、水印)多线程导出pdf数据pdf导出工具类(页眉、图片底纹、柱图、饼图、表格、一级二级标题)实体类pdf工具类#xff08;根据html生成pdf文件#xff09;页脚工具类柱图工具类饼图工具类 controller层
Post… 这里写自定义目录标题 controller层pdf模板RiskReportTemplate1(页脚、水印)多线程导出pdf数据pdf导出工具类(页眉、图片底纹、柱图、饼图、表格、一级二级标题)实体类pdf工具类根据html生成pdf文件页脚工具类柱图工具类饼图工具类 controller层
PostMapping(/queryRiskReport1)public Object queryRiskReport1(RequestBody RiskBo riskBo)throws Exception {String corpKey riskBo.getCorpKey();String reportNo 18073201;String beginDateriskBo.getBeginDate();String endDateriskBo.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 tnew 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);}pdf模板RiskReportTemplate1(页脚、水印)
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 tnew 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(htmlheadmeta http-equiv\Content-Type\ content\text/html; charsetutf-8\ /title客户风险分析报告/title/head);template.append(bodydiv);//获取当前时间String curentDateStr DateUtils.getDateFormatter(new Date());//首页template.append(getFirstHtml(riskModel2.getK1().getQymc(), riskModel2.getK1().getTyshxydm(),curentDateStr, authDate, bankName, person, reportNo,beginDate,endDate));/** 1.企业概览 */template.append(ReportUtils2.title(企业概览, 1));/** 1.1.基本信息 */template.append(K1.getHtml(riskModel2.getK1()));/** 1.2.企业信用评分 */template.append(K1.score(riskModel2.getK1().getScore()));/** 2.公司治理 */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()));/** 3.1.经营风险 */template.append(K4.getHtml2(riskModel2.getK4_1List()));/** 3.1.1.经营资质 */template.append(K3.getHtml2(riskModel2.getK3_1List()));/** 3.1.2.运营风险 */template.append(K6.getHtml1(riskModel2.getK6List(),aBoolean));/** 3.1.3.司法风险 */template.append(K7.getHtml(riskModel2.getK7List(), riskModel2.getK7_1List(),aBoolean));/** 3.1.4.行政处罚 */template.append(K8.getHtml(riskModel2.getK8List(),aBoolean));/** 3.2.财务风险 */template.append(K4.getHtml3(riskModel2.getK4_2List()));/** 3.2.1.偿债能力 */template.append(K6.getHtml2(riskModel2.getK6_1List(), 偿债能力, 本风险监测时段中暂未监测到本企业存在异常。,aBoolean));/** 3.2.2.盈利能力 */template.append(K6.getHtml2(riskModel2.getK6_2List(), 盈利能力, 本风险监测时段中暂未监测到本企业存在异常。,aBoolean));/** 3.2.3.营运能力 */template.append(K6.getHtml2(riskModel2.getK6_3List(), 营运能力, 本风险监测时段中暂未监测到本企业存在异常。,aBoolean));/** 3.3.授用信风险 */template.append(ReportUtils2.title(授用信风险, 2));/** 3.3.1.履约风险 */template.append(K6.getHtml2(riskModel2.getK6_4List(), 履约风险, 本风险监测时段中暂未监测到本企业存在异常。,aBoolean));/** 3.3.2.过度授信风险 */template.append(K6.getHtml2(riskModel2.getK6_5List(), 过度授信风险, 本风险监测时段中暂未监测到本企业存在异常。,aBoolean));/** 3.3.3.企业非银融资 */template.append(K6.getHtml2(riskModel2.getK6_6List(), 企业非银融资, 本风险监测时段中暂未监测到本企业存在异常。,aBoolean));/** 3.3.4.其他授信异常 */template.append(K6.getHtml2(riskModel2.getK6_7List(), 其他授信异常, 本风险监测时段中暂未监测到本企业存在异常。,aBoolean));/** 3.4.关联风险 */template.append(ReportUtils2.title(关联风险, 2));/** 3.4.1.工商信息监测 */template.append(K6.getHtml2(riskModel2.getK6_8List(), 工商信息监测, 本风险监测时段中未监测到本企业存在企业关联人企业)发生破产清算注销风险。,aBoolean));/** 3.4.2.司法信息监测 */template.append(K9.getHtml(riskModel2.getK9List(),aBoolean));/** 3.4.3.履约风险征信 */template.append(K6.getHtml2(riskModel2.getK6_9List(), 履约风险征信, 本风险监测时段中未监测到本企业存在关联征信五级不良、关联企业在他行未结清业务发生逾期、关联企业非银机构授信异常风险。,aBoolean));/** 3.5.名单监测 */template.append(ReportUtils2.title(名单监测, 2));/** 3.5.1.黑名单 *///template.append(K10.getHtml(riskModel2.getK10List()));/** 3.6.系统性风险 */template.append(ReportUtils2.title(系统性风险, 2));/** 3.6.1.行业风险 */template.append(K6.getHtml3(riskModel2.getK6_10List(),aBoolean));template.append(/div);template.append(/body);template.append(/html);return template;}/*** 首页* param companyName 企业名称* param companyCode 统一社会信用代码* param time 报告生成日期如2021-01-01* param dataRetrievalCycle 数据周期如2021-01-01 至 2024-01-31* param bankName 金融机构名称* param person 查询人员* param reportNo 报告编号*/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(/divbr/);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(/divbr/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(/divbr/br/br/);return firstHtml;}/*** 生成页脚* param srcPdfPath 源文件* param tagetPdfPath 加工后文件*/public static void addPageNum(String srcPdfPath, String tagetPdfPath) {try {// 输出文件 流FileOutputStream fos new FileOutputStream(tagetPdfPath);// 读取 源PDF文件进行一页一页复制才能触发 添加页码的 页面监听事件PdfReader reader new PdfReader(srcPdfPath);// 获取 源文件总页数int num reader.getNumberOfPages();// 新建文档默认A4大小Document document new Document(PageSize.A4);PdfWriter writer PdfWriter.getInstance(document, fos);// 设置页面监听事件必须在open方法前writer.setPageEvent(new PdfNumPageEventTax(num));document.open();// PDF内容体PdfContentByte pdfContent writer.getDirectContent();//System.out.println(总页数 num);// 页面数是从1开始的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;//给pdf加水印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(i1 || itotal-1) continue;pageRect reader.getPageSizeWithRotation(i);under stamper.getOverContent(i);under.saveState();under.setGState(gs);under.beginText();under.setFontAndSize(base, 18);//控制水印字体大小// 水印文字成30度角倾斜// 获取每一页的高度、宽度float pageHeight pageRect.getHeight();float pageWidth pageRect.getWidth();// 根据纸张大小多次添加 水印文字成30度角倾斜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 - textW12, height -textH*2 , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, 查询机构:bankName, width - textW24, height -textH*3 , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, 苏州企业征信服务有限公司, width - textW36, 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();}}}// /** 文本内容 content文本内容fontSize字体大小color字体颜色backgroundColor背景色 */
// public static StringBuffer title(String content, int fontSize, 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;);
// description.append(line-height:).append(1.8).append(;);
// description.append(letter-spacing:).append(1.8).append(px;);
// description.append(\);
// description.append(content).append(/div);
// return description;
// }// /** 文本内容 content文本内容fontSize字体大小color字体颜色backgroundColor背景色 */
// public static StringBuffer description(String content, int fontSize, String color, String backgroundColor, int fontWeight){
// StringBuffer description new StringBuffer();description.append(br/);
// 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.8).append(;);
// description.append(letter-spacing:).append(1.8).append(px;);
// description.append(\);
// description.append(nbsp;nbsp;);
// description.append(content).append(/div);description.append(br/);
// return description;
// }
//
// /**
// * 加载图片柱状图/折线图
// */
// public static StringBuffer getImgOneHtml(String title, ListString xData, ListDouble yDataList, ListColor 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(ListString keyList, ListDouble 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;
// }
}多线程导出pdf数据
public RiskModel2 queryRiskReport1(String corpKey,String beginDate,String endDate){RiskModel2 riskModel2 new RiskModel2();/** 1.基本信息 */CompletableFutureK1 k1 CompletableFuture.supplyAsync(() -{K1 k_1 riskReportMapper.queryK1(corpKey);k_1.setTyshxydm(corpKey);return k_1;}, creditReportExecutor);/** 2.公司治理 */CompletableFutureListK3 k3List CompletableFuture.supplyAsync(() -{ListK3 k_3List riskReportMapper.queryK3List(corpKey, Arrays.asList(R001,R002,R003),beginDate,endDate);return k_3List;}, creditReportExecutor);/** 3.风险信息:1级风险类型分布 */CompletableFutureListK4 k4List CompletableFuture.supplyAsync(() -{ListK4 k_4List riskReportMapper.queryK4List(corpKey,beginDate,endDate);return k_4List;}, creditReportExecutor);/** 3.风险信息:1级风险类型分布 */CompletableFutureListK5 k5List CompletableFuture.supplyAsync(() -{ListK5 k_5List riskReportMapper.queryK5List(corpKey,beginDate,endDate);return k_5List;}, creditReportExecutor);/** 3.1.经营风险 */CompletableFutureListK4 k4_1List CompletableFuture.supplyAsync(() -{ListK4 k_4List riskReportMapper.queryK4_1List(corpKey, 经营风险,beginDate,endDate);return k_4List;}, creditReportExecutor);/** 3.1.1.经营资质 */CompletableFutureListK3 k3_1List CompletableFuture.supplyAsync(() -{ListK3 k_3List riskReportMapper.queryK3List(corpKey, Arrays.asList(R004,R005),beginDate,endDate);return k_3List;}, creditReportExecutor);/** 3.1.2.运营风险 */CompletableFutureListK6 k6List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 运营风险,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.1.3.司法风险 */CompletableFutureListK7 k7_1List CompletableFuture.supplyAsync(() -{ListK7 k_7List riskReportMapper.queryK7_1List(corpKey, 司法风险,beginDate,endDate);return k_7List;}, creditReportExecutor);CompletableFutureListK7 k7List CompletableFuture.supplyAsync(() -{ListK7 k_7List riskReportMapper.queryK7List(corpKey, 司法风险,beginDate,endDate);return k_7List;}, creditReportExecutor);/** 3.1.4.行政处罚 */CompletableFutureListK8 k8List CompletableFuture.supplyAsync(() -{ListK8 k_8List riskReportMapper.queryK8List(corpKey, 行政处罚,beginDate,endDate);return k_8List;}, creditReportExecutor);/** 3.2 财务风险 */CompletableFutureListK4 k4_2List CompletableFuture.supplyAsync(() -{ListK4 k_4List riskReportMapper.queryK4_1List(corpKey, 财务风险,beginDate,endDate);return k_4List;}, creditReportExecutor);/** 3.2.1 偿债能力 */CompletableFutureListK6 k6_1List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 偿债能力,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.2.2盈利能力 */CompletableFutureListK6 k6_2List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 盈利能力,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.2.3营运能力 */CompletableFutureListK6 k6_3List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 营运能力,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.1履约风险 */CompletableFutureListK6 k6_4List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 履约风险,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.2 过度授信风险 */CompletableFutureListK6 k6_5List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 过度授信风险,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.3 企业非银融资 */CompletableFutureListK6 k6_6List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 企业非银融资,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.3.4 其他授信异常 */CompletableFutureListK6 k6_7List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 其他授信异常,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.4.1 工商信息监测(暂无数据) *//** 3.4.2 司法信息监测 */CompletableFutureListK9 k9List CompletableFuture.supplyAsync(() -{ListK9 k9_1List riskReportMapper.queryK9List(corpKey, 司法信息监测,beginDate,endDate);return k9_1List;}, creditReportExecutor);/** 3.4.3 履约风险征信 */CompletableFutureListK6 k6_9List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 履约风险征信,beginDate,endDate);return k_6List;}, creditReportExecutor);/** 3.5.1 黑名单 */CompletableFutureListK10 k10List CompletableFuture.supplyAsync(() -{ListK10 k10_1List riskReportMapper.queryK10List(corpKey, 黑名单,beginDate,endDate);return k10_1List;}, creditReportExecutor);/** 3.6 系统性风险 *//** 3.6 系统性风险 */CompletableFutureListK6 k6_10List CompletableFuture.supplyAsync(() -{ListK6 k_6List riskReportMapper.queryK6List(corpKey, 行业风险,beginDate,endDate);return k_6List;}, creditReportExecutor);CompletableFutureVoid 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导出工具类(页眉、图片底纹、柱图、饼图、表格、一级二级标题)
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;MapString, Object data null;if(dataType 1){/** 数据类型是list */data ReportUtils2.changeObjectList(clazz, jsonStr);}else{/** 数据类型是对象 */data ReportUtils2.changeObject(clazz, jsonStr);}if(data null || data.size() 0) return template;ListReportField reportFieldList (ListReportField)data.get(nameList);ListMapString,Object valueList (ListMapString,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;}//jsonArrStr支持的对象中可以有key:value和最多两个ListString且其中一个ListString为yearListpublic static MapString, Object changeObjectList(Class? clazz, String jsonArrStr) {// 获取带有特定注解的字段SetField annotatedFields getAnnotatedFields(clazz, ReportFieldAnnotation.class);ListReportField reportFieldList new ArrayList();ListJSONObject objectList JSONArray.parseArray(jsonArrStr, JSONObject.class);ListMapString, String valueList new ArrayList(); MapString, 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();//0:字符串1:ListStringString 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());value.put(fieldName, Objects.isNull(vObj) ? : vObj.toString().replaceAll(, ).replaceAll(, ));}else if(dataType 1){//ListString类型的时候字段名取yearList的值if(yearList.equals(field.getName())) continue;Object yearListObj jsonObject.get(yearList);if(Objects.isNull(yearListObj) || Objects.isNull(vObj)) continue;ListString yearList JSONArray.parseArray(JSONObject.toJSONString(yearListObj), String.class);ListString vList JSONArray.parseArray(JSONObject.toJSONString(vObj), String.class);for(int i0; iyearList.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); });MapString, Object res new HashMap();res.put(nameList, reportFieldList);res.put(valueList, valueList);return res;}//jsonStr只支持key:valuepublic static MapString, Object changeObject(Class? clazz, String jsonStr) {//获取带有特定注解的字段SetField annotatedFields getAnnotatedFields(clazz, ReportFieldAnnotation.class);ListReportField reportFieldList new ArrayList();JSONObject jsonObject JSONObject.parseObject(jsonStr);ListMapString, String valueList new ArrayList(); MapString, String value new HashMap();for (Field field : annotatedFields) {ReportField reportField new ReportField();ReportFieldAnnotation annotation field.getAnnotation(ReportFieldAnnotation.class);int dataType annotation.dataType();//0:字符串1:ListStringString 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){//ListString类型的时候字段名取yearList的值Object yearListObj jsonObject.get(yearList);if(Objects.isNull(yearListObj) || Objects.isNull(vObj)) continue;ListString yearList JSONArray.parseArray(JSONObject.toJSONString(yearListObj), String.class);ListString vList JSONArray.parseArray(JSONObject.toJSONString(vObj), String.class);for(int i0; iyearList.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);MapString, 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 SetField getAnnotatedFields(Class? clazz, Class? extends Annotation annotation) {SetField 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;}/** 个性化数据-列表数据展示 reportFieldList:标题行 value:标题行对应的字段对应的值 */public static StringBuffer listHtml(ListReportField reportFieldList, ListMapString,Object value){ListString keyList new ArrayList();//存放标题行对应的字段StringBuffer tempHtml new StringBuffer();tempHtml.append(div class\table_box_b\);tempHtml.append(table width\100%\ cellspacing\0\);tempHtml.append(tbody);/** -----------------------------标题列start----------------------------------- */tempHtml.append(tr); int j1; int size reportFieldList.size();for(ReportField reportField : reportFieldList){if(jsize){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(j1){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);/** -----------------------------标题列end----------------------------------- *//** -----------------------------数据列start----------------------------------- */if(value ! null value.size() 0){for(MapString, Object vMap : value) {tempHtml.append(tr); int keySize keyList.size(); int flag 1;for (String key : keyList) {if(keySizeflag){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);}}/** -----------------------------数据列end----------------------------------- */tempHtml.append(/tbody/table/div);return tempHtml;}/** 个性化数据-横着两列标题排列 reportFieldList:标题行 value:标题行对应的字段对应的值 */public static StringBuffer twoTitleHtml(ListReportField reportFieldList, ListMapString,Object value){StringBuffer tempHtml new StringBuffer();for(MapString, 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;}/** 个性化数据-横着单列标题排列 reportFieldList:标题行 value:标题行对应的字段对应的值 */public static StringBuffer oneTitleHtml(ListReportField reportFieldList, ListMapString,Object value){StringBuffer tempHtml new StringBuffer();for(MapString, 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;}/** 文本内容 content文本内容level标题等级 */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(\/thinsp; span style\font-weight: 900;color:#005BAB;\).append(content).append(/span);description.append(/div);}return description;}/** 文本内容 content文本内容fontSize字体大小color字体颜色backgroundColor背景色 */public static StringBuffer description(String content, int fontSize, String color, String backgroundColor, int fontWeight){StringBuffer description new StringBuffer();
// description.append(br/);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(nbsp;nbsp;);description.append(content).append(/div);
// description.append(br/);return description;}/*** 加载图片柱状图/折线图*/public static StringBuffer getImgOneHtml(String title, ListString xData, ListDouble yDataList, ListColor 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(ListString keyList, ListDouble 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;}/*** 加图片做pdf首页的底纹* param srcPdfPath 源文件* param tagetPdfPath 加工后文件* param pageNum 页码* param type 底纹类型0客户风险分析报告1客群风险分析报告*/public static void addImageWatermark(String srcPdfPath, String tagetPdfPath, int pageNum, int type) {try {// 加载PDF文件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 (type0 ? 客户风险分析报告 : 客群风险分析报告) .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);// 获取原始PDF内容并添加到新的PDF中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);}// 关闭PDF文件stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();}}/*** 加图片做pdf页眉* param pdfFilePath 源文件* param toFdfPath 加工后文件*/public static void pdf_yemei(String companyName, String pdfFilePath, String toFdfPath) {try {String NoFirstYeMeiImage baseUrl companyName 合并后图片.png;getImageAndChar2(ConstantConfig.RISK_IMAGE 非首页-页眉.jpg, NoFirstYeMeiImage, companyName);// 加载PDF文件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);// 获取原始PDF内容并添加到新的PDF中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);}}// 关闭PDF文件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其宽度为两张图片的宽度之和高度为两张图片中较高的那张图片的高度 */BufferedImage mergedImage new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());/** 创建Graphics2D对象以在新的合并图像上绘图 */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();}}/** 1级标题专用 -- 合并文字和图片生成一个新的图片 */public static String forTitle1(String content){String toImagePath baseUrl System.currentTimeMillis();try {// 读取第一张图片BufferedImage image1 ImageIO.read(new File(ConstantConfig.RISK_IMAGE 一级标题.jpg));/** 创建一个新的BufferedImage其宽度为两张图片的宽度之和高度为两张图片中较高的那张图片的高度 */BufferedImage mergedImage new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());/** 创建Graphics2D对象以在新的合并图像上绘图 */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;}/** 2级标题专用 -- 合并文字和图片生成一个新的图片 */public static String forTitle2(String content){String toImagePath baseUrl System.currentTimeMillis();try {// 读取第一张图片BufferedImage image1 ImageIO.read(new File(ConstantConfig.RISK_IMAGE 二级标题.jpg));/** 创建一个新的BufferedImage其宽度为两张图片的宽度之和高度为两张图片中较高的那张图片的高度 */BufferedImage mergedImage new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());/** 创建Graphics2D对象以在新的合并图像上绘图 */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;}}
实体类
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(end1);}}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(end1);}}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(ListK3 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(ListK3 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(
// 本风险监测时段中监测到企业存在k3List.size()条经营资质风险其中企业工商信息变更风险
// k3List.stream().filter(item -R004.equals(item.getFxxl())).count()条企业经营期限到期或状态为‘非存续’风险
// k3List.stream().filter(item -R005.equals(item.getFxxl())).count()条。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 ListK3 changeK3List(ListK3 k3List){if(k3List null || k3List.size() 0) return k3List;ListK3 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;}}
pdf工具类根据html生成pdf文件
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/home/cc/data/images/;public static final String imgPrefixUrlD:\\test\\images\\;private static final Logger logger LoggerFactory.getLogger(PdfUtils.class);//根据html生成pdf文件
// public static void htmp2pdf(String creditName, String pdfName,String template){
// Document document null;
// PdfWriter pdfWriter null;
// try {
// document new Documapi/v1/financial/queryFinancialDataent(PageSize.A4);
// pdfWriter PdfWriter.getInstance(document, new FileOutputStream(pdfName));
// document.open();
// document.addAuthor(Soren);
// document.addCreator(Soren);
// document.addCreationDate();
// document.addTitle(测试生成pdf);
// XMLWorkerHelper helper XMLWorkerHelper.getInstance();
// //读取模板文件并且写入String中
// InputStream in new FileInputStream(template);
// InputStreamReader isr new InputStreamReader(in, utf-8);
// BufferedReader brd new BufferedReader(isr);
// StringBuffer sbf new StringBuffer();
// String line ;
// while((linebrd.readLine())!null) {
// sbf.append(line);
// }
// String htmlTemplete sbf.toString();
//
// //此处替换特定字符或者拼接html代码的处理
// htmlTemplete htmlTemplete.replaceAll(###TITLE###, 长三角征信链企业征信报告);
// htmlTemplete htmlTemplete.replaceAll(###INTRODUCE###, 该报告来源:creditName);
// htmlTemplete htmlTemplete.replaceAll(###date###, 日期DateUtils.convertYMD(new Date()));htmlTemplete htmlTemplete.replaceAll(###date###, 日期UUID.randomUUID().toString());
// //模板不支持svg格式图片
// helper.parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlTemplete.getBytes(UTF-8)), Charset.forName(utf8), new AsianFontProvider());
// logger.info(根据html生成pdf文件完成!);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (DocumentException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }finally {
// if(document!null)
// //如果不关闭则会生成空的破损的pdf
// document.close();
// }
// return;
// }//根据html生成pdf文件
// public static void htmppdf(String htmlStr, String pdfName, Boolean isTrue){
// Document document null;
// PdfWriter pdfWriter null;
// try {
// document new Document(PageSize.A4);
// pdfWriter PdfWriter.getInstance(document, new FileOutputStream(pdfName));
// document.open();
// document.addAuthor(Soren);
// document.addCreator(Soren);
// document.addCreationDate();
// document.addTitle(测试生成pdf);
// XMLWorkerHelper helper XMLWorkerHelper.getInstance();
// //模板不支持svg格式图片
// helper.parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlStr.getBytes(UTF-8)), Charset.forName(utf8), new AsianFontProvider());
// logger.info(根据html生成pdf文件完成!);
// if(isTrue){
// //先把文件名字更换为临时名字
// String temName pdfName.substring(0, pdfName.lastIndexOf(.)) temp pdfName.substring(pdfName.lastIndexOf(.));//临时文件名
// renameFile(pdfName, temName);
// //文件加水印,输出原文件名
// waterMark(temName,
// pdfName);
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (DocumentException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }finally {
// if(document!null)
// //如果不关闭则会生成空的破损的pdf
// document.close();
// }
// return;
// }/** 合并pdf文件* param files 要合并文件数组(绝对路径如{ e:\\1.pdf, e:\\2.pdf ,* e:\\3.pdf}),合并的顺序按照数组中的先后顺序如2.pdf合并在1.pdf后。* param newfile 合并后新产生的文件绝对路径如 e:\\temp\\tempNew.pdf,* return boolean 合并成功返回true否则返回false**/public static boolean mergePdfFiles(ListString 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;}/** pdf文件是否加密授权加密文件不可更改。加一层水印如果加成功表示不是加密文件*/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(oFile123));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);//控制水印字体大小// 水印文字成30度角倾斜//你可以随心所欲的改你自己想要的角度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;}/**文件转换为字节数组* param file* return*/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) {// 将指定字节数组中从偏移量 off 开始的 len 个字节写入此字节数组输出流。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;}/**根据byte[] 数组生成文件 在本地* param bfile 字节数组* param filePath 文件路径*/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 oldfilenew File(oldname);File newfilenew 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;//给pdf加水印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);//控制水印字体大小// 水印文字成30度角倾斜// 获取每一页的高度、宽度float pageHeight pageRect.getHeight();float pageWidth pageRect.getWidth();// 根据纸张大小多次添加 水印文字成30度角倾斜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 - textW12, height -textH*2 , 30);under.setFontAndSize(base, 12);//控制水印字体大小under.showTextAligned(Element.ALIGN_LEFT, time, width - textW24, 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) {
// renameFile(D:\\test\\123.pdf,D:\\test\\模板.pdf);//根据html生成pdf
// PdfUtils.htmp2pdf(苏信, D:\\test\\1234.pdf,D:\\test\\template(3).html);
// PdfUtils.htmp2pdf(br/苏州征信、江苏省南京分融服务信用平台、安徽征信, D:\\test\\123.pdf,D:\\test\\first.html);
// D:\test\template(1).html//合并pdf文件
// ListString files new ArrayList();
// files.add(D:\\test\\123.pdf);
// files.add(D:\\test\\1234.pdf);
// String savepath D:\\test\\xiaomingnew.pdf;
// Boolean bool mergePdfFiles(files, savepath);
// //给pdf加水印
// waterMark(D:\\test\\xiaomingnew.pdf, D:\\test\\666.pdf, xoads, 2021);
// String pdfName /d:cdc.cdsc.csdcs;
// String begin pdfName.substring(0, pdfName.lastIndexOf(.));
// String endStr pdfName.substring(pdfName.lastIndexOf(.));
// System.out.println(beginbegin);System.out.println(endStr xxx.pdf.substring(0, xxx.pdf.length() - 4));// try {
// //合并pdf文件
// Boolean bool PdfUtils.mergePdfFiles(Arrays.asList(D:\\test\\666.pdf,D:\\test\\666.pdf), D:\\test\\777.pdf);
// }catch (Exception e){
// }}//解压文件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();//得到zip包里的所有元素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);//以ZipEntry为参数得到一个InputStream并写到OutputStream中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]);// 根据 ret 抽象路径名和 child 路径名字符串创建一个新 File 实例return ret;}//根据html生成pdf文件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(imgPrefixUrlbackground.jpg);/* 设置图片的位置 */image.setAbsolutePosition(0, 80);/* 设置图片的大小 */image.scaleAbsolute(595, 842);document.add(image);}//此处替换特定字符或者拼接html代码的处理htmlTemplate htmlTemplate.replaceAll(###TITLE###, 长三角报告);htmlTemplate htmlTemplate.replaceAll(###INTRODUCE###, 该报告来源:creditName);htmlTemplate htmlTemplate.replaceAll(###date###, 日期 DateUtils.convertYMD(new Date()));XMLWorkerHelper helper XMLWorkerHelper.getInstance();//模板不支持svg格式图片byteArrayInputStream new ByteArrayInputStream(htmlTemplate.getBytes(UTF-8));helper.parseXHtml(pdfWriter, document, byteArrayInputStream, Charset.forName(utf8), new AsianFontProvider());logger.info(pdf文件转换完成!);
// if(isTrue){
// //先把文件名字更换为临时名字
// String begin pdfName.substring(0, pdfName.lastIndexOf(.));
// String endStr pdfName.substring(pdfName.lastIndexOf(.));
// String temName begin temp endStr;
// renameFile(pdfName, temName);
// //文件加水印,输出原文件名
// waterMark(temName,
// pdfName);
// }} 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());}}}/*** 删除单个文件** param fileName* 要删除的文件的文件名* return 单个文件删除成功返回true否则返回false*/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;}}
}页脚工具类
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.*;/*** description 针对页码生成的PDF事件监听* author myw* date 2023-4-10 20:24:21**/
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;// PDF文档内容PdfContentByte pdfContent writer.getDirectContent();pdfContent.saveState();pdfContent.beginText();int footerFontSize 10;// 解决页码中文无法显示 或者 显示为乱码的问题// 但是必须引入jar包 itext-asian-5.2.0.jarBaseFont 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) {
// try {
// // PDF文档内容
// PdfContentByte pdfContent writer.getDirectContent();
// pdfContent.saveState();
// pdfContent.beginText();
// int footerFontSize 10;
// // 解决页码中文无法显示 或者 显示为乱码的问题
// // 但是必须引入jar包 itext-asian-5.2.0.jar
// 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();
// }super.onStartPage(writer, document);}
}
柱图工具类
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 {ListString titleList JSONArray.parseArray([\严重警告\, \警告\, \关注\, \提示\], String.class);ListDouble 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, ListString titleList, ListString xData, ListListDouble yDataList, ListColor 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);//设置图例Legend的位置chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);//设置图例Legend的排列chart.getStyler().setXAxisLabelRotation(0);//设置X轴的字旋转角度if(xData ! null xData.size() 6){chart.getStyler().setXAxisLabelRotation(40);//设置X轴的字旋转角度}chart.setYAxisGroupTitle(0, yUnit);//设置Y轴组的标题chart.getStyler().setYAxisGroupPosition(0, Styler.YAxisPosition.Left);//设置Y轴组的显示位置// 设置字体大小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(#);//设置Y轴的小数点格式//避免科学计数法chart.getStyler().setChartPadding(10);//设置图表的内边距chart.getStyler().setPlotBorderVisible(true);//设置图表中绘图区域的边框是否可见
// chart.getStyler().setYAxisMin(0.0);chart.getStyler().setAxisTickMarksColor(Color.white);//设置图表中刻度标记的颜色//柱状图if(type 0 titleList ! null titleList.size() 0){chart.getStyler().setOverlapped(false);//设置图表中数据系列的重叠方式for(int i0; 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 i0; 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;}/**** param title* param xDataList X轴数据列表/线名字列表* param yDataList Y轴数据列表* param colorList 颜色列表* param yUnit Y轴单位名字* param type 图形类型0:柱状图1:折线图* param width 图片宽度* param height 图片高度* param SpaceFill* return*/public String getChart2(String title, ListString xDataList, ListDouble yDataList, ListColor 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 i0; i xDataList.size(); i){ListDouble yData new ArrayList();for(int y0; y yDataList.size(); y){if(yi){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);//设置图例Legend的位置chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);//设置图例Legend的排列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;}/** X轴相关配置 */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));//设置坐标轴的字体样式/** X轴相关配置 */chart.getStyler().setXAxisLabelRotation(0);//设置X轴的字旋转角度if(isRotation){chart.getStyler().setXAxisLabelRotation(40);//设置X轴的字旋转角度}/** Y轴相关配置 */chart.setYAxisGroupTitle(0, yUnit);//设置Y轴组的标题chart.getStyler().setYAxisGroupPosition(0, Styler.YAxisPosition.Left);//设置Y轴组的显示位置chart.getStyler().setYAxisDecimalPattern(#);//设置Y轴的小数点格式//避免科学计数法
// chart.getStyler().setYAxisMin(0.6);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.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{ListString keyList Arrays.asList(无任何司法异常, 涉及刑事诉讼且为被告人, 涉重大经济纠纷, 涉及经济纠纷, 涉及劳动纠纷,近一年立案数量大于3, 当前存在执行公开信息, 新增查封信息, 失信被执行);ListDouble 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(ListString keyList, ListDouble rDataList, Boolean isTitleVisible, String title,Boolean isLegendVisible, RectangleEdge rectangleEdge, int flag){String path baseUrl System.currentTimeMillis().png;try {// 创建数据集DefaultPieDataset dataset new DefaultPieDataset();for(int i0; ikeyList.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,中文将显示不出来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;}/** PiePlot配置 */private static JFreeChart chartPiePlot(ListString keyList, JFreeChart chart, int flag){PiePlot piePlot (PiePlot)chart.getPlot();for(int i0; ikeyList.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){/** 展示key百分比 */PieSectionLabelGenerator labelGenerator new StandardPieSectionLabelGenerator({0}\n{2}, // 标签格式其中 {0} 是扇区键{1} 是扇区值{2} 是百分比new DecimalFormat(0), // 用于格式化数值的 DecimalFormat 对象new DecimalFormat(0%) // 用于格式化百分比的 DecimalFormat 对象);piePlot.setLabelGenerator(labelGenerator);}else if(flag 1){/** 展示百分比 */PieSectionLabelGenerator labelGenerator new StandardPieSectionLabelGenerator({2}, // 标签格式其中 {0} 是扇区键{1} 是扇区值{2} 是百分比new DecimalFormat(0), // 用于格式化数值的 DecimalFormat 对象new DecimalFormat(0%) // 用于格式化百分比的 DecimalFormat 对象);piePlot.setLabelGenerator(labelGenerator);}else if(flag 2){/** 展示百分比 */PieSectionLabelGenerator labelGenerator new StandardPieSectionLabelGenerator({0}\n企业占比{1}%, // 标签格式其中 {0} 是扇区键{1} 是扇区值{2} 是百分比new DecimalFormat(0), // 用于格式化数值的 DecimalFormat 对象new DecimalFormat(0%)// 用于格式化数值的 DecimalFormat 对象);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 {ListString titleList Arrays.asList(无任何司法异常, 涉及刑事诉讼且为被告人, 涉重大经济纠纷, 涉及经济纠纷, 涉及劳动纠纷,近一年立案数量大于3, 当前存在执行公开信息, 新增查封信息, 失信被执行);ListDouble 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);}/*** 饼状图/环图* param title* param titleList 模块名* param rData 数据量* param colorList 模块的颜色集合* param legendVisible 图例是否可见true:可见false:不可见* param legendPosition 设置图例Legend的位置 OutsideE左侧 OutsideS* param legendLayout 设置图例Legend的排列 Vertical竖着排列 Horizontal横着排列* return*/public String getChart(String title, ListString titleList, ListDouble rData, ListColor 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);//设置饼图中每个扇区Slice的边框宽度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 i0; i titleList.size(); i){PieSeries series chart.addSeries(titleList.get(i), rData.get(i));series.setFillColor(colorList.get(i));
// series.setShowInLegend(true);}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().setLegendPadding(100);//设置图例Legend的内边距
// chart.getStyler().setLegendSeriesLineLength(200);//设置图例系列线条的长度chart.getStyler().setLegendBorderColor(Color.white);//设置图例边框颜色:白色chart.getStyler().setLegendVisible(false);//默认图例不可见if(legendVisible){chart.getStyler().setLegendVisible(true);//图例可见chart.getStyler().setLegendPosition(legendPosition);//设置图例Legend的位置chart.getStyler().setLegendLayout(legendLayout);//设置图例Legend的排列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);//设置饼图中每个扇区Slice的标签是否可见if(labelsVisible){chart.getStyler().setLabelsFont(new Font(宋体, Font.CENTER_BASELINE, 12));chart.getStyler().setLabelsFontColor(Color.black);chart.getStyler().setLabelsVisible(true);//设置饼图中每个扇区Slice的标签是否可见chart.getStyler().setLabelsFontColorAutomaticEnabled(true);//设置成false的时候有个白色的图列字变成了指定的颜色chart.getStyler().setLabelsFontColorAutomaticLight(Color.black);chart.getStyler().setLabelsFontColorAutomaticDark(Color.orange);chart.getStyler().setForceAllLabelsVisible(true);//控制饼图中所有扇区的标签是否必须可见}return chart;}
}