1
0
Просмотр исходного кода

Merge branch 'master' of xwh/kmall-pt-general into master

张创标 4 лет назад
Родитель
Сommit
51c0800573

+ 4 - 0
kmall-admin/src/main/java/com/kmall/admin/controller/exportPDF/ExportPDFController.java

@@ -41,6 +41,7 @@ public class ExportPDFController {
             Map<String, Object> model = new HashMap<>();
             Map<String, Object> model = new HashMap<>();
             model.put("dto", pdfGoodsDto);
             model.put("dto", pdfGoodsDto);
             model.put("type",type);
             model.put("type",type);
+            model.put("head",head);
             model.put("fontUrl",fontUrl);
             model.put("fontUrl",fontUrl);
             model.put("uploadDir",uploadDir);
             model.put("uploadDir",uploadDir);
             return new ModelAndView(new ViewPDF(), model);
             return new ModelAndView(new ViewPDF(), model);
@@ -48,6 +49,9 @@ public class ExportPDFController {
             Map<String, Object> model = new HashMap<>();
             Map<String, Object> model = new HashMap<>();
             model.put("dto", pdfGoodsDto);
             model.put("dto", pdfGoodsDto);
             model.put("type",type);
             model.put("type",type);
+            model.put("head",head);
+            model.put("fontUrl",fontUrl);
+            model.put("uploadDir",uploadDir);
             return new ModelAndView(new ViewPDF(), model);
             return new ModelAndView(new ViewPDF(), model);
         }else{
         }else{
             return null;
             return null;

+ 8 - 3
kmall-admin/src/main/java/com/kmall/admin/utils/pdf/PdfUtil.java

@@ -11,7 +11,7 @@ import java.io.IOException;
 public class PdfUtil {
 public class PdfUtil {
 
 
   public void createPDF(Document document, PdfWriter writer, PDFGoodsDto pdfGoodsDto,
   public void createPDF(Document document, PdfWriter writer, PDFGoodsDto pdfGoodsDto,
-                        String type, String fontUrl, String uploadDir) throws IOException, com.lowagie.text.DocumentException {
+                        String type, String fontUrl, String uploadDir,String head) throws IOException, com.lowagie.text.DocumentException {
 
 
       switch (type){
       switch (type){
           case "1":
           case "1":
@@ -35,8 +35,13 @@ public class PdfUtil {
               pdfWrite12.write(document,writer,pdfGoodsDto,fontUrl,uploadDir);
               pdfWrite12.write(document,writer,pdfGoodsDto,fontUrl,uploadDir);
               break;
               break;
           case "36":
           case "36":
-              PDFWrite_36 pdfWrite36 = new PDFWrite_36();
-              pdfWrite36.write(document,writer,pdfGoodsDto,fontUrl,uploadDir);
+              if ("1".equals(head)) {
+                  PDFWrite_36 pdfWrite36 = new PDFWrite_36();
+                  pdfWrite36.write(document, writer, pdfGoodsDto, fontUrl, uploadDir);
+              }else if ("0".equals(head)){
+                  PDFWrite_nohead_36 pdfWrite_nohead_36 = new PDFWrite_nohead_36();
+                  pdfWrite_nohead_36.write(document, writer, pdfGoodsDto, fontUrl, uploadDir);
+              }
               break;
               break;
           default:
           default:
               break;
               break;

+ 2 - 1
kmall-admin/src/main/java/com/kmall/admin/utils/pdf/ViewPDF.java

@@ -25,6 +25,7 @@ public class ViewPDF extends AbstractPdfView {
       PDFGoodsDto pdfGoodsDto = (PDFGoodsDto) model.get("dto");
       PDFGoodsDto pdfGoodsDto = (PDFGoodsDto) model.get("dto");
       String fontUrl = (String) model.get("fontUrl");
       String fontUrl = (String) model.get("fontUrl");
       String uploadDir = (String) model.get("uploadDir");
       String uploadDir = (String) model.get("uploadDir");
-      pdfUtil.createPDF(document,writer,pdfGoodsDto,type,fontUrl,uploadDir);
+      String head = (String) model.get("head");
+      pdfUtil.createPDF(document,writer,pdfGoodsDto,type,fontUrl,uploadDir,head);
   }
   }
 }
 }

+ 182 - 0
kmall-admin/src/main/java/com/kmall/admin/utils/pdf/type/PDFWrite_nohead_36.java

@@ -0,0 +1,182 @@
+package com.kmall.admin.utils.pdf.type;
+
+import com.kmall.api.entity.exportpdf.PDFGoodsDto;
+import com.lowagie.text.*;
+import com.lowagie.text.Font;
+import com.lowagie.text.Image;
+import com.lowagie.text.pdf.*;
+
+import java.awt.*;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+/**
+ * @author zhangchuangbiao
+ * @version 1.0
+ * 2020-09-11 09:51
+ */
+public class PDFWrite_nohead_36 {
+
+
+    public void write(Document doc, PdfWriter writer, PDFGoodsDto pdfGoodsDto
+            , String fontUrl, String uploadDir) throws IOException, DocumentException {
+
+        // 设置字体
+        BaseFont bfHei = BaseFont.createFont(fontUrl,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
+        Font FontChinese11 = new Font(bfHei, 11);
+
+        String path = uploadDir;
+        FileOutputStream out = new FileOutputStream(path);
+
+//        doc.setMargins(0,0,0,0);
+
+        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);//版本(默认1.4)
+        doc.open();// 打开文档
+
+        // -----------------------标签头----------------------
+        for (int i = 0; i < 9; i++) {
+            getPdfPTable(FontChinese11, bfHei, doc,pdfGoodsDto);
+        }
+
+        doc.close();  //记得关闭document
+
+    }
+
+    private static void getPdfPTable(Font fontChinese11, BaseFont bfHei, Document doc, PDFGoodsDto pdfGoodsDto) throws IOException, DocumentException {
+        PdfPTable table = new PdfPTable(4);
+
+        // --------------产品名称-------------------------------
+        table = new PdfPTable(4);
+
+        for (int i = 0; i < 4; i++) {
+            setTable(bfHei, table, 13, pdfGoodsDto.getProductName());
+        }
+
+        doc.add(table);
+
+
+
+        // --------------产品英文名称-------------------------------
+        table = new PdfPTable(4);
+
+        for (int i = 0; i < 4; i++) {
+            setTable(bfHei, table, 7, pdfGoodsDto.getEnglishName());
+        }
+        doc.add(table);
+
+
+
+        table = new PdfPTable(4);
+
+        for (int i = 0; i < 4; i++) {
+           PdfPTable pdfPTable4 = new PdfPTable(1);
+            Font font = new Font(bfHei, 20);
+            Paragraph bigPrice = new Paragraph("¥"+pdfGoodsDto.getPrice().substring(0,pdfGoodsDto.getPrice().indexOf(".")), font);
+            bigPrice.setAlignment(Element.ALIGN_BOTTOM);
+            // --------------小价格-------------------------------
+            font = new Font(bfHei, 10);
+            Paragraph smallPrice = new Paragraph(pdfGoodsDto.getPrice().substring(pdfGoodsDto.getPrice().indexOf(".")), font);
+            smallPrice.setAlignment(Element.ALIGN_BOTTOM);
+            smallPrice.setIndentationLeft(50);
+            bigPrice.add(smallPrice);
+
+
+
+            PdfPCell priceCell = new PdfPCell(bigPrice);
+            priceCell.setBorder(0);
+            priceCell.setHorizontalAlignment(Element.ALIGN_LEFT); // 水平居中
+            priceCell.setVerticalAlignment(Element.ALIGN_BOTTOM); // 垂直居中
+            pdfPTable4.addCell(priceCell);
+
+
+            // ----------------test-----------------------
+            PdfPTable pdfPTable8 = new PdfPTable(2);
+            pdfPTable8.setWidths(new int[]{10,8});
+            font = new Font(bfHei, 6);
+            // --------------左上的字-------------------------------
+            Paragraph leftParagraph1 = new Paragraph("\n建议零售价省¥"+pdfGoodsDto.getSavings()+"\n\n", font);
+            font = new Font(bfHei, 4.5f);
+            Paragraph leftParagraph2 = new Paragraph("税率:"+pdfGoodsDto.getStartFax()+"\n", font);
+            leftParagraph1.add(leftParagraph2);
+            PdfPCell leftCell1 = new PdfPCell(leftParagraph1);
+            leftCell1.setBorder(0);
+            leftCell1.setHorizontalAlignment(Element.ALIGN_LEFT); // 水平居中
+            leftCell1.setVerticalAlignment(Element.ALIGN_MIDDLE); // 垂直居中
+            pdfPTable8.addCell(leftCell1);
+            pdfPTable8.addCell(priceCell);
+
+            PdfPCell p = new PdfPCell(pdfPTable8);
+            p.setBorder(0);
+            table.setWidthPercentage(110f);
+            table.addCell(p);
+        }
+
+
+        doc.add(table);
+
+        table = new PdfPTable(4);
+        for (int i = 0; i < 4; i++) {
+            // --------------底部黑色和条形码区域-------------------------------
+            PdfPTable pdfPTable7 = new PdfPTable(2);
+            Font font = new Font(bfHei, 5.5f);
+            font.setColor(Color.WHITE);
+            Paragraph PLU = new Paragraph(pdfGoodsDto.getPlu(), font);
+            Paragraph blankParagraph = new Paragraph("                        ", font);
+            Paragraph barCode = new Paragraph(pdfGoodsDto.getBarCode().substring(pdfGoodsDto.getBarCode().length()-4), font);
+            blankParagraph.add(barCode);
+            PLU.add(blankParagraph);
+            PLU.setAlignment(Element.ALIGN_CENTER);
+
+            PdfPCell pluCell = new PdfPCell(PLU);
+            pluCell.setBackgroundColor(Color.BLACK);
+            pluCell.setBorder(0);
+            pluCell.setFixedHeight(10.5f);
+            pluCell.setHorizontalAlignment(Element.ALIGN_LEFT); // 水平居中
+            pluCell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 垂直居中
+            // -------------条形码图片
+            Image img = Image.getInstance("E:\\demo\\barCode.jpg");
+            img.setAlignment(Image.ALIGN_CENTER);  //设置图片居中
+            img.setBorder(Image.NO_BORDER);
+            img.setBorderColor(Color.WHITE);
+            img.scaleAbsolute(72,10.5f);
+            PdfPCell imgCell = new PdfPCell(img,false);
+            imgCell.setBorder(0);
+            pdfPTable7.addCell(pluCell);
+            pdfPTable7.addCell(imgCell);
+            PdfPCell p = new PdfPCell(pdfPTable7);
+            p.setBorder(0);
+            table.setWidthPercentage(110f);
+            table.addCell(p);
+        }
+        doc.add(table);
+        table = new PdfPTable(1);
+        Paragraph blankRow = new Paragraph("\n");
+        PdfPCell nextLine = new PdfPCell(blankRow);
+        nextLine.setBorder(0);
+        nextLine.setFixedHeight(8.5f);
+        table.addCell(nextLine);
+        doc.add(table);
+
+
+
+    }
+
+
+    // 去除边框,设置水平垂直居中
+    private static void setBasisStyle(PdfPCell pluCell, int alignLeft, int alignMiddle) {
+        pluCell.setBorder(0);
+        pluCell.setHorizontalAlignment(alignLeft); // 水平居中
+        pluCell.setVerticalAlignment(alignMiddle); // 垂直居中
+    }
+
+    private static void setTable(BaseFont bfHei, PdfPTable table, int i2, String s) {
+        Font font = new Font(bfHei, i2); // 设置字体
+        Paragraph paragraph = new Paragraph(s, font); // 段落
+        PdfPCell nameCell = new PdfPCell(paragraph); // 单元Cell
+        setBasisStyle(nameCell, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE);
+        table.setWidthPercentage(110f);
+        table.addCell(nameCell);
+    }
+
+
+}