浏览代码

华为云obs和pdf修改

bmmx 1 年之前
父节点
当前提交
fe82856257
共有 2 个文件被更改,包括 97 次插入90 次删除
  1. 3 2
      src/main/java/com/om/utils/HuaweiObsUtil.java
  2. 94 88
      src/main/java/com/om/utils/ITextPdfUtil.java

+ 3 - 2
src/main/java/com/om/utils/HuaweiObsUtil.java

@@ -100,8 +100,9 @@ public class HuaweiObsUtil {
             int statusCode = response.getStatusCode();
             if (200 == statusCode) {
                 // 拼接文件在OBS中的URL并返回
-                String objectUrl = "https://"+path + "/" + objectName;
-                return objectUrl;
+                String uploadUrl = getUploadUrl(originalFilename, prefix);
+                //String objectUrl = "https://"+path + "/" + objectName;
+                return uploadUrl;
             }
             // 关闭OBS客户端
             obsClient.close();

+ 94 - 88
src/main/java/com/om/utils/ITextPdfUtil.java

@@ -1,7 +1,12 @@
 package com.om.utils;
 
 
+
 import com.itextpdf.io.font.PdfEncodings;
+import com.itextpdf.io.image.BmpImageData;
+import com.itextpdf.io.image.ImageData;
+import com.itextpdf.io.image.ImageDataFactory;
+import com.itextpdf.io.image.PngImageData;
 import com.itextpdf.kernel.colors.Color;
 import com.itextpdf.kernel.colors.DeviceRgb;
 import com.itextpdf.kernel.font.PdfFont;
@@ -10,6 +15,7 @@ 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.annot.PdfLinkAnnotation;
 import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
 import com.itextpdf.layout.Document;
 import com.itextpdf.layout.Style;
@@ -25,6 +31,7 @@ import org.apache.commons.lang3.StringUtils;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.net.URL;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
@@ -44,25 +51,21 @@ public class ITextPdfUtil {
 
 
     //生成简单PDF
-    public static File MapToPDF(Integer id,List<ControlListMap> controlListMap
-            , Map<String,String> baseMap, Map<String,String> companyMap ) {
+    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");
+        try (PdfWriter writer = new PdfWriter("src\\main\\resources\\" + 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);
+            DeviceRgb black = new DeviceRgb(34, 34, 34);
             //设置文档属性
             pdf.getDocumentInfo().setAuthor("bmmx");
-            pdf.getDocumentInfo().setTitle("IText测试PDF");
+            pdf.getDocumentInfo().setTitle("诊断报告");
             pdf.getDocumentInfo().setSubject("XXX公司");
-            pdf.getDocumentInfo().setMoreInfo("1", "111");
-            pdf.getDocumentInfo().setCreator("huanzi");
-            pdf.getDocumentInfo().setKeywords("IText");
 
             //设置字体
             document.setFont(ITextPdfUtil.getPdfFont());
@@ -84,34 +87,39 @@ public class ITextPdfUtil {
             DeviceRgb c1 = new DeviceRgb(136, 136, 136);
             DeviceRgb c2 = new DeviceRgb(40, 40, 40);
             Table baseTable = new Table(2);
-            mapToTable(baseMap,baseTable,c1,c2);
-
+            mapToTable(baseMap, baseTable, c1, c2);
 
+            ImageData imageData = ImageDataFactory.create("src\\main\\resources\\img_1.png");
+            Image update = new Image(imageData);
+            update.setWidth(20);
+            update.setHeight(20);
+            update.setMarginLeft(250);
 
 
             // 车辆信息
             document.add(new Div()
-                            .setMarginTop(10)
-                            .setHorizontalAlignment(HorizontalAlignment.CENTER)
-                            .setBackgroundColor(new DeviceRgb(235, 241, 250))
-                            .setBorderRadius(new BorderRadius(5))
+                    .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))
+                            .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(update)
                     .add(baseTable));
 
             SolidLine solidLine = new SolidLine(0.2f);
-            solidLine.setColor(new DeviceRgb(64,109,183));
+            solidLine.setColor(new DeviceRgb(64, 109, 183));
             //solidLine.setLineWidth(260);
 
             for (ControlListMap listMap : controlListMap) {
@@ -121,11 +129,11 @@ public class ITextPdfUtil {
 
                 Table faultCodeTable = new Table(2);
 
-                mapToTable(faultCodeMap,faultCodeTable,red,red);
+                mapToTable(faultCodeMap, faultCodeTable, red, red);
 
                 Table versionTable = new Table(2);
 
-                mapToTable(versionMap,versionTable,black,black);
+                mapToTable(versionMap, versionTable, black, black);
                 // 车辆信息
                 document.add(new Div()
                         .setMarginTop(10)
@@ -134,7 +142,7 @@ public class ITextPdfUtil {
                         .setBorderRadius(new BorderRadius(5))
 
                         .add(new Div()
-                                .setBackgroundColor(new DeviceRgb( 64,109,183))
+                                .setBackgroundColor(new DeviceRgb(64, 109, 183))
                                 .setBorderBottomLeftRadius(new BorderRadius(8))
                                 .setBorderBottomRightRadius(new BorderRadius(8))
                                 .setWidth(100)
@@ -142,32 +150,32 @@ public class ITextPdfUtil {
                                 .add(new Paragraph("诊断详情")
                                         .setTextAlignment(TextAlignment.CENTER)
                                         .addStyle(new Style()
-                                                .setFontColor(new DeviceRgb(255,255,255))
+                                                .setFontColor(new DeviceRgb(255, 255, 255))
                                                 .setBold()
                                         )))
                         .add(new Div()
-                                .setBackgroundColor(new DeviceRgb( 64,109,183))
+                                .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))
+                                        .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))
+                        .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))
+                        .add(new Paragraph("版本信息(" + versionMap.size() + ")")
+                                .setFontColor(new DeviceRgb(153, 153, 153))
                                 .setFontSize(10)
                                 .setMarginLeft(10)
                                 .setMarginTop(10)
@@ -176,11 +184,9 @@ public class ITextPdfUtil {
             }
 
 
-
-
             Table companyTable = new Table(2);
-            DeviceRgb grey = new DeviceRgb( 136,136,136);
-            mapToTable(companyMap,companyTable,grey,grey);
+            DeviceRgb grey = new DeviceRgb(136, 136, 136);
+            mapToTable(companyMap, companyTable, grey, grey);
 
             Text firstCh = new Text("*").setFontColor(red).setFontSize(8);
             Text rest = new Text("该车辆检测报告仅对本次检查结果负责")
@@ -188,16 +194,18 @@ public class ITextPdfUtil {
                     .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)
-                            )
-                    );
+                    .add(update)
+                    .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");
+            File file = new File("src\\main\\resources\\" + yyyyMMdd + "report" + id + ".pdf");
+
 
             return file;
 
@@ -209,7 +217,7 @@ public class ITextPdfUtil {
     }
 
 
-    private static void mapToTable(Map<String,String> map,Table table,Color one,Color two){
+    private static void mapToTable(Map<String, String> map, Table table, Color one, Color two) {
         table.setMargin(5);
         table.setWidth(270);
         table.setFontSize(10);
@@ -218,7 +226,7 @@ public class ITextPdfUtil {
             table.addCell(entry.getKey() + ": ")
                     .setFontColor(one);
             String value = entry.getValue();
-            if (StringUtils.isEmpty(value)){
+            if (StringUtils.isEmpty(value)) {
                 value = " ";
             }
             table.addCell(new Cell()
@@ -228,10 +236,9 @@ public class ITextPdfUtil {
         RemoveBorder(table);
     }
 
-    private static void RemoveBorder(Table table)
-    {
+    private static void RemoveBorder(Table table) {
         for (IElement iElement : table.getChildren()) {
-            ((Cell)iElement).setBorder(Border.NO_BORDER);
+            ((Cell) iElement).setBorder(Border.NO_BORDER);
         }
     }
 
@@ -247,41 +254,40 @@ public class ITextPdfUtil {
     }
 
 
-
     //测试
     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");
+        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();