|
@@ -0,0 +1,295 @@
|
|
|
+package com.om.utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.itextpdf.io.font.PdfEncodings;
|
|
|
+import com.itextpdf.kernel.colors.Color;
|
|
|
+import com.itextpdf.kernel.colors.DeviceRgb;
|
|
|
+import com.itextpdf.kernel.font.PdfFont;
|
|
|
+import com.itextpdf.kernel.font.PdfFontFactory;
|
|
|
+import com.itextpdf.kernel.geom.PageSize;
|
|
|
+import com.itextpdf.kernel.pdf.PdfDocument;
|
|
|
+import com.itextpdf.kernel.pdf.PdfReader;
|
|
|
+import com.itextpdf.kernel.pdf.PdfWriter;
|
|
|
+import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
|
|
|
+import com.itextpdf.layout.Document;
|
|
|
+import com.itextpdf.layout.Style;
|
|
|
+import com.itextpdf.layout.borders.Border;
|
|
|
+import com.itextpdf.layout.element.*;
|
|
|
+import com.itextpdf.layout.property.BorderRadius;
|
|
|
+import com.itextpdf.layout.property.HorizontalAlignment;
|
|
|
+import com.itextpdf.layout.property.TextAlignment;
|
|
|
+import com.om.entity.dto.ControlListMap;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * itextpdf 工具类
|
|
|
+ * iText 7是iText强大的PDF工具包的最新版本,用于PDF生成,PDF编程,处理和操作,如数字签名等。
|
|
|
+ * https://kb.itextpdf.com/home/it7kb/ebooks
|
|
|
+ */
|
|
|
+public class ITextPdfUtil {
|
|
|
+ //字体,我这里使用系统自带的simhei黑体
|
|
|
+ private static final String FONT = "C:/Windows/Fonts/simhei.ttf";
|
|
|
+
|
|
|
+
|
|
|
+ //生成简单PDF
|
|
|
+ public static File MapToPDF(Integer id,List<ControlListMap> controlListMap
|
|
|
+ , Map<String,String> baseMap, Map<String,String> companyMap ) {
|
|
|
+
|
|
|
+ String yyyyMMdd = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
+ try (PdfWriter writer = new PdfWriter("src\\main\\resources\\template\\outpdf\\"+yyyyMMdd+"report"+id+".pdf");
|
|
|
+ //FileOutputStream out = new FileOutputStream(new File("src\\main\\resources\\outpdf\\report.pdf"))
|
|
|
+ PdfDocument pdf = new PdfDocument(writer);
|
|
|
+ Document document = new Document(pdf, PageSize.A6);
|
|
|
+
|
|
|
+ ) {
|
|
|
+ DeviceRgb red = new DeviceRgb(255, 59, 48);
|
|
|
+ DeviceRgb black = new DeviceRgb( 34, 34, 34);
|
|
|
+ //设置文档属性
|
|
|
+ pdf.getDocumentInfo().setAuthor("bmmx");
|
|
|
+ pdf.getDocumentInfo().setTitle("IText测试PDF");
|
|
|
+ pdf.getDocumentInfo().setSubject("XXX公司");
|
|
|
+ pdf.getDocumentInfo().setMoreInfo("1", "111");
|
|
|
+ pdf.getDocumentInfo().setCreator("huanzi");
|
|
|
+ pdf.getDocumentInfo().setKeywords("IText");
|
|
|
+
|
|
|
+ //设置字体
|
|
|
+ document.setFont(ITextPdfUtil.getPdfFont());
|
|
|
+
|
|
|
+ //页边距
|
|
|
+ document.setMargins(10, 10, 10, 10);
|
|
|
+
|
|
|
+ //诊断报告 + 时间
|
|
|
+ document.add(new Div()
|
|
|
+ .add(new Paragraph("诊断报告")
|
|
|
+ .addStyle(new Style()
|
|
|
+ .setBold()))
|
|
|
+ .add(new Paragraph("— " + LocalDateTime.now()
|
|
|
+ .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " —"))
|
|
|
+ .addStyle(new Style()
|
|
|
+ .setFontColor(new DeviceRgb(54, 85, 153)))
|
|
|
+ .setBackgroundColor(new DeviceRgb(235, 241, 250))
|
|
|
+ .setTextAlignment(TextAlignment.CENTER));
|
|
|
+ DeviceRgb c1 = new DeviceRgb(136, 136, 136);
|
|
|
+ DeviceRgb c2 = new DeviceRgb(40, 40, 40);
|
|
|
+ Table baseTable = new Table(2);
|
|
|
+ mapToTable(baseMap,baseTable,c1,c2);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 车辆信息
|
|
|
+ document.add(new Div()
|
|
|
+ .setMarginTop(10)
|
|
|
+ .setHorizontalAlignment(HorizontalAlignment.CENTER)
|
|
|
+ .setBackgroundColor(new DeviceRgb(235, 241, 250))
|
|
|
+ .setBorderRadius(new BorderRadius(5))
|
|
|
+
|
|
|
+ .add(new Div()
|
|
|
+ .setBackgroundColor(new DeviceRgb( 64,109,183))
|
|
|
+ .setBorderBottomLeftRadius(new BorderRadius(8))
|
|
|
+ .setBorderBottomRightRadius(new BorderRadius(8))
|
|
|
+ .setWidth(100)
|
|
|
+ .setHorizontalAlignment(HorizontalAlignment.CENTER)
|
|
|
+ .add(new Paragraph("车辆信息")
|
|
|
+ .setTextAlignment(TextAlignment.CENTER)
|
|
|
+ .addStyle(new Style()
|
|
|
+ .setFontColor(new DeviceRgb(255,255,255))
|
|
|
+ .setBold()
|
|
|
+ )))
|
|
|
+ .add(baseTable));
|
|
|
+
|
|
|
+ SolidLine solidLine = new SolidLine(0.2f);
|
|
|
+ solidLine.setColor(new DeviceRgb(64,109,183));
|
|
|
+ //solidLine.setLineWidth(260);
|
|
|
+
|
|
|
+ for (ControlListMap listMap : controlListMap) {
|
|
|
+ String name = listMap.getName();
|
|
|
+ Map<String, String> versionMap = listMap.getVersionMap();
|
|
|
+ Map<String, String> faultCodeMap = listMap.getFaultCodeMap();
|
|
|
+
|
|
|
+ Table faultCodeTable = new Table(2);
|
|
|
+
|
|
|
+ mapToTable(faultCodeMap,faultCodeTable,red,red);
|
|
|
+
|
|
|
+ Table versionTable = new Table(2);
|
|
|
+
|
|
|
+ mapToTable(versionMap,versionTable,black,black);
|
|
|
+ // 车辆信息
|
|
|
+ document.add(new Div()
|
|
|
+ .setMarginTop(10)
|
|
|
+ .setHorizontalAlignment(HorizontalAlignment.CENTER)
|
|
|
+ .setBackgroundColor(new DeviceRgb(235, 241, 250))
|
|
|
+ .setBorderRadius(new BorderRadius(5))
|
|
|
+
|
|
|
+ .add(new Div()
|
|
|
+ .setBackgroundColor(new DeviceRgb( 64,109,183))
|
|
|
+ .setBorderBottomLeftRadius(new BorderRadius(8))
|
|
|
+ .setBorderBottomRightRadius(new BorderRadius(8))
|
|
|
+ .setWidth(100)
|
|
|
+ .setHorizontalAlignment(HorizontalAlignment.CENTER)
|
|
|
+ .add(new Paragraph("诊断详情")
|
|
|
+ .setTextAlignment(TextAlignment.CENTER)
|
|
|
+ .addStyle(new Style()
|
|
|
+ .setFontColor(new DeviceRgb(255,255,255))
|
|
|
+ .setBold()
|
|
|
+ )))
|
|
|
+ .add(new Div()
|
|
|
+ .setBackgroundColor(new DeviceRgb( 64,109,183))
|
|
|
+ .setMarginTop(20)
|
|
|
+ .setBorderTopRightRadius(new BorderRadius(5))
|
|
|
+ .setMaxWidth(150)
|
|
|
+ .setMaxHeight(30)
|
|
|
+ .add(new Paragraph(name)
|
|
|
+ .setFontColor(new DeviceRgb(255,255,255))
|
|
|
+ .setFontSize(10)
|
|
|
+ .setTextAlignment(TextAlignment.CENTER)
|
|
|
+ )
|
|
|
+ )
|
|
|
+ .add(new LineSeparator(solidLine).setMarginRight(10))
|
|
|
+ .add(new Paragraph("故障码("+faultCodeMap.size()+")")
|
|
|
+ .setFontColor(new DeviceRgb(153,153,153))
|
|
|
+ .setFontSize(10)
|
|
|
+ .setMarginLeft(10)
|
|
|
+ .setMarginTop(10)
|
|
|
+ )
|
|
|
+ .add(faultCodeTable).setFontSize(10)
|
|
|
+
|
|
|
+ .add(new Paragraph("版本信息("+versionMap.size()+")")
|
|
|
+ .setFontColor(new DeviceRgb(153,153,153))
|
|
|
+ .setFontSize(10)
|
|
|
+ .setMarginLeft(10)
|
|
|
+ .setMarginTop(10)
|
|
|
+ )
|
|
|
+ .add(versionTable).setFontSize(10));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Table companyTable = new Table(2);
|
|
|
+ DeviceRgb grey = new DeviceRgb( 136,136,136);
|
|
|
+ mapToTable(companyMap,companyTable,grey,grey);
|
|
|
+
|
|
|
+ Text firstCh = new Text("*").setFontColor(red).setFontSize(8);
|
|
|
+ Text rest = new Text("该车辆检测报告仅对本次检查结果负责")
|
|
|
+ .setFontSize(8)
|
|
|
+ .setFontColor(grey);
|
|
|
+
|
|
|
+ document.add(new Div()
|
|
|
+ .setBackgroundColor(new DeviceRgb( 235,241,250))
|
|
|
+ .add(companyTable)
|
|
|
+ .setMarginTop(10)
|
|
|
+ .add(new Paragraph().add(firstCh).add(rest)
|
|
|
+ .setMarginLeft(10)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ document.close();
|
|
|
+ System.out.println("操作完成!");
|
|
|
+ File file = new File("src\\main\\resources\\template\\outpdf\\"+yyyyMMdd+"report"+id+".pdf");
|
|
|
+
|
|
|
+ return file;
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.err.println("操作异常...");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static void mapToTable(Map<String,String> map,Table table,Color one,Color two){
|
|
|
+ table.setMargin(5);
|
|
|
+ table.setWidth(270);
|
|
|
+ table.setFontSize(10);
|
|
|
+ table.setFontColor(new DeviceRgb(136, 136, 136));
|
|
|
+ for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
+ table.addCell(entry.getKey() + ": ")
|
|
|
+ .setFontColor(one);
|
|
|
+ String value = entry.getValue();
|
|
|
+ if (StringUtils.isEmpty(value)){
|
|
|
+ value = " ";
|
|
|
+ }
|
|
|
+ table.addCell(new Cell()
|
|
|
+ .add(new Paragraph(value))
|
|
|
+ .setFontColor(two));
|
|
|
+ }
|
|
|
+ RemoveBorder(table);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void RemoveBorder(Table table)
|
|
|
+ {
|
|
|
+ for (IElement iElement : table.getChildren()) {
|
|
|
+ ((Cell)iElement).setBorder(Border.NO_BORDER);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取统一字体
|
|
|
+ public static PdfFont getPdfFont() {
|
|
|
+ PdfFont pdfFont = null;
|
|
|
+ try {
|
|
|
+ pdfFont = PdfFontFactory.createFont(ITextPdfUtil.FONT, PdfEncodings.IDENTITY_H, true);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return pdfFont;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //测试
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Map<String,String> baseMap = new HashMap<>();
|
|
|
+ baseMap.put("车架号","01");
|
|
|
+ baseMap.put("里程","52Km");
|
|
|
+ baseMap.put("年款","1000");
|
|
|
+ baseMap.put("报告编号","01240222242360836214099968");
|
|
|
+ baseMap.put("诊断路径","诊断>五菱新能源TEST");
|
|
|
+ baseMap.put("SN","WL01000005");
|
|
|
+
|
|
|
+ Map<String,String> faultCodeMap = new HashMap<>();
|
|
|
+ faultCodeMap.put("B111716","供电电压电量过低");
|
|
|
+ faultCodeMap.put("U007588","CHA CAN网段控制器BuSOff");
|
|
|
+ faultCodeMap.put("P129600","非N或P档无法ready");
|
|
|
+ faultCodeMap.put("P129700","充电连接信号有效无法ready");
|
|
|
+
|
|
|
+ Map<String,String> versionMap = new HashMap<>();
|
|
|
+ versionMap.put("引导软件标识符","BtswV1.00");
|
|
|
+ versionMap.put("零件号","0000000000");
|
|
|
+ versionMap.put("定义的ECU软件编号","G050_1HB20");
|
|
|
+ versionMap.put("定义的ECU硬件编号","G050_1HB20");
|
|
|
+ versionMap.put("供应商代码","S-00009475");
|
|
|
+ versionMap.put("ECU名称","0000000VCU");
|
|
|
+ versionMap.put("供应商硬件版本号","V005.001");
|
|
|
+ versionMap.put("供应商软件版本号","V001.000");
|
|
|
+ versionMap.put("ECU序列编号","HB00009475230425T000000");
|
|
|
+ versionMap.put("车辆识别号","LZWADAGA4LB460541");
|
|
|
+ versionMap.put("SK码","00000000000000000000000000000000");
|
|
|
+
|
|
|
+
|
|
|
+ Map<String,String> companyMap = new HashMap<>();
|
|
|
+ companyMap.put("公司","XXXXXX");
|
|
|
+ companyMap.put("地址","XXXXXXXXXXXX");
|
|
|
+ companyMap.put("电话","18888888888");
|
|
|
+
|
|
|
+ List<ControlListMap> controlListMap = new ArrayList<>();
|
|
|
+ ControlListMap controlListMap1 = new ControlListMap();
|
|
|
+ controlListMap1.setName("G050右舵车/VCU整车控制器");
|
|
|
+ controlListMap1.setVersionMap(versionMap);
|
|
|
+ controlListMap1.setFaultCodeMap(faultCodeMap);
|
|
|
+ controlListMap.add(controlListMap1);
|
|
|
+
|
|
|
+ File file = MapToPDF(1, controlListMap, baseMap, companyMap);
|
|
|
+ }
|
|
|
+}
|