Ver código fonte

检查报告预览

wangbo 8 meses atrás
pai
commit
92ff5c8d0a

+ 1 - 2
business-common/src/main/java/com/rongwei/bscommon/sys/service/ZhcxSeaBindProofService.java

@@ -1,7 +1,6 @@
 package com.rongwei.bscommon.sys.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.rongwei.bsentity.domain.ZhcxInspectionReportManageDo;
 import com.rongwei.bsentity.domain.ZhcxSeaBindProofDo;
 
 import javax.servlet.http.HttpServletResponse;
@@ -14,6 +13,6 @@ public interface ZhcxSeaBindProofService extends IService<ZhcxSeaBindProofDo> {
     void seabingPreviewExcel(HttpServletResponse response, ZhcxSeaBindProofDo zhcxSeaBindProofDo) throws Exception;
 
 
-
+     void  previewPdf(HttpServletResponse response, String id);
 
 }

+ 35 - 1
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ZhcxSeaBindProofServiceImpl.java

@@ -13,6 +13,9 @@ import com.google.zxing.client.j2se.MatrixToImageWriter;
 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.qrcode.QRCodeWriter;
 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.pdf.PdfReader;
+import com.lowagie.text.pdf.PdfStamper;
 import com.rongwei.bscommon.sys.dao.ZhcxSeaBindProofDao;
 import com.rongwei.bscommon.sys.service.ZhcxSeaBindProofDetalService;
 import com.rongwei.bscommon.sys.service.ZhcxSeaBindProofService;
@@ -61,7 +64,7 @@ public class ZhcxSeaBindProofServiceImpl extends ServiceImpl<ZhcxSeaBindProofDao
          throw new CustomException("参数异常,请联系管理员排查");
      }
 
-     // 获取模板文件
+     // 获取模板文件1
      SysFileItemDo templateFile = sysFileItemService.getById("351d7b0c79034a25882415ae4332d0c3");
 
      if (!excelUtils.GetLicense()) {
@@ -374,4 +377,35 @@ public class ZhcxSeaBindProofServiceImpl extends ServiceImpl<ZhcxSeaBindProofDao
         }
     }
 
+    @Override
+    public void previewPdf(HttpServletResponse response, String id) {
+        // 通过 id 获取 SysFileItemDo 对象
+        SysFileItemDo templateFile = sysFileItemService.getById(id);
+
+        // 获取 PDF 文件的路径
+        String filePath = templateFile.getFullpath();
+
+        // 设置响应头,告诉浏览器这是一个 PDF 文件,并且以预览方式显示
+        response.setContentType("application/pdf");
+        response.setHeader("Content-Disposition", "inline; filename=\"" + new File(filePath).getName() + "\"");
+
+        // 读取 PDF 文件并将其输出到响应流
+        try (FileInputStream pdfInputStream = new FileInputStream(filePath);
+             OutputStream outputStream = response.getOutputStream()) {
+            // 使用 iText 读取 PDF 文件
+            PdfReader pdfReader = new PdfReader(pdfInputStream);
+            PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
+
+            // 不做任何修改,直接将文件输出到响应流
+            pdfStamper.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+            // 这里可以处理异常,例如返回一个错误信息
+        } catch (DocumentException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+
+
 }

+ 8 - 0
business-server/src/main/java/com/rongwei/bsserver/controller/ZhcxSeaBindProofController.java

@@ -49,7 +49,15 @@ public class ZhcxSeaBindProofController {
     }
 
 
+    @RequestMapping("/previewPdf/{id}")
+    public void previewPdf(HttpServletResponse response,@PathVariable("id") String id) {
+        try {
 
+            zhcxSeaBindProofService.previewPdf(response,id);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }