/** * 下载打印PDF * @param request * @param response * @throws ServletException * @throws IOException * @throws DocumentException * @throws com.itextpdf.text.DocumentException * @throws com.itextpdf.text.DocumentException */ @RequestMapping(value="/downPrintPDF") public void downPrintPDF(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, DocumentException, com.itextpdf.text.DocumentException { SessionUserPojo user=CommonController.getUserInfo(request); // 禁止数据缓存。 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // 流程id String procInstanceId=request.getParameter("procInstanceId"); request.setCharacterEncoding("UTF-8"); // boolean checkPermissions=this.examTaskService.checkPermissions(user.getLoginCode(), procInstanceId); ExternalCuringParam taskExt=(ExternalCuringParam)this.examTaskService.getTaskDetail(procInstanceId,user); //查询附件列表// List attachmentList=this.examTaskService.getFileListByProcessInstanceId(Pid); //查询备注列表 List tontentList=this.examTaskService.getListByPid(procInstanceId); String filePath = request.getSession().getServletContext().getRealPath(STATIC_FILE_PATH); File file_floder =new File(filePath); //如果文件夹不存在则创建 if (!file_floder .exists() && !file_floder .isDirectory()) { file_floder .mkdir(); } File file = null; //创建file对象 if(ProcessType.RCBXSQ.getType().equals(taskExt.getProcInstanceType())){ file = createPrintRcbxPdf(filePath+"\\MOLBASE_办公管理系统_"+user.getLoginCode().trim()+".pdf",taskExt,tontentList,user); }else if(ProcessType.CCBXSQ.getType().equals(taskExt.getProcInstanceType())){ file = createPrintCcbxPdf(filePath+"\\MOLBASE_办公管理系统_"+user.getLoginCode().trim()+".pdf",taskExt,tontentList,user); } //设置response的编码方式 response.setContentType("application/x-msdownload"); // 文件尺寸 response.setContentLength((int)file.length()); String filename = new String(file.getName().getBytes("GB2312"), "ISO_8859_1"); response.setHeader("Content-Disposition","attachment;filename=" + filename); response.setHeader("Pragma","no-cache"); OutputStream myout = response.getOutputStream(); if(file.exists()){ FileInputStream fis = new FileInputStream(file); BufferedInputStream buff = new BufferedInputStream(fis); byte [] b = new byte[1024]; long k = 0; while(k < file.length()){ int j = buff.read(b,0,1024); k += j; myout.write(b,0,j); myout.flush(); } buff.close(); } myout.close(); } @SuppressWarnings("static-access") private File createPrintCcbxPdf(String filepath,ExternalCuringParam taskExt,List tontentList,SessionUserPojo user) throws DocumentException, com.itextpdf.text.DocumentException, IOException{ BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font headfont = new Font(bfChinese, 13, Font.BOLD);// 设置字体大小// Font keyfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小 Font textfont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小// Font textfont_underline = new Font(bfChinese, 10, Font.UNDERLINE);// 设置字体大小// Font textfont1 = new Font(bfChinese, 12, Font.ITALIC);// 设置字体大小 // 1.新建document对象 // 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。 //Document document = new Document(PageSize.A4, 50, 50, 50, 50); com.itextpdf.text.Document document = new com.itextpdf.text.Document(); //add Chinese font //BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //Font headfont=new Font(bfChinese,10,Font.BOLD); //Font keyfont=new Font(bfChinese,8,Font.BOLD); //Font textfont=new Font(bfChinese,8,Font.NORMAL); // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。 // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。// PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\MOLBASE 办公管理系统_"+user.getLoginCode()+".pdf")); //Create Writer associated with document File file = new File(filepath); if(file.exists()){ file.delete(); } file.createNewFile(); com.itextpdf.text.pdf.PdfWriter writer = com.itextpdf.text.pdf.PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setViewerPreferences(writer.HideToolbar); // 3.打开文档 document.open(); // 4.向文档中添加内容 // 标题// document.addTitle("Hello mingri example"); // 作者// document.addAuthor("wolf"); // 主题// document.addSubject("This exam/ple explains how to add metadata.");// document.addKeywords("iText, Hello mingri");// document.addCreator("My program using iText"); // document.newPage(); // 向文档中添加内容// document.add(new Paragraph("\n")); // 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落// document.add(new Paragraph("First page of the document."));// document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD))); // document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD)));// document.add(new Paragraph("xx租赁有限公司\n\n\n", textfont)); Paragraph title = new Paragraph(taskExt.getProcInstanceName()+"\n", headfont); title.setAlignment(Element.ALIGN_CENTER); document.add(title); document.add(new Paragraph("\n",new Font(bfChinese, 8, Font.NORMAL))); // 增加表格 PdfPTable table1 = createTable(new float[]{15,35,15,35}, true, null); table1.completeRow(); table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table1.addCell(createCell("发起人", textfont, Element.ALIGN_CENTER, 1, 1, true)); table1.addCell(createCell(taskExt.getOwnerName()+"/"+taskExt.getDeptName()+"_"+taskExt.getStartTime().split(" ")[0], textfont, Element.ALIGN_CENTER, 1, 1, true)); table1.addCell(createCell("流程编号", textfont, Element.ALIGN_CENTER, 1, 1, true)); table1.addCell(createCell(taskExt.getProcInstanceCode(), textfont, Element.ALIGN_CENTER, 1, 1, true)); document.add(table1); document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL))); PdfPTable table1_1 = createTable(new float[]{15,85}, true, null); table1_1.completeRow(); table1_1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table1_1.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table1_1.addCell(createCell("备注", textfont, Element.ALIGN_CENTER, 1, 1, true)); table1_1.addCell(createCell(taskExt.getStartContent(), textfont, Element.ALIGN_LEFT, 1, 1, true)); document.add(table1_1); document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL))); String process_param = taskExt.getProcess_param(); JSONObject obj=JSONObject.parseObject(process_param); PdfPTable table2 = createTable(new float[]{15,35,15,35}, true, null); table2.completeRow(); table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table2.addCell(createCell("报销人", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell(obj.getString("expense_person_name"), textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("报销人部门", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell(obj.getString("expense_person_dept_name"), textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("费用公司", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell(obj.getString("expense_company"), textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("费用部门", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell(obj.getString("expense_dept_name"), textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("出发日期", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell(obj.getString("startOutDate"), textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("返回日期", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell(obj.getString("endOutDate"), textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("费用合计", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell(obj.getString("expense_money_total"), textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, true)); table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, true)); document.add(table2); document.add(new Paragraph("\n",new Font(bfChinese, 4, Font.NORMAL))); // 增加表格 PdfPTable table = createTable(new float[]{14,10,10,22,10,34}, true, null); table.completeRow(); //出差城市 出差天数 单据数 费用项目 单据金额 费用说明 table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, 1, false)); table.addCell(createCell("出差城市", textfont, Element.ALIGN_CENTER, 1, 1, true)); table.addCell(createCell("出差天数", textfont, Element.ALIGN_CENTER, 1, 1, true)); table.addCell(createCell("单据数", textfont, Element.ALIGN_CENTER, 1, 1, true)); table.addCell(createCell("费用项目", textfont, Element.ALIGN_CENTER, 1, 1, true)); table.addCell(createCell("单据金额", textfont, Element.ALIGN_CENTER, 1, 1, true)); table.addCell(createCell("费用说明", textfont, Element.ALIGN_CENTER, 1, 1, true)); JSONArray arr1=obj.getJSONArray("trip_expense_message"); Iterator