How to sent a generated pdf file by jasper report as an e-mail attachment

时间:2019-05-31 11:44:09

标签: java spring pdf jasper-reports

I need to know how generate pdf file using jasper report. After that, I will send this pdf as an attachment to e-mail.

here is the code from my Controller. My controller method takes parameters HttpServletRequest req and HttpServletResponse res. And works like how I generate pdf to people when they want to download to their bills.

    JasperPrint result=null;

    if(req.getParameter("orderId")!=null)
    {


    Integer orderId = new Integer(req.getParameter("orderId"));


        HashMap argMap = new HashMap();

         argMap.put(CustomerKeys.CUSTOMER_ID,req.getSession().getAttribute("userid"));
         argMap.put(BillKeys.BILL_ID,orderId);

         try{

         result = (JasperPrint)CoreServerService.callService("Bill_getJasperBill",argMap);

         if(result!=null)
         {
             OutputStream contentStream = arg1.getOutputStream();

             ByteArrayOutputStream baos =new ByteArrayOutputStream();
             arg1.setContentType("application/x-pdf");
             arg1.setHeader( "Content-Disposition", "attachment; filename=\"" + result.getName() + "\"" );           

             JasperExportManager.exportReportToPdfStream(result,baos);
             arg1.setContentLength(baos.size());
             baos.writeTo(contentStream);
             contentStream.flush();

         }


         }
         catch(Exception ex)
         {
             ex.printStackTrace();
             return null;
         }

And now I want to sent people those pdfs in e-mail. So I can't use HttpServletRequest req and HttpServletResponse res, because I have to do this automatically for every user. (not using anyone's http request).

So, I think I should use jasper report to generate pdf file and I will save that pdf files at server. Then I will add them to e-mail like attachment. At this point I need your opinion. I can generate jasper report now. What should be my next move? Are there any posibilities to do that task with another way?

0 个答案:

没有答案