コード例 #1
0
  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;
  }
コード例 #2
0
  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;
  }