Kaynağa Gözat

PDF导出调整-未完全开发

anderx 1 yıl önce
ebeveyn
işleme
01269046f7

+ 34 - 46
src/main/java/com/goafanti/common/utils/pdf/PDFUtils.java

@@ -114,55 +114,27 @@ public class PDFUtils {
         return font;
     }
 
-    public void pushMainExpenseExport(MainExpenseAccount mainExpenseAccount,HttpServletResponse response, String uploadPath) {
-        String title = "报销明细表";
-        String attName = String.format("报销明细表_%s.pdf",new Date().getTime());
-        String realFileName = uploadPath+"/tmp/"+new Date().getTime() + ".pdf";
 
-        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
-        Document document=new Document();
-        try {
-            FileUtils.setAttachmentResponseHeader(response, title,attName);
-            FileOutputStream outputStream = new FileOutputStream(realFileName);
-            PdfWriter.getInstance(document, outputStream);
-            document.open();
-            Paragraph paragraph = new Paragraph(title, getTitleFont());
-            paragraph.setAlignment(1);
-            document.add(paragraph);
-            //默认算60
-            pushExpenseTitle(document,mainExpenseAccount);
 
-            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 static String   CHECKNO="报销编号:";
-     private static String   APPLYDEP="申请部门:";
-     private static String   ANAME="报销人:";
-     private static String   CREATETIME="报销日期:";
-     private static String   REMARKS="报销事由:";
-     private static String   BANK_ACCOUNT="收款方式:";
-    private void pushExpenseTitle(Document document, MainExpenseAccount mainExpenseAccount) throws DocumentException {
+    private static String   CHECKNO="报销编号:";
+    private static String   APPLYDEP="申请部门:";
+    private static String   ANAME="报销人:";
+    private static String   CREATETIME="报销日期:";
+    private static String   REMARKS="报销事由:";
+    private static String   BANK_ACCOUNT="收款方式:";
+    public static void pushExpenseTitle(Document document, MainExpenseAccount mainExpenseAccount) throws DocumentException {
 
         int  x= CHECKNO.length() + mainExpenseAccount.getCheckNo().length()/2 +
                 APPLYDEP.length() + mainExpenseAccount.getApplyDepName().length() +
                 ANAME.length() + mainExpenseAccount.getAname().length();
         StringBuilder builder = new StringBuilder();
         if (x>55){
-            builder.append(CHECKNO).append(mainExpenseAccount.getCheckNo()).append("  ")
-                    .append(APPLYDEP).append(mainExpenseAccount.getApplyDepName()).append(" ")
+            builder.append(CHECKNO).append(mainExpenseAccount.getCheckNo()).append("          ")
+                    .append(APPLYDEP).append(mainExpenseAccount.getApplyDepName()).append("          ")
                     .append(ANAME).append(mainExpenseAccount.getAname());
         }else {
-            int y =(60-x);
+            int y =(60-x)*2;
             StringBuilder blank= new StringBuilder();
             for (int i = 0; i < y; i++) {
                 blank.append(" ");
@@ -173,14 +145,30 @@ public class PDFUtils {
         }
         Paragraph tile1=new Paragraph(builder.toString(),getFont(8));
         document.add(tile1);
-
-
-        String str2=String.format("报销日期:%s   报销事由:%s  收款方式:%s",
-                mainExpenseAccount.getCreateTimeStr(),mainExpenseAccount.getRemarks(),
-                mainExpenseAccount.getBank() + " " + mainExpenseAccount.getAccounts() + " " +
-                        mainExpenseAccount.getName());
-        Paragraph tile2=new Paragraph(str2,getFont(8));
-        tile2.setAlignment(1);
+        int x2 = CREATETIME.length() + mainExpenseAccount.getCreateTimeStr().length()/2 +
+                REMARKS.length() + mainExpenseAccount.getRemarks().length() +
+                BANK_ACCOUNT.length() + mainExpenseAccount.getBank().length() +
+                mainExpenseAccount.getAccounts().length()/2 + mainExpenseAccount.getName().length();
+        builder.setLength(0);
+        if (x2>55){
+            builder.append(CREATETIME).append(mainExpenseAccount.getCreateTimeStr()).append("          ")
+                    .append(REMARKS).append(mainExpenseAccount.getRemarks()).append("          ")
+                    .append(BANK_ACCOUNT).append(mainExpenseAccount.getBank()).append("  ")
+                    .append(mainExpenseAccount.getAccounts()).append("  ")
+                    .append(mainExpenseAccount.getName());
+        }else {
+            int y =(60-x2)*2;
+            StringBuilder blank= new StringBuilder();
+            for (int i = 0; i < y; i++) {
+                blank.append(" ");
+            }
+            builder.append(CREATETIME).append(mainExpenseAccount.getCreateTimeStr()).append(blank)
+                    .append(REMARKS).append(mainExpenseAccount.getRemarks()).append(blank)
+                    .append(BANK_ACCOUNT).append(mainExpenseAccount.getBank()).append(" ")
+                    .append(mainExpenseAccount.getAccounts()).append(" ")
+                    .append(mainExpenseAccount.getName());
+        }
+        Paragraph tile2=new Paragraph(builder.toString(),getFont(8));
         document.add(tile2);
     }
 

+ 142 - 1
src/main/java/com/goafanti/expenseAccount/service/impl/ExpenseAccountServiceImpl.java

@@ -12,6 +12,7 @@ import com.goafanti.common.enums.NoticeTypes;
 import com.goafanti.common.error.BusinessException;
 import com.goafanti.common.model.*;
 import com.goafanti.common.utils.*;
+import com.goafanti.common.utils.excel.FileUtils;
 import com.goafanti.common.utils.excel.NewExcelUtil;
 import com.goafanti.common.utils.pdf.PDFUtils;
 import com.goafanti.core.mybatis.BaseMybatisDao;
@@ -24,14 +25,25 @@ import com.goafanti.expenseAccount.Enums.EAsecondaryTypes;
 import com.goafanti.expenseAccount.bo.*;
 import com.goafanti.expenseAccount.service.ExpenseAccountService;
 import com.goafanti.organization.bo.OrganizationListOut;
+import com.itextpdf.text.Document;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Paragraph;
+import com.itextpdf.text.Phrase;
+import com.itextpdf.text.pdf.PdfPCell;
+import com.itextpdf.text.pdf.PdfPRow;
+import com.itextpdf.text.pdf.PdfPTable;
+import com.itextpdf.text.pdf.PdfWriter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -436,10 +448,139 @@ public class ExpenseAccountServiceImpl extends BaseMybatisDao<ExpenseAccountMapp
     public Object mainExpenseExport(Integer id, HttpServletResponse response) {
         MainExpenseAccount mainExpenseAccount = mainExpense(id);
         PDFUtils pdfUtils = new PDFUtils();
-        pdfUtils.pushMainExpenseExport(mainExpenseAccount,response,uploadPath);
+        pushMainExpenseExport(mainExpenseAccount,response,uploadPath);
         return null;
     }
 
+    public void pushMainExpenseExport(MainExpenseAccount mainExpenseAccount,HttpServletResponse response, String uploadPath) {
+        String title = "报销明细表";
+        String attName = String.format("报销明细表_%s.pdf",new Date().getTime());
+        String realFileName = uploadPath+"/tmp/"+new Date().getTime() + ".pdf";
+
+        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
+        Document document=new Document();
+        try {
+            FileUtils.setAttachmentResponseHeader(response, title,attName);
+            FileOutputStream outputStream = new FileOutputStream(realFileName);
+            PdfWriter.getInstance(document, outputStream);
+            document.open();
+            Paragraph paragraph = new Paragraph(title, PDFUtils.getTitleFont());
+            paragraph.setAlignment(1);
+            document.add(paragraph);
+            //默认算60
+            PDFUtils.pushExpenseTitle(document,mainExpenseAccount);
+            PdfPTable table = new PdfPTable(12);
+            ArrayList<PdfPRow> rows = table.getRows();
+            float[] columnWidths ={10,16,10,32,32,32,10,16,12,12,28,28};
+            table.setWidths(columnWidths);
+            table.setSpacingBefore(10f);
+            table.setSpacingAfter(10f);
+            PdfPCell[] cells =new PdfPCell[12];
+            PdfPRow row=new PdfPRow(cells);
+            cells[0] = new PdfPCell(new Phrase("序号",PDFUtils.getFont(8)));
+            cells[1] = new PdfPCell(new Phrase("费用时间",PDFUtils.getFont(8)));
+            cells[2] = new PdfPCell(new Phrase("时长",PDFUtils.getFont(8)));
+            cells[3] = new PdfPCell(new Phrase("出差地址",PDFUtils.getFont(8)));
+            cells[4] = new PdfPCell(new Phrase("出差企业",PDFUtils.getFont(8)));
+            cells[5] = new PdfPCell(new Phrase("合同编号",PDFUtils.getFont(8)));
+            cells[6] = new PdfPCell(new Phrase("类型",PDFUtils.getFont(8)));
+            cells[7] = new PdfPCell(new Phrase("详情",PDFUtils.getFont(8)));
+            cells[8] = new PdfPCell(new Phrase("费用",PDFUtils.getFont(8)));
+            cells[9] = new PdfPCell(new Phrase("实报",PDFUtils.getFont(8)));
+            cells[10] = new PdfPCell(new Phrase("出差事由",PDFUtils.getFont(8)));
+            cells[11] = new PdfPCell(new Phrase("报销订单/部门",PDFUtils.getFont(8)));
+            rows.add(row);
+            List<SonExpenseAccount> sonList = mainExpenseAccount.getSonList();
+            //序号
+            int index = 0;
+            for (SonExpenseAccount e : sonList) {
+                List<SonExpenseAccountDetails> detList = e.getDetList();
+                //详情计算,详情完成后需要一个统计
+                int detIndex = 0;
+                for (SonExpenseAccountDetails det : detList) {
+                    index++;
+                    detIndex++;
+                    PdfPCell[] cells1 =new PdfPCell[12];
+                    PdfPRow row1=new PdfPRow(cells1);
+                    cells1[0] = new PdfPCell(new Phrase(String.valueOf(index),PDFUtils.getFont(8)));
+                    cells1[1] = new PdfPCell(new Phrase(det.getAgreeTime(),PDFUtils.getFont(8)));
+                    if (e.getPublicReleaseType()!=null){
+                        cells1[2] = new PdfPCell(new Phrase(e.getDuration(),PDFUtils.getFont(8)));
+                        cells1[3] = new PdfPCell(new Phrase(e.getDistrictName(),PDFUtils.getFont(8)));
+                        cells1[4] = new PdfPCell(new Phrase(e.getUserNames(),PDFUtils.getFont(8)));
+                    }else {
+                        cells1[2] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+                        cells1[3] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+                        cells1[4] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+                    }
+
+                    cells1[5] = new PdfPCell(new Phrase(e.getContractNo(),PDFUtils.getFont(8)));
+                    cells1[6] = new PdfPCell(new Phrase(getExpenseType(e),PDFUtils.getFont(8)));
+                    cells1[7] = new PdfPCell(new Phrase(getExpenseSecondaryType(e),PDFUtils.getFont(8)));
+                    cells1[8] = new PdfPCell(new Phrase(det.getAmount().stripTrailingZeros().toEngineeringString(),PDFUtils.getFont(8)));
+                    cells1[9] = new PdfPCell(new Phrase(det.getRealAmount().stripTrailingZeros().toEngineeringString(),PDFUtils.getFont(8)));
+                    cells1[10] = new PdfPCell(new Phrase(e.getPlan(),PDFUtils.getFont(8)));
+                    if (index==1){
+                        cells1[11] = new PdfPCell(new Phrase(getOrderNoAndDepartment(e,mainExpenseAccount.getApplyDepName()),PDFUtils.getFont(8)));
+                    }else {
+                        cells1[11] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+                    }
+                    rows.add(row1);
+                    if (detIndex==detList.size()){
+                        addCount(e,rows,index);
+                    }
+                }
+            }
+
+
+            document.add(table);
+            document.close();
+            outputStream.close();
+//            FileUtils.writeBytes(realFileName, response.getOutputStream());
+//            FileUtils.deleteFile(realFileName);
+        } catch (DocumentException | IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+    private void addCount(SonExpenseAccount e,ArrayList<PdfPRow> rows,int index) {
+        index++;
+        PdfPCell[] cells =new PdfPCell[12];
+        PdfPRow row=new PdfPRow(cells);
+        cells[0] = new PdfPCell(new Phrase(String.valueOf(index),PDFUtils.getFont(8)));
+        cells[1] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        cells[2] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        cells[3] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        cells[4] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        cells[5] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        cells[6] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        cells[7] = new PdfPCell(new Phrase("小计",PDFUtils.getFont(8)));
+        cells[8] = new PdfPCell(new Phrase(e.getAmount().stripTrailingZeros().toEngineeringString(),PDFUtils.getFont(8)));
+        cells[9] = new PdfPCell(new Phrase(e.getRealAmount().stripTrailingZeros().toEngineeringString(),PDFUtils.getFont(8)));
+        cells[10] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        cells[11] = new PdfPCell(new Phrase("",PDFUtils.getFont(8)));
+        rows.add(row);
+    }
+
+
+    private String getOrderNoAndDepartment(SonExpenseAccount e,String applyDepName) {
+        return e.getOrderNo()+"\n"+applyDepName;
+    }
+
+    private String getExpenseSecondaryType(SonExpenseAccount e) {
+        return EAsecondaryTypes.getDescByCode(e.getSecondaryType());
+    }
+
+    private String getDetType(SonExpenseAccountDetails det) {
+        return null;
+    }
+
+    private String getExpenseType(SonExpenseAccount e) {
+        return EATypes.getDescByCode(e.getType());
+    }
+
+
 
     private void pushResettingDebit(InputExpenseAccount in, Integer debitId) {
         ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(debitId);