Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. public void downloadPdf() throws IOException, DocumentException {
  2. System.out.println("hi i ma in");
  3. resultList=examinationDetailsService.readAdmitCardData();
  4.  
  5. for(Object[] data:resultList)
  6. {
  7. personalDetails=(PersonalDetails)data[0];
  8. System.out.println("name"+personalDetails.getApplicantName());
  9. rollNoAssign=(RollNoAssign)data[1];
  10. System.out.println("rollno"+rollNoAssign.getRollNo());
  11. examDateAssign=(ExamDateAssign)data[2];
  12. }
  13. //Converting Date
  14. SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
  15. String date = DATE_FORMAT.format(examDateAssign.getExaminationDate());
  16. // Get the FacesContext
  17. FacesContext facesContext = FacesContext.getCurrentInstance();
  18. // Get HTTP response
  19. HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
  20. // Set response headers
  21. response.reset(); // Reset the response in the first place
  22. response.setHeader("Content-Type", "application/pdf"); // Set only the content type
  23. // Open response output stream
  24. OutputStream responseOutputStream = response.getOutputStream();
  25.  
  26. Document document = new Document(PageSize.A4, 0, 0, 0, 0);
  27. PdfWriter.getInstance(document,response.getOutputStream());
  28. document.open();
  29. PdfPTable maintable = new PdfPTable(1);
  30. maintable.setWidthPercentage(90);
  31. maintable.setSpacingBefore(0f);
  32. maintable.setSpacingAfter(0f);
  33. Paragraph ph1 = new Paragraph();
  34. ph1.add(new Chunk("nnGOVERNMENT OF ASSAM nOFFICE OF THE ELECTRICAL LICENSING BOARD, ASSAMn1", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL)));
  35. //ph1.add(new Chunk("ST", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL)));
  36. Chunk superScript = new Chunk("ST", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, Font.NORMAL));
  37. superScript.setTextRise(5f);
  38. ph1.add(superScript);
  39. ph1.add(new Chunk(" FLOOR, WEST END BLOCK, HOUSEFED COMPLEX,DISPUR, GUWAHATI-781006, ASSAM.", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL)));
  40. ph1.add(new Chunk("n***n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD)));
  41. ph1.add(new Chunk("nADMIT CARD", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD)));
  42.  
  43. PdfPCell heading1 = new PdfPCell(ph1);
  44. heading1.setBorder(0);
  45. heading1.setHorizontalAlignment(Element.ALIGN_CENTER);
  46. heading1.setVerticalAlignment(Element.ALIGN_CENTER);
  47. maintable.addCell(heading1);
  48.  
  49. PdfPTable maintable1 = new PdfPTable(1);
  50. maintable1.setWidthPercentage(87);
  51. maintable1.setSpacingBefore(0f);
  52. maintable1.setSpacingAfter(0f);
  53.  
  54. Paragraph ph2 = new Paragraph();
  55. ph2.add(new Chunk("nnShri/Smti "+personalDetails.getApplicantName()+", Roll No. "+rollNoAssign.getRollNo()+" is hereby "
  56. + "allowed to appear in the examination for grant of Electrical Supervisor's Certificate of Competency "
  57. + "to be held at "+ rollNoAssign.getVenue().getName()
  58. + "as per schedule given below:", FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL)));
  59. ph2.add(new Chunk("nn Viva-voce", FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.BOLD)));
  60. ph2.add(new Chunk(" test on "+date+ " at 9 AM/12.30 PM ;", FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL)));
  61. ph2.add(new Chunk(" Written", FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.BOLD)));
  62. ph2.add(new Chunk(" test on __________ at 9 AM.", FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL)));
  63. ph2.add(new Chunk("nnnPlease bring the followings with you while coming for the said examinations: n"
  64. + "n1.Original copy of Degree/Diploma/ITI Certificate/Supervisor's Certificate of Competency/"
  65. + "Workmen's Permitnn belonging to you and this Admit Card in original.n"
  66. + "n2.Detail experience certificate(s) relevant to the part(s) of Supervisor's examination applied for.n"
  67. + "n3.n"
  68. + "nnNB: (a) No alteration is allowed in the entries on this Admit Card without the authority of the Board."
  69. + "n (b) No expense(s) incurred by any candidate will be borne by the Board.nnn", FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL)));
  70. PdfPCell heading2 = new PdfPCell(ph2);
  71. heading2.setBorder(0);
  72. heading2.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
  73. heading2.setVerticalAlignment(Element.ALIGN_CENTER);
  74. maintable1.addCell(heading2);
  75.  
  76. PdfPTable maintable2 = new PdfPTable(2);
  77. float[] columnWidths = new float[]{55f, 45f};
  78. maintable2.setWidths(columnWidths);
  79. maintable2.setWidthPercentage(84);
  80. maintable2.setSpacingBefore(0f);
  81. maintable2.setSpacingAfter(0f);
  82.  
  83. Paragraph ph31 = new Paragraph();
  84. ph31.add(new Chunk("Details furnished by you in the application form and/or examination process are used by the Board"
  85. + " for further needful, hence, if you feel any correction(s) in the same is/are required, please get those done before"
  86. + " leaving the examination venue. The Board shall not be under any obligation of removing the difficulties arising later"
  87. + " on out of incorrect/improper information furnished by you or non-furnishing of required ones.n", FontFactory.getFont(FontFactory.COURIER, 10, Font.NORMAL)));
  88. PdfPCell heading31 = new PdfPCell(ph31);
  89. heading31.setBorder(15);
  90. heading31.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
  91. heading31.setVerticalAlignment(Element.ALIGN_LEFT);
  92.  
  93. maintable2.addCell(heading31);
  94.  
  95. Paragraph ph32 = new Paragraph();
  96. ph32.add(new Chunk("nnn(Member Secretary)nElectrical Licensing Board,nAssam.", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL)));
  97. PdfPCell heading32 = new PdfPCell(ph32);
  98. heading32.setBorder(0);
  99. heading32.setHorizontalAlignment(Element.ALIGN_CENTER);
  100. heading32.setVerticalAlignment(Element.ALIGN_CENTER);
  101. maintable2.addCell(heading32);
  102.  
  103. document.add(maintable);
  104. document.add(maintable1);
  105. document.add(maintable2);
  106. document.close();
  107.  
  108. /* // Read PDF contents
  109. URL url = new URL(PDF_URL);
  110. InputStream pdfInputStream = url.openStream();*/
  111.  
  112. // Read PDF contents and write them to the output
  113. byte[] bytesBuffer = new byte[2048];
  114. int bytesRead;
  115. /* while ((bytesRead = pdfInputStream.read(bytesBuffer)) > 0) {
  116. responseOutputStream.write(bytesBuffer, 0, bytesRead);
  117. }
  118. */
  119. Base64 encoder = new Base64();
  120. byte[] decodedBytes = encoder.o
  121.  
  122. // Make sure that everything is out
  123. responseOutputStream.write(decodedBytes);
  124. responseOutputStream.flush();
  125.  
  126. // Close both streams
  127. //pdfInputStream.close();
  128. responseOutputStream.close();
  129.  
  130. // JSF doc:
  131. // Signal the JavaServer Faces implementation that the HTTP response for this request has already been generated
  132. // (such as an HTTP redirect), and that the request processing lifecycle should be terminated
  133. // as soon as the current phase is completed.
  134. facesContext.responseComplete();
  135.  
  136. }
  137.  
  138. PdfWriter.getInstance(document,response.getOutputStream());
  139.  
  140. OutputStream responseOutputStream = response.getOutputStream();
  141.  
  142. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  143.  
  144. PdfWriter.getInstance(document, baos);
  145.  
  146. byte[] pdf = baos.toByteArray();
  147.  
  148. byte[] base64 = Base64.encodeBase64(pdf);
  149.  
  150. response.setHeader("Expires", "0");
  151. response.setHeader("Cache-Control",
  152. "must-revalidate, post-check=0, pre-check=0");
  153. response.setHeader("Pragma", "public");
  154. response.setContentType("application/pdf");
  155. response.setContentLength(base64.length);
  156. OutputStream os = response.getOutputStream();
  157. os.write(base64);
  158. os.flush();
  159. os.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement