private Response downloadMorePdf(List<Integer> billIdList) throws Exception { Response response = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); List<String> list = new ArrayList<String>(); for (int i = 0; i < billIdList.size(); i++) { Integer billId = billIdList.get(i); Map<String, Object> templateMap = getTemplateMap(billId); if (templateMap == null || templateMap.isEmpty()) { logger.info("账单数据不完整,无法下载账单"); continue; } StringBuffer fileNameStr = new StringBuffer(); Bill bill = billMapper.selectByPrimaryKey(billId); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); fileNameStr .append(bill.getAgencyNickName()) .append(sdf.format(bill.getBillStartTime())) .append("-") .append(sdf.format(bill.getBillEndTime())) .append(".") .append(".pdf"); // classpath 中模板路径,根据html模版来生成pdf String template = "pdfConfig/templates/test.html"; PdfDocumentGenerator pdfGenerator = new PdfDocumentGenerator(); // 生成pdf try { pdfGenerator.generate(template, getTemplateMap(billId), bos); } catch (DocumentGeneratingException e) { e.printStackTrace(); } list.add(fileNameStr.toString()); } zipFiles(list, bos); String zipFileName = date2String(new Date()) + ".zip"; response = Response.status(Status.OK) .header("Content-Type", "application/zip;charset=ISO8859-1") .header("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"") .entity(bos.toByteArray()) .build(); return response; }
private Map<String, Object> getTemplateMap(Integer billId) { Map<String, Object> root = new HashMap<String, Object>(); PreviewInfoVO previewInfoVO = billMapper.selectPreviewInfo(billId); if (previewInfoVO == null) { return root; } previewInfoVO.setNullFields(); root.put("previewInfoVO", previewInfoVO); BillInformationExample billInformationExample = new BillInformationExample(); billInformationExample.createCriteria().andBillIdEqualTo(billId); billInformationExample.setOrderByClause("id asc"); List<BillInformation> billInformationList = billInformationMapper.selectByExample(billInformationExample); if (billInformationList != null && billInformationList.size() == 4) { // billInformationList = new ArrayList<BillInformation>(); // BillInformation info1 = new BillInformation(); // info1.setFeeType("流量费"); // billInformationList.add(info1); // // BillInformation info2 = new BillInformation(); // info2.setFeeType("服务费"); // billInformationList.add(info2); // // BillInformation info3 = new BillInformation(); // info3.setFeeType("名单费"); // billInformationList.add(info3); // // BillInformation info4 = new BillInformation(); // info3.setFeeType("其他费用"); // billInformationList.add(info4); } Integer consumeNumber = billInformationList.get(0).getConsumeNumber(); consumeNumber = consumeNumber == null ? new Integer(0) : consumeNumber; billInformationList.get(0).setConsumeNumber(consumeNumber); root.put("info1", billInformationList.get(0)); root.put("info2", billInformationList.get(1)); root.put("info3", billInformationList.get(2)); root.put("info4", billInformationList.get(3)); root.put("imagePath", ResourceLoader.getPath("pdfConfig/images")); return root; }
private Response downloadOnePdf(final @QueryParam("billId") Integer billId) throws Exception { Response response = null; Map<String, Object> templateMap = getTemplateMap(billId); if (templateMap == null || templateMap.isEmpty()) { logger.info("账单数据不完整,无法下载账单"); return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); StringBuffer fileNameStr = new StringBuffer(); Bill bill = billMapper.selectByPrimaryKey(billId); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); fileNameStr .append(bill.getAgencyNickName()) .append(sdf.format(bill.getBillStartTime())) .append("-") .append(sdf.format(bill.getBillEndTime())) .append("."); fileNameStr.append(".pdf"); String fileName = new String(fileNameStr.toString().getBytes("GBK"), "ISO8859-1"); // classpath 中模板路径,根据html模版来生成pdf String template = "pdfConfig/templates/test.html"; PdfDocumentGenerator pdfGenerator = new PdfDocumentGenerator(); // 生成pdf try { pdfGenerator.generate(template, templateMap, bos); } catch (DocumentGeneratingException e) { e.printStackTrace(); } response = Response.status(Response.Status.OK) .header("Content-Type", "application/pdf;charset=ISO8859-1") .header("Content-Disposition", "attachment; filename=\"" + fileName + "\"") .entity(bos.toByteArray()) .build(); bos.close(); return response; }