Thursday, February 12, 2015

Java Struts2 Spring: Create PDF in ByteStream and Attach in Email

   private void ssNumIssuedEmail(String applNm, String memNum, String dobth, String email, String purpose) throws Exception {  
     try {  
         String from = "";  
         String to = email;  
         String userid = "userid";  
         String password = "pass";  
         tranNo = (String)request.getSession().getAttribute("tranNo");  
         Date systemDate = registrationFacade.getRcsFacade().getLibrariesDao().getSystemDate();          
         SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy hh:mm:ss a");  
         String formatedDate = sdf.format(systemDate);  
         Properties properties = new Properties();  
         properties.load(getClass().getClassLoader().getResourceAsStream("email.properties"));  
         Session mailSession = null;  
         try{  
         final String emailUserNm = properties.getProperty("mail.user");  
         final String emailPassword = properties.getProperty("mail.password");  
         mailSession = Session.getInstance(properties,  
           new javax.mail.Authenticator() {  
             protected PasswordAuthentication getPasswordAuthentication() {  
             return new PasswordAuthentication(emailUserNm, emailPassword);  
           }  
         });  
         }catch(Exception e){  
           e.printStackTrace();  
         }  
         from = properties.getProperty("mail.user");            
         MimeMessage message = new MimeMessage(mailSession);  
         message.setFrom(new InternetAddress(from, "Application Confirmation"));  
         message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));  
         message.setSubject("SSS Number Application Confirmation");  
         message.setSentDate(systemDate);  
         BodyPart messageBodyPart = new MimeBodyPart();  
         messageBodyPart.setContent("<div style=\"font-family:Tahoma, Arial, Helvetica, sans-serif;font-size:12px\">Good day <b>" + applNm + "</b>!<br/>" +  
                       "<p>Please be informed that you have successfully applied for GOCC Membership number online. Below is your SS number with your application details:</p><br/>" +  
                       "<p>SS Number: <b>" + memNum + "</b></p>" +  
                       "<p>Date and Time of Application: <b>" + formatedDate + "</b></p>" +  
                       "<p>Transaction No: <b>" + tranNo +"</b></p>" +                                             
                       "<p>Purpose of Application: <b>" + purpose + "</b></p><br/>" +  
             "<p>Kindly go to the nearest Branch for submission of the required supporting documents and issuance of the Personal Record Form. </p>" +  
             "<p>We would like to inform you also of user id and password to our Website Account, as follows:</p>" +  
             "<p>user id: <b>" + userid + "</b></p>" +  
             "<p>password: <b>" + password + "</b></p><br/><br/>" +  
             "<p>Thank you for using the GOCC Website!</p>" +  
             "<p><i>This is a system generated email. Please do not reply.</i></p>",  
             "text/html");  
         Multipart multipart = new MimeMultipart();  
         ByteArrayInputStream bais = createSlip();  
         String fileNameOfSource = "Membership Confirmation Slip";  
         multipart.addBodyPart(messageBodyPart);  
         messageBodyPart = new MimeBodyPart();  
         DataSource attachment = new ByteArrayDataSource(bais, "application/pdf");  
         messageBodyPart.setDataHandler(new DataHandler(attachment));  
         messageBodyPart.setFileName(fileNameOfSource);  
         multipart.addBodyPart(messageBodyPart);  
         message.setContent(multipart);  
         Transport.send(message);  
       }  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
   }  

   public ByteArrayInputStream createSlip(){  
     try{  
       ssNum = "3427898025";  
       rcsUtils = new RCSUtils();        
       memberStaticBean = registrationFacade.getRcsFacade().getRcStaticDao().getInfoSummary(ssNum);  
       ssNum = rcsUtils.formatSsNumber(ssNum);  
       applNm = memberStaticBean.getMemberName();  
       dobth = memberStaticBean.getDobth();  
       ByteArrayOutputStream baos = new ByteArrayOutputStream();  
       Document document = new Document(PageSize.A6);  
       document.addAuthor("GOCC Author");  
       document.addCreationDate();  
       document.addCreator("GOCC Author");  
       document.addTitle("SSS Number Slip");  
       PdfWriter writer = PdfWriter.getInstance(document, baos);  
       document.open();  
       /*set rectangle border*/  
       PdfContentByte under = writer.getDirectContent();  
       under.rectangle(5, 180, 285, 210);  
       Image imgLogo = Image.getInstance(new URL("http://" + request.getLocalAddr() +  
                      ":" + request.getLocalPort() +  
                      request.getContextPath() +  
                      "/images/sss_logo.gif"));  
       imgLogo.scalePercent(25.0f);  
       imgLogo.setAbsolutePosition(9f, 340f);  
       Font fontNormal = new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL);  
       Font fontBold = new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.BOLD);  
       Paragraph par10 = new Paragraph();  
       par10.add(imgLogo);  
       par10.setAlignment(Element.ALIGN_LEFT);  
       Paragraph par11 =  
         new Paragraph("Republic of the Philippines", fontNormal);  
       par11.setAlignment(Element.ALIGN_CENTER);  
       Paragraph par12 =  
         new Paragraph("GOCC", fontBold);  
       par12.setAlignment(Element.ALIGN_CENTER);  
       Paragraph par13 =  
         new Paragraph("GOCC NUMBER SLIP" + "\n", fontNormal);  
       par13.setAlignment(Element.ALIGN_CENTER);  
       Paragraph par0 =  
         new Paragraph("\n Membership Number: " + ssNum, fontNormal);  
       par0.setAlignment(Element.ALIGN_CENTER);  
       Paragraph par1 = new Paragraph(applNm, fontNormal);  
       par1.setAlignment(Element.ALIGN_CENTER);  
       Paragraph par2 =  
         new Paragraph("Birthdate: " + dobth, fontNormal);  
       par2.setAlignment(Element.ALIGN_CENTER);  
       Paragraph par3 = new Paragraph(" ", fontNormal);  
       par3.setAlignment(Element.ALIGN_CENTER);  
       document.add(par10);  
       document.add(par11);  
       document.add(par12);  
       document.add(par13);  
       document.add(par0);  
       document.add(par1);  
       document.add(par2);  
       document.add(par3);  
       Image barcode = Image.getInstance(new URL(request.getRequestURL().substring(0, request.getRequestURL().indexOf(request.getContextPath()) +  
                                       request.getContextPath().length()) +  
                      "/barcode.html?barcode=" + ssNum +" " + applNm));  
       PdfPTable table = new PdfPTable(1);  
       table.setHorizontalAlignment(Element.ALIGN_CENTER);  
       table.addCell(barcode);  
       document.add(table);  
       document.close();  
       fileName = "Membership_Slip.pdf";  
       pdfReport = new ByteArrayInputStream(baos.toByteArray());  
       baos.close();  
       return pdfReport;  
     }catch(Exception e){  
       e.printStackTrace();  
       return null;  
     }      
   }  

No comments:

Post a Comment