Selaa lähdekoodia

导出Wrod开发

anderx 3 vuotta sitten
vanhempi
commit
8e5410f5b4

+ 6 - 0
pom.xml

@@ -407,6 +407,12 @@
 			<artifactId>easyword</artifactId>
 			<version>1.1.5</version>
 		</dependency>
+		<!--pdf-->
+		<dependency>
+			<groupId>com.itextpdf</groupId>
+			<artifactId>itextpdf</artifactId>
+			<version>5.5.13</version>
+		</dependency>
 
 
 

+ 0 - 1
src/main/java/com/goafanti/RD/bo/OutWordRdDetails.java

@@ -6,7 +6,6 @@ import groovy.transform.builder.Builder;
 
 import java.math.BigDecimal;
 
-@Builder
 public class OutWordRdDetails {
 
 

+ 7 - 4
src/main/java/com/goafanti/RD/controller/RDContorller.java

@@ -168,11 +168,14 @@ public class RDContorller extends BaseApiController {
         }
     }
 
-    @RequestMapping(value = "/detailsOUtWord",method = RequestMethod.GET)
-    public void detailsOUtWord(Long id,HttpServletResponse response){
-        rdService.detailsOUtWord(id,response);
+    @RequestMapping(value = "/downloadDetailsWord",method = RequestMethod.GET)
+    public void downloadDetailsWord(Long id,HttpServletResponse response){
+        rdService.pushDownloadDetailsWord(id,response);
     }
 
-
+    @RequestMapping(value = "/downloadDetailsPDF",method = RequestMethod.GET)
+    public void downloadDetailsPDF(Long id,HttpServletResponse response){
+        rdService.downloadDetailsPDF(id,response);
+    }
 
 }

+ 2 - 1
src/main/java/com/goafanti/RD/service/RDService.java

@@ -40,6 +40,7 @@ public interface RDService {
      */
     int updateBrowseOrDownload(Long id, Integer type);
 
-    void detailsOUtWord(Long id, HttpServletResponse response);
+    void pushDownloadDetailsWord(Long id, HttpServletResponse response);
 
+    void downloadDetailsPDF(Long id, HttpServletResponse response);
 }

+ 103 - 2
src/main/java/com/goafanti/RD/service/impl/RDServiceImpl.java

@@ -3,21 +3,31 @@ package com.goafanti.RD.service.impl;
 import com.goafanti.RD.bo.*;
 import com.goafanti.RD.service.RDService;
 import com.goafanti.common.bo.Result;
+import com.goafanti.common.constant.AFTConstants;
 import com.goafanti.common.dao.RdDetailsMapper;
 import com.goafanti.common.model.RdDetails;
+import com.goafanti.common.utils.DateUtils;
+import com.goafanti.common.utils.excel.FileUtils;
 import com.goafanti.common.utils.excel.NewExcelUtil;
 import com.goafanti.common.utils.word.WordUtils;
 import com.goafanti.core.mybatis.BaseMybatisDao;
 import com.goafanti.core.mybatis.page.Pagination;
 import com.goafanti.core.shiro.token.TokenManager;
+import com.itextpdf.text.Document;
+import com.itextpdf.text.Image;
+import com.itextpdf.text.Rectangle;
+import com.itextpdf.text.pdf.*;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.util.*;
 
 @Service
@@ -141,9 +151,8 @@ public class RDServiceImpl extends BaseMybatisDao<RdDetailsMapper> implements RD
 
 
     @Override
-    public void detailsOUtWord(Long id, HttpServletResponse response) {
+    public void pushDownloadDetailsWord(Long id, HttpServletResponse response) {
         OutRdDetails bo = rdDetailsMapper.details(id);
-        rdDetailsMapper.updateBrowseOrDownload(id,1);
         OutWordRdDetails data =new OutWordRdDetails();
         BeanUtils.copyProperties(bo,data);
         data.setStartEndTime(bo.getRdStartStr()+"~"+bo.getRdEndStr());
@@ -154,4 +163,96 @@ public class RDServiceImpl extends BaseMybatisDao<RdDetailsMapper> implements RD
             e.printStackTrace();
         }
     }
+
+    @Override
+    public void downloadDetailsPDF(Long id, HttpServletResponse response) {
+        OutRdDetails bo = rdDetailsMapper.details(id);
+        rdDetailsMapper.updateBrowseOrDownload(id,1);
+        OutWordRdDetails data =new OutWordRdDetails();
+        BeanUtils.copyProperties(bo,data);
+        data.setStartEndTime(bo.getRdStartStr()+"~"+bo.getRdEndStr());
+
+        String tableName = data.getRdName();
+        String url=uploadPath+"/tmp/RD_details_word_template.pdf";
+
+        String realFileName = tableName+ DateUtils.parseDateToStr(AFTConstants.YYYYMMDDHHMMSS, new Date())+".docx";
+        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
+        OutputStream out = null;
+        ByteArrayOutputStream bos = null;
+        PdfStamper stamper = null;
+        PdfReader reader = null;
+        try {
+        FileUtils.setAttachmentResponseHeader(response, tableName,realFileName);
+            // 保存到本地
+            // out = new FileOutputStream(fileName);
+            // 输出到浏览器端
+            out = response.getOutputStream();
+            // 读取PDF模板表单
+            reader = new PdfReader(url);
+            // 字节数组流,用来缓存文件流
+            bos = new ByteArrayOutputStream();
+            // 根据模板表单生成一个新的PDF
+            stamper = new PdfStamper(reader, bos);
+            // 获取新生成的PDF表单
+            AcroFields form = stamper.getAcroFields();
+            // 给表单生成中文字体,这里采用系统字体,不设置的话,中文显示会有问题
+            BaseFont font = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
+            form.addSubstitutionFont(font);
+            // 装配数据
+            Map<String, Object> params = new HashMap<>(15);
+            params.put("RdName", data.getRdName());
+            params.put("userName", data.getUserName());
+
+            // 遍历data,给pdf表单赋值
+            for(String key : params.keySet()){
+                // 图片要单独处理
+                if("studentImg".equals(key)){
+                    int pageNo = form.getFieldPositions(key).get(0).page;
+                    Rectangle signRect = form.getFieldPositions(key).get(0).position;
+                    float x = signRect.getLeft();
+                    float y = signRect.getBottom();
+                    String studentImage = params.get(key).toString();
+                    //根据路径或Url读取图片
+                    Image image = Image.getInstance(studentImage);
+                    //获取图片页面
+                    PdfContentByte under = stamper.getOverContent(pageNo);
+                    //图片大小自适应
+                    image.scaleToFit(signRect.getWidth(), signRect.getHeight());
+                    //添加图片
+                    image.setAbsolutePosition(x, y);
+                    under.addImage(image);
+                }
+                // 设置普通文本数据
+                else {
+                    form.setField(key, params.get(key).toString());
+                }
+            }
+            // 表明该PDF不可修改
+            stamper.setFormFlattening(true);
+            // 关闭资源
+            stamper.close();
+            // 将ByteArray字节数组中的流输出到out中(即输出到浏览器)
+            Document doc = new Document();
+            PdfCopy copy = new PdfCopy(doc, out);
+            doc.open();
+            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
+            copy.addPage(importPage);
+            doc.close();
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            try {
+                if (out != null) {
+                    out.flush();
+                    out.close();
+                }
+                if (reader != null) {
+                    reader.close();
+                }
+            }catch (Exception e){
+                e.printStackTrace();
+            }
+        }
+
+    }
 }