itext打印PDF
阿新 • • 發佈:2017-10-30
轉義 ear lai nts ebo evo getc resp jsp
//jsp頁面標簽 <div class="mainQ_time"> <input class="sub btn btn-primary" id="printpdf" type="button" value="打印WORD"> </div>
$("#printpdf").click(function(){ //獲取頁面的文本域輸入項的值 var completion = $("#perf_data1").val(); varstandingTime = $("#perf_data2").val(); var machineBroken = $("#perf_data3").val(); var complaintsClaims = $("#perf_data4").val(); var safetyManagement = $("#perf_data5").val(); var qualityControl = $("#perf_data6").val(); var theDebugging = $("#perf_data7").val();var afterSales = $("#perf_data8").val(); var month = $(".startCreateTime").val(); var a = "\\r\\n"; //換行轉義 completion = completion.replace(/\n|\r\n/g,a); standingTime = standingTime.replace(/\n|\r\n/g,a); machineBroken = machineBroken.replace(/\n|\r\n/g,a); complaintsClaims= complaintsClaims.replace(/\n|\r\n/g,a); safetyManagement = safetyManagement.replace(/\n|\r\n/g,a); qualityControl = qualityControl.replace(/\n|\r\n/g,a); theDebugging = theDebugging.replace(/\n|\r\n/g,a); afterSales = afterSales.replace(/\n|\r\n/g,a); if(month==""){ alert("請選擇月份"); return; } //var monthlyReport = { "completion": completion, "standingTime": standingTime, "machineBroken": machineBroken, "complaintsClaims": complaintsClaims, "safetyManagement": safetyManagement, "qualityControl": qualityControl, "theDebugging": theDebugging, "afterSales": afterSales,"month":month }; //發送GET請求到controller window.location.href ="${basePath}/wa/mq/itext?completion="+completion+"&standingTime="+standingTime+"&machineBroken="+machineBroken+"&complaintsClaims="+complaintsClaims+"&safetyManagement="+safetyManagement+"&qualityControl="+qualityControl+"&theDebugging="+theDebugging+"&afterSales="+afterSales+"&month="+month; });
@RequestMapping(value="/itext",method ={RequestMethod.GET}) @ResponseBody public String itextPDF(MonthlyReport monthlyReport ,HttpServletRequest request,HttpServletResponse response) throws Exception{ //統一的日期處理格式 DateFormat formater1 = new SimpleDateFormat("yyyy/MM/dd"); DateFormat formater = new SimpleDateFormat("MM.dd"); String path = request.getSession().getServletContext().getRealPath("/fileDisk/rep"); //查詢出數據 List<PmMaintenanceplan> listPm = pmMaintenanceplanService.selectByStoppingTime(monthlyReport.getMonth()); List<PmDebugVo> listPd = pmMaintenanceplanService.selectPm(monthlyReport.getMonth()); List<CsTroubleVo> listCstv = csTroubleInfoService.selectGeneralSituation(monthlyReport.getMonth()); List<CsTroubleInfoVo> listCstiv = csTroubleInfoService.selectDetails(monthlyReport.getMonth()); String strFailure= csTroubleInfoService.selectFailureNumber(monthlyReport.getMonth()); //判斷文件是否存在,如果存在就刪除 File file = new File(path+"/word.docx"); if(file.exists()&& file.isFile()){ file.delete(); } Font font = new Font(Font.NORMAL,12, Font.NORMAL); Font font1 = new Font(Font.NORMAL,12, Font.BOLD); //Font font2 = new Font(Font.NORMAL,16, Font.NORMAL); Font font3 = new Font( Font.NORMAL,10, Font.NORMAL); Document document = new Document(PageSize.A4); RtfWriter2.getInstance(document, new FileOutputStream(path + "/word.docx")); document.open(); Paragraph p = new Paragraph("標題",new Font(Font.NORMAL, 18, Font.BOLD, new Color(0, 0, 0)) ); //段落對齊方式 p.setAlignment(1); document.add(p); String yearMonth = monthlyReport.getMonth().replace("-", "年"); Paragraph p2 = new Paragraph("("+yearMonth+"月)", font3); //設置一個段落前後的間距。 p2.setSpacingAfter(35); p2.setSpacingBefore(8); p2.setAlignment(1); document.add(p2); Paragraph t1=new Paragraph("一、*****:",font1); t1.setSpacingAfter(4); t1.setSpacingBefore(4); document.add(t1); String completion = monthlyReport.getCompletion(); completion = completion.replaceAll("\\\\r\\\\n", "\n"); Paragraph tt1=new Paragraph(completion,font); tt1.setSpacingAfter(4); tt1.setSpacingBefore(4); document.add(tt1); Paragraph t2=new Paragraph("二、*****:",font1); t2.setSpacingAfter(4); t2.setSpacingBefore(4); document.add(t2); Paragraph tt2=new Paragraph(monthlyReport.getStandingTime().replaceAll("\\\\r\\\\n", "\n"),font); tt2.setSpacingAfter(8); tt2.setSpacingBefore(4); document.add(tt2); String str = monthlyReport.getMonth().substring(monthlyReport.getMonth().length()-2,monthlyReport.getMonth().length()); Paragraph p3 = new Paragraph("表1 ***"+str+"月份完成情況", font); p3.setSpacingAfter(4); p3.setSpacingBefore(4); p3.setAlignment(1); document.add(p3); Color lightGrey = new Color(0xd0,0xd0,0xd0); Table table1 = new Table(6); Cell cell11 = new Cell("序號"); Cell cell12 = new Cell("**"); Cell cell13 = new Cell("**"); Cell cell14 = new Cell("**"); Cell cell15 = new Cell("**"); Cell cell16 = new Cell("**"); //水平居中 cell11.setHorizontalAlignment(Element.ALIGN_CENTER); //垂直居中 cell11.setVerticalAlignment(Element.ALIGN_MIDDLE); cell12.setHorizontalAlignment(Element.ALIGN_CENTER); cell12.setVerticalAlignment(Element.ALIGN_MIDDLE); cell13.setHorizontalAlignment(Element.ALIGN_CENTER); cell13.setVerticalAlignment(Element.ALIGN_MIDDLE); cell14.setHorizontalAlignment(Element.ALIGN_CENTER); cell14.setVerticalAlignment(Element.ALIGN_MIDDLE); cell15.setHorizontalAlignment(Element.ALIGN_CENTER); cell15.setVerticalAlignment(Element.ALIGN_MIDDLE); cell16.setHorizontalAlignment(Element.ALIGN_CENTER); cell16.setVerticalAlignment(Element.ALIGN_MIDDLE); //設置背景顏色,在上方定義了這個顏色變量 cell11.setBackgroundColor(lightGrey); cell12.setBackgroundColor(lightGrey); cell13.setBackgroundColor(lightGrey); cell14.setBackgroundColor(lightGrey); cell15.setBackgroundColor(lightGrey); cell16.setBackgroundColor(lightGrey); table1.addCell(cell11); table1.addCell(cell12); table1.addCell(cell13); table1.addCell(cell14); table1.addCell(cell15); table1.addCell(cell16); //document.add(table1); // 添加表頭的元素 Integer q=1; for(PmMaintenanceplan pm : listPm){ String num = q.toString(); Cell cell111 = new Cell(num); String realdeliverytime = formater1.format(pm.getRealdeliverytime()); //Cell cell111 = new Cell(""); Cell cell121 = new Cell(pm.getLocamotivetype()); Cell cell131 = new Cell(pm.getLocamotivenum()); Cell cell141 = new Cell(pm.getProcess()); Cell cell151 = new Cell(pm.getPlanstopnum().toString()); Cell cell161 = new Cell(realdeliverytime); cell111.setHorizontalAlignment(Element.ALIGN_CENTER); cell111.setVerticalAlignment(Element.ALIGN_MIDDLE); cell121.setHorizontalAlignment(Element.ALIGN_CENTER); cell121.setVerticalAlignment(Element.ALIGN_MIDDLE); cell131.setHorizontalAlignment(Element.ALIGN_CENTER); cell131.setVerticalAlignment(Element.ALIGN_MIDDLE); cell141.setHorizontalAlignment(Element.ALIGN_CENTER); cell141.setVerticalAlignment(Element.ALIGN_MIDDLE); cell151.setHorizontalAlignment(Element.ALIGN_CENTER); cell151.setVerticalAlignment(Element.ALIGN_MIDDLE); cell161.setHorizontalAlignment(Element.ALIGN_CENTER); cell161.setVerticalAlignment(Element.ALIGN_MIDDLE); table1.addCell(cell111); table1.addCell(cell121); table1.addCell(cell131); table1.addCell(cell141); table1.addCell(cell151); table1.addCell(cell161); q = Integer.parseInt(num); q=q+1; } document.add(table1); Paragraph t3=new Paragraph("三、****:",font1); t3.setSpacingAfter(4); t3.setSpacingBefore(8); document.add(t3); //用於換行 Paragraph tt3=new Paragraph(monthlyReport.getMachineBroken().replaceAll("\\\\r\\\\n", "\n"),font); tt3.setSpacingAfter(4); tt3.setSpacingBefore(4); document.add(tt3); Paragraph t4=new Paragraph("四、****:",font1); t4.setSpacingAfter(4); t4.setSpacingBefore(4); document.add(t4); Paragraph tt4=new Paragraph(monthlyReport.getComplaintsClaims().replaceAll("\\\\r\\\\n", "\n"),font); tt4.setSpacingAfter(4); tt4.setSpacingBefore(4); document.add(tt4); Paragraph t5=new Paragraph("五、****:",font1); t5.setSpacingAfter(4); t5.setSpacingBefore(4); document.add(t5); Paragraph tt5=new Paragraph(monthlyReport.getSafetyManagement().replaceAll("\\\\r\\\\n", "\n"),font); tt5.setSpacingAfter(4); tt5.setSpacingBefore(4); document.add(tt5); Paragraph t6=new Paragraph("六、****:",font1); t6.setSpacingAfter(4); t6.setSpacingBefore(4); document.add(t6); Paragraph t7=new Paragraph("1)***:",font); t7.setSpacingAfter(4); t7.setSpacingBefore(4); document.add(t7); Paragraph tt7=new Paragraph(monthlyReport.getQualityControl().replaceAll("\\\\r\\\\n", "\n"),font); tt7.setSpacingAfter(4); tt7.setSpacingBefore(4); document.add(tt7); Paragraph t8=new Paragraph("2) ***:",font); t8.setSpacingAfter(4); t8.setSpacingBefore(4); document.add(t8); Paragraph tt8=new Paragraph(monthlyReport.getTheDebugging().replaceAll("\\\\r\\\\n", "\n"),font); tt8.setSpacingAfter(8); tt8.setSpacingBefore(4); document.add(tt8); Paragraph p4 = new Paragraph("表2 "+str+"月份***", font); p4.setSpacingAfter(4); p4.setSpacingBefore(4); p4.setAlignment(1); document.add(p4); Table table2 = new Table(9); Cell cell21 = new Cell("序號"); Cell cell22 = new Cell("**"); Cell cell23 = new Cell("**"); Cell cell24 = new Cell("**"); Cell cell25 = new Cell("**"); Cell cell26 = new Cell("**"); Cell cell27 = new Cell("**"); Cell cell28 = new Cell("**"); Cell cell29 = new Cell("**"); cell21.setHorizontalAlignment(Element.ALIGN_CENTER); cell21.setVerticalAlignment(Element.ALIGN_MIDDLE); cell22.setHorizontalAlignment(Element.ALIGN_CENTER); cell22.setVerticalAlignment(Element.ALIGN_MIDDLE); cell23.setHorizontalAlignment(Element.ALIGN_CENTER); cell23.setVerticalAlignment(Element.ALIGN_MIDDLE); cell24.setHorizontalAlignment(Element.ALIGN_CENTER); cell24.setVerticalAlignment(Element.ALIGN_MIDDLE); cell25.setHorizontalAlignment(Element.ALIGN_CENTER); cell25.setVerticalAlignment(Element.ALIGN_MIDDLE); cell26.setHorizontalAlignment(Element.ALIGN_CENTER); cell26.setVerticalAlignment(Element.ALIGN_MIDDLE); cell27.setHorizontalAlignment(Element.ALIGN_CENTER); cell27.setVerticalAlignment(Element.ALIGN_MIDDLE); cell28.setHorizontalAlignment(Element.ALIGN_CENTER); cell28.setVerticalAlignment(Element.ALIGN_MIDDLE); cell29.setHorizontalAlignment(Element.ALIGN_CENTER); cell29.setVerticalAlignment(Element.ALIGN_MIDDLE); cell21.setBackgroundColor(lightGrey); cell22.setBackgroundColor(lightGrey); cell23.setBackgroundColor(lightGrey); cell24.setBackgroundColor(lightGrey); cell25.setBackgroundColor(lightGrey); cell26.setBackgroundColor(lightGrey); cell27.setBackgroundColor(lightGrey); cell28.setBackgroundColor(lightGrey); cell29.setBackgroundColor(lightGrey); table2.addCell(cell21); table2.addCell(cell22); table2.addCell(cell23); table2.addCell(cell24); table2.addCell(cell25); table2.addCell(cell26); table2.addCell(cell27); table2.addCell(cell28); table2.addCell(cell29); Integer i=1; //這是我自己所需要展示的數據, for(PmDebugVo pd : listPd){ String num = i.toString(); String startTime = formater.format(pd.getStartTime()); String endTime = formater.format(pd.getEndTime()); Cell cell211 = new Cell(num); Cell cell221 = new Cell(pd.getFollowUsername()); Cell cell231 = new Cell(startTime); Cell cell241 = new Cell(endTime); String reporteStage; if("1".equals(pd.getReporteStage())){ reporteStage="呵呵"; }else if("2".equals(pd.getReporteStage())){ reporteStage="哈哈"; }else if("3".equals(pd.getReporteStage())){ reporteStage="嘿嘿"; }else{ reporteStage=pd.getReporteStage(); } Cell cell251 = new Cell(pd.getLocamotiveType()+pd.getLocamotiveNum()+reporteStage); Cell cell261 = new Cell(pd.getBureau()); Cell cell271 = new Cell(pd.getProcess()); Cell cell281 = new Cell(pd.getProblem()); Cell cell291 = new Cell(pd.getMatter()); cell211.setHorizontalAlignment(Element.ALIGN_CENTER); cell211.setVerticalAlignment(Element.ALIGN_MIDDLE); cell221.setHorizontalAlignment(Element.ALIGN_CENTER); cell221.setVerticalAlignment(Element.ALIGN_MIDDLE); cell231.setHorizontalAlignment(Element.ALIGN_CENTER); cell231.setVerticalAlignment(Element.ALIGN_MIDDLE); cell241.setHorizontalAlignment(Element.ALIGN_CENTER); cell241.setVerticalAlignment(Element.ALIGN_MIDDLE); cell251.setHorizontalAlignment(Element.ALIGN_CENTER); cell251.setVerticalAlignment(Element.ALIGN_MIDDLE); cell261.setHorizontalAlignment(Element.ALIGN_CENTER); cell261.setVerticalAlignment(Element.ALIGN_MIDDLE); cell271.setHorizontalAlignment(Element.ALIGN_CENTER); cell271.setVerticalAlignment(Element.ALIGN_MIDDLE); cell281.setHorizontalAlignment(Element.ALIGN_CENTER); cell281.setVerticalAlignment(Element.ALIGN_MIDDLE); cell291.setHorizontalAlignment(Element.ALIGN_CENTER); cell291.setVerticalAlignment(Element.ALIGN_MIDDLE); table2.addCell(cell211); table2.addCell(cell221); table2.addCell(cell231); table2.addCell(cell241); table2.addCell(cell251); table2.addCell(cell261); table2.addCell(cell271); table2.addCell(cell281); table2.addCell(cell291); i = Integer.parseInt(num); i=i+1; } document.add(table2); Paragraph t9=new Paragraph("3) ***:",font); t9.setSpacingAfter(4); t9.setSpacingBefore(8); document.add(t9); Paragraph tt9=new Paragraph(strFailure.replaceAll("\\\\r\\\\n", "\n"),font); tt9.setSpacingAfter(4); tt9.setSpacingBefore(4); document.add(tt9); Paragraph p5 = new Paragraph("表3 ****", font); p5.setSpacingAfter(4); p5.setSpacingBefore(4); p5.setAlignment(1); document.add(p5); Table table3 = new Table(4); Cell cell31 = new Cell("序號"); Cell cell32 = new Cell("**"); Cell cell33 = new Cell("**"); Cell cell34 = new Cell("**"); cell31.setHorizontalAlignment(Element.ALIGN_CENTER); cell31.setVerticalAlignment(Element.ALIGN_MIDDLE); cell32.setHorizontalAlignment(Element.ALIGN_CENTER); cell32.setVerticalAlignment(Element.ALIGN_MIDDLE); cell33.setHorizontalAlignment(Element.ALIGN_CENTER); cell33.setVerticalAlignment(Element.ALIGN_MIDDLE); cell34.setHorizontalAlignment(Element.ALIGN_CENTER); cell34.setVerticalAlignment(Element.ALIGN_MIDDLE); cell31.setBackgroundColor(lightGrey); cell32.setBackgroundColor(lightGrey); cell33.setBackgroundColor(lightGrey); cell34.setBackgroundColor(lightGrey); table3.addCell(cell31); table3.addCell(cell32); table3.addCell(cell33); table3.addCell(cell34); Integer j=1; /*for(CsTroubleVo cstv : listCstv){ String num = j.toString(); table3.addCell(new Cell(num)); table3.addCell(new Cell(cstv.getModel())); table3.addCell(new Cell(cstv.getPeriod())); String num1 = String.valueOf(cstv.getFailureNum()); table3.addCell(new Cell(num1)); j = Integer.parseInt(num); j=j+1; }*/ int m = 0; for(int n = 0;n<listCstv.size();n++){ String num = j.toString(); Cell cell311 = new Cell(num); Cell cell321 = new Cell(listCstv.get(n).getModel()); Cell cell331 = new Cell(listCstv.get(n).getPeriod()); Cell cell341 = new Cell(String.valueOf(listCstv.get(n).getFailureNum())); cell311.setHorizontalAlignment(Element.ALIGN_CENTER); cell311.setVerticalAlignment(Element.ALIGN_MIDDLE); cell321.setHorizontalAlignment(Element.ALIGN_CENTER); cell321.setVerticalAlignment(Element.ALIGN_MIDDLE); cell331.setHorizontalAlignment(Element.ALIGN_CENTER); cell331.setVerticalAlignment(Element.ALIGN_MIDDLE); cell341.setHorizontalAlignment(Element.ALIGN_CENTER); cell341.setVerticalAlignment(Element.ALIGN_MIDDLE); if(listCstv.size()==1){ table3.addCell(cell311); table3.addCell(cell321); table3.addCell(cell331); table3.addCell(cell341); }else if(n<listCstv.size()-1){ if(listCstv.get(m).getModel().equals(listCstv.get(n+1).getModel())){ cell311.setRowspan(2); table3.addCell(cell311); cell321.setRowspan(2); table3.addCell(cell321); table3.addCell(cell331); table3.addCell(cell341); }else{ m=n+1; table3.addCell(cell311); table3.addCell(cell321); table3.addCell(cell331); table3.addCell(cell341); j = Integer.parseInt(num); j=j+1; } }else if(n==listCstv.size()-1){ if(listCstv.get(n).getModel().equals(listCstv.get(n-1).getModel())){ table3.addCell(cell331); table3.addCell(cell341); }else{ table3.addCell(cell311); table3.addCell(cell321); table3.addCell(cell331); table3.addCell(cell341); } }else{ table3.addCell(cell331); table3.addCell(cell341); } /*table3.addCell(new Cell(num)); table3.addCell(cell312); table3.addCell(new Cell(listCstv.get(n).getPeriod())); String num1 = String.valueOf(listCstv.get(n).getFailureNum()); table3.addCell(new Cell(num1));*/ } document.add(table3); Paragraph p6 = new Paragraph("表4 ****", font); p6.setSpacingAfter(4); p6.setSpacingBefore(4); p6.setAlignment(1); document.add(p6); //表四 Table table4 = new Table(6); Cell cell41 = new Cell("序號"); Cell cell42 = new Cell("**"); Cell cell43 = new Cell("**"); Cell cell44 = new Cell("**"); Cell cell45 = new Cell("**"); Cell cell46 = new Cell("**"); cell41.setHorizontalAlignment(Element.ALIGN_CENTER); cell41.setVerticalAlignment(Element.ALIGN_MIDDLE); cell42.setHorizontalAlignment(Element.ALIGN_CENTER); cell42.setVerticalAlignment(Element.ALIGN_MIDDLE); cell43.setHorizontalAlignment(Element.ALIGN_CENTER); cell43.setVerticalAlignment(Element.ALIGN_MIDDLE); cell44.setHorizontalAlignment(Element.ALIGN_CENTER); cell44.setVerticalAlignment(Element.ALIGN_MIDDLE); cell45.setHorizontalAlignment(Element.ALIGN_CENTER); cell45.setVerticalAlignment(Element.ALIGN_MIDDLE); cell46.setHorizontalAlignment(Element.ALIGN_CENTER); cell46.setVerticalAlignment(Element.ALIGN_MIDDLE); cell41.setBackgroundColor(lightGrey); cell42.setBackgroundColor(lightGrey); cell43.setBackgroundColor(lightGrey); cell44.setBackgroundColor(lightGrey); cell45.setBackgroundColor(lightGrey); cell46.setBackgroundColor(lightGrey); table4.addCell(cell41); table4.addCell(cell42); table4.addCell(cell43); table4.addCell(cell44); table4.addCell(cell45); table4.addCell(cell46); Integer k = 1; for(CsTroubleInfoVo cstiv : listCstiv){ String num = k.toString(); String troubleDate = formater1.format(cstiv.getTroubleDate()); Cell cell411 = new Cell(num); Cell cell421 = new Cell(cstiv.getTrainModelNumber()); Cell cell431 = new Cell(cstiv.getPeriod()); Cell cell441 = new Cell(troubleDate); Cell cell451 = new Cell(cstiv.getTroubleDescription()); Cell cell461 = new Cell(cstiv.getProcessDescription()); cell411.setHorizontalAlignment(Element.ALIGN_CENTER); cell411.setVerticalAlignment(Element.ALIGN_MIDDLE); cell421.setHorizontalAlignment(Element.ALIGN_CENTER); cell421.setVerticalAlignment(Element.ALIGN_MIDDLE); cell431.setHorizontalAlignment(Element.ALIGN_CENTER); cell431.setVerticalAlignment(Element.ALIGN_MIDDLE); cell441.setHorizontalAlignment(Element.ALIGN_CENTER); cell441.setVerticalAlignment(Element.ALIGN_MIDDLE); cell451.setHorizontalAlignment(Element.ALIGN_CENTER); cell451.setVerticalAlignment(Element.ALIGN_MIDDLE); cell461.setHorizontalAlignment(Element.ALIGN_CENTER); cell461.setVerticalAlignment(Element.ALIGN_MIDDLE); table4.addCell(cell411); table4.addCell(cell421); table4.addCell(cell431); table4.addCell(cell441); table4.addCell(cell451); table4.addCell(cell461); k = Integer.parseInt(num); k=k+1; } document.add(table4); // 5.關閉文檔 對象 document.close(); //將生成的文檔下載傳遞到客戶端 response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName="+"word.doc"); try { InputStream inputStream = new FileInputStream(new File(path+"/word.docx")); OutputStream os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } // 這裏主要關閉。 os.close(); inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
itext打印PDF