Browse Source

下载PDF开发

anderx 3 years ago
parent
commit
c6316efcfd

+ 48 - 9
src/main/java/com/goafanti/RD/service/impl/RDServiceImpl.java

@@ -9,6 +9,7 @@ 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.pdf.PDFUtils;
 import com.goafanti.common.utils.word.WordUtils;
 import com.goafanti.core.mybatis.BaseMybatisDao;
 import com.goafanti.core.mybatis.page.Pagination;
@@ -156,6 +157,7 @@ public class RDServiceImpl extends BaseMybatisDao<RdDetailsMapper> implements RD
     @Override
     public void pushDownloadDetailsWord(Long id, HttpServletResponse response) {
         OutRdDetails bo = rdDetailsMapper.details(id);
+        //        rdDetailsMapper.updateBrowseOrDownload(id, 1);
         OutWordRdDetails data =new OutWordRdDetails();
         BeanUtils.copyProperties(bo,data);
         if (bo.getRdStartStr()!=null&&bo.getRdEndStr()!=null){
@@ -174,27 +176,64 @@ public class RDServiceImpl extends BaseMybatisDao<RdDetailsMapper> implements RD
     @Override
     public void pushDownloadDetailsPDF(Long id, HttpServletResponse response) {
         OutRdDetails bo = rdDetailsMapper.details(id);
-        rdDetailsMapper.updateBrowseOrDownload(id, 1);
+//        rdDetailsMapper.updateBrowseOrDownload(id, 1);
         OutWordRdDetails data = new OutWordRdDetails();
         BeanUtils.copyProperties(bo, data);
-        data.setStartEndTime(bo.getRdStartStr() + "~" + bo.getRdEndStr());
-
+        if (bo.getRdStartStr()!=null&&bo.getRdEndStr()!=null){
+            data.setStartEndTime(bo.getRdStartStr()+"~"+bo.getRdEndStr());
+        }else {
+            data.setStartEndTime("");
+        }
         String tableName = data.getRdName();
-        String url = uploadPath + "/tmp/RD_details_word_template.pdf";
-
-        String realFileName = uploadPath+"/tmp/"+DateUtils.parseDateToStr(AFTConstants.YYYYMMDDHHMMSS_OUT, new Date()) + ".pdf";
-//        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
+        String realFileName = uploadPath+"/tmp/"+new Date().getTime() + ".pdf";
+        String attName = data.getRdName()+new Date().getTime() + ".pdf";
+        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
 
         Document document=new Document();
         try {
-            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(realFileName));
+        FileUtils.setAttachmentResponseHeader(response, tableName,attName);
+            FileOutputStream outputStream = new FileOutputStream(realFileName);
+            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
             document.open();
-            document.add(new Paragraph("Hello Word"));
+            Paragraph paragraph = new Paragraph(data.getRdName(), PDFUtils.getTitleFont());
+            paragraph.setAlignment(1);
+            document.add(paragraph);
+            addDocument(document,"项目起止时间: ",data.getStartEndTime());
+            addDocument(document,"公司名称: ",data.getUserName());
+            addDocument(document,"项目负责人: ",data.getConsultantName());
+            addDocument(document,"技术领域: ",data.getTechnicalField());
+            addDocument(document,"技术来源: ",data.getTechnologySource());
+            addDocument(document,"研发费用总预计: ",data.getTotalAmount().toEngineeringString()+" 万元");
+            addDocument(document,"研发目的/立项目的/实施方式",null);
+            addContent(document,"        "+data.getRdObjective());
+            addDocument(document,"核心技术/创新点",null);
+            addContent(document,"        "+data.getCoreTechnology());
+            addDocument(document,"成果",null);
+            addContent(document,"        "+data.getAchieveResults());
             document.close();
+            outputStream.close();
+            FileUtils.writeBytes(realFileName, response.getOutputStream());
+            FileUtils.deleteFile(realFileName);
         } catch (DocumentException e) {
             e.printStackTrace();
         } catch (FileNotFoundException e) {
             e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
         }
     }
+
+    private void addDocument(Document document, String key, String value) throws DocumentException {
+        if(key!=null)document.add(new Chunk(key,PDFUtils.getBigFont()));
+        Phrase phrase = new Phrase(value, PDFUtils.getFont());
+        phrase.setLeading(40);
+        if(value!=null)document.add(phrase);
+        document.add(Chunk.NEWLINE);
+    }
+    private void addContent(Document document,  String value) throws DocumentException {
+        Phrase phrase = new Phrase(value, PDFUtils.getFont());
+        phrase.setLeading(25);
+        if(value!=null)document.add(phrase);
+        document.add(Chunk.NEWLINE);
+    }
 }

+ 40 - 0
src/main/java/com/goafanti/common/utils/pdf/PDFUtils.java

@@ -1,8 +1,13 @@
 package com.goafanti.common.utils.pdf;
 
+import com.itextpdf.text.BaseColor;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Font;
+import com.itextpdf.text.pdf.BaseFont;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.awt.*;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -10,6 +15,8 @@ import java.io.InputStream;
 public class PDFUtils {
 
 
+
+
     public void loadPDF(MultipartFile file){
 
         try {
@@ -19,4 +26,37 @@ public class PDFUtils {
             e.printStackTrace();
         }
     }
+    public static Font getTitleFont(){
+        return setFont(24,Font.BOLD);
+    }
+
+    public static Font getBigFont(){
+        return setFont(16,Font.BOLD);
+    }
+
+    public static Font getFont(){
+        Font font =setFont(12,Font.NORMAL);
+        return font;
+    }
+
+
+    /**
+     *
+     * @param size  大小
+     * @return
+     */
+    public static Font setFont(Integer size,Integer style){
+            BaseFont baseFont = null;
+            Font font=null;
+        try {
+            baseFont=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
+            font=new Font(baseFont,size,style);
+        } catch (DocumentException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        return font;
+    }
 }