This code snippet will guide you to how to Generate the PDF report
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.util.Date; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Set; import java.util.ArrayList; import java.util.Map; import java.util.Iterator; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.HeaderFooter; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.Section; import com.lowagie.text.Table; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfEncryptor; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.Image; import com.lowagie.text.pdf.codec.GifImage; import com.lowagie.text.pdf.RandomAccessFileOrArray; import com.lowagie.text.pdf.codec.TiffImage; import com.lowagie.text.pdf.codec.GifImage; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import java.awt.Color; import java.awt.Graphics; import com.lowagie.text.Image; public class Health { HashMap resultSet= null ; ServerHealthCheck serverHealthStatus = new ServerHealthCheck(); public static String dateTime= "" ; boolean emailsend= false ; Date date= new Date(); private boolean add; String filename; public static String fileNamev; Rectangle pageSize = new Rectangle( 400 , 400 ); Document document = new Document (pageSize); public Health() throws IOException { try { SimpleDateFormat sdf = new SimpleDateFormat( "dd_MMM_yy_HH_mm_ss" ); //System.out.print(sdf); dateTime = sdf.format(date).toString(); // System.out.print(dateTime); filename = dateTime + ".pdf" ; // System.out.print(filename); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( "MorningHealthcheck/SUPPORT_ATT/" + filename)); } catch (FileNotFoundException e) { System.out.print( "Filenot found" ); } catch (DocumentException e) { System.out.print( "Document not found" ); } catch (Exception e) { e.printStackTrace(); } //System.out.println("Started creating PDF File " + filename); try { document.open(); document.addTitle( "MDIS MorningHealthCheck" ); document.addSubject( "PDF Generated by the Automated Health Check" ); document.addKeywords( "MorningHealthCheck MDIS" ); document.addCreator( "MDISPSS" ); document.addAuthor( "Rajashekar D" ); Image image = Image.getInstance( "Images/MDIS.jpg" ); image.setAlignment(Image.LEFT); image.scaleAbsoluteHeight( 20 ); image.scaleAbsoluteWidth( 2 ); image.scalePercent( 60 , 60 ); document.add(image); } catch (Exception e1) { System.out.print( "EXception in Adding the Image" ); } try { resultSet = serverHealthStatus.connect(); Set set = resultSet.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry map = (Map.Entry) iterator.next(); String key = (String) map.getKey(); document.add( new Paragraph( "\n" )); document.add( new Paragraph( "THE BELOW ARE IN " + key, FontFactory.getFont(FontFactory.COURIER, 12 , Font.BOLD, Color.GREEN))); HashMap val = (HashMap) map.getValue(); Set set1 = val.entrySet(); Iterator iterator1 = set1.iterator(); while (iterator1.hasNext()) { Map.Entry map1 = (Map.Entry) iterator1.next(); String Key = (String) map1.getKey(); document.add( new Paragraph( "\n" )); document.add( new Paragraph(Key, FontFactory.getFont( FontFactory.COURIER, 10 , Font.BOLD, new Color( 0 , 0 , 255 )))); document.add( new Paragraph( "\n" )); ArrayList value = (ArrayList) map1.getValue(); Iterator ite = value.iterator(); while (ite.hasNext()) { String line = (String) ite.next(); Paragraph p = new Paragraph(line, FontFactory.getFont( FontFactory.COURIER, 7 , Font.NORMAL, Color.WHITE)); PdfPTable table = new PdfPTable( 1 ); PdfPCell cell = new PdfPCell(p); cell.setHorizontalAlignment( 200 ); cell.setBackgroundColor(Color.darkGray); cell.setBorder( 1 ); cell.setBorderColor(Color.white); cell.setColspan( 2 ); table.addCell(cell); document.add(table); } } } emailsend = true ; } catch (Exception e) { System.out.print( "null value" ); emailsend = false ; } finally { if (document!= null ) document.close(); } try { PdfEncryptor.encrypt( new PdfReader( "MorningHealthcheck/SUPPORT_ATT/" + filename), new FileOutputStream( "MorningHealthcheck/SUPPORT_ATT/2" + filename), true , "mdis" , "" , PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY); File tempFile = new File( "MorningHealthcheck/SUPPORT_ATT/" + filename); tempFile.delete(); File tempFile2 = new File( "MorningHealthcheck/SUPPORT_ATT/2" + filename); tempFile2.renameTo(tempFile); // System.out.println("File " + filename+ " has been protected with the password"); fileNamev = filename; emailsend = true ; } catch (DocumentException e) { //System.out.print("Document not found"); emailsend = false ; } if (emailsend = true ) { EmailSend send = new EmailSend(); } else { System.out.print( "There is an problem and Exceptions in the Document Preparing" ); } System.out.print( "You are here exit from the sentrion mail handler" ); } public static void main(String args []) throws IOException { Health a= new Health(); System.out.print( "Your out of the box" ); //System.exit(0); } } |