| 1 | package pdftest; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use English; | ||
| 5 | use CGI qw(:all); | ||
| 6 | |||
| 7 | my ($curr, $status, $message, $pdfrep); | ||
| 8 | |||
| 9 | sub pdftest | ||
| 10 | { | ||
| 11 | # This call to CGI initialises all the required variables | ||
| 12 | |||
| 13 | $curr = CGI->new(); | ||
| 14 | |||
| 15 | # This sets up the PDF REP Module | ||
| 16 | |||
| 17 | $pdfrep = PDFREP->new(); | ||
| 18 | |||
| 19 | # This generates the HTML output for the test display page | ||
| 20 | |||
| 21 | my $text1 = param("SUBMIT"); | ||
| 22 | |||
| 23 | print header(); | ||
| 24 | print start_html(-TITLE=>'Hello Trevor',-BGCOLOR=>'228b22',-TEXT=>'FFFFFF',-ALINK=>'FFFFFF', | ||
| 25 | -VLINK=>'FFFFFF'); | ||
| 26 | print center(); | ||
| 27 | print "\n\n"; | ||
| 28 | print h1($text1); | ||
| 29 | print "\n\n"; | ||
| 30 | print br(); | ||
| 31 | |||
| 32 | # This call to PDFREP generates the first line of data for the pdf file | ||
| 33 | # This should be called to generate the heading line once per output file | ||
| 34 | # It will open the output file with the given name and the given directory. | ||
| 35 | # It returns a status flag = 1 succesful and 0 failure and a message text. | ||
| 36 | # The .pdf file extension is added automatically to the output file | ||
| 37 | # It also opens the temporary file for page data which is the same name and directory | ||
| 38 | # but the extension is .tmp. So make sure there is enough room in the area you are | ||
| 39 | # working in. The tmp file is deleted at the end of run or in case of an error. | ||
| 40 | |||
| 41 | # All sections of PDFREP return a status flag and error message this is so that the Package | ||
| 42 | # will not fall over and leave the error coding to the individual programmer. | ||
| 43 | # Nice open source let you do what you want huh. | ||
| 44 | |||
| 45 | my $filenam = "pdftest1"; | ||
| 46 | my $filedir = "../"; | ||
| 47 | my $title = "Test Document For JPG Files"; | ||
| 48 | my $author = "Trevor Ward"; | ||
| 49 | |||
| 50 | ($status, $message) = $pdfrep->heading($filenam, $filedir, $title, $author); | ||
| 51 | |||
| 52 | # This is the error checking if failed files have to be closed and deleted by the PDFREP Package. | ||
| 53 | # This is done by calling the crashed part of the package with no parameters | ||
| 54 | |||
| 55 | if (!$status) | ||
| 56 | { | ||
| 57 | print "<BR><BR><h4>PDF FILE CREATION FAILED </h4>- $message<BR><BR>\n"; | ||
| 58 | ($status, $message) = $pdfrep->crashed(); | ||
| 59 | print "$status - $message<BR>\n"; | ||
| 60 | } | ||
| 61 | else | ||
| 62 | { | ||
| 63 | print "$status - $message<BR>\n"; | ||
| 64 | } | ||
| 65 | |||
| 66 | # The next item to setup in generating the pdf file is the fonts used throughout the document. | ||
| 67 | # This is done by calling FONTSET with two parameters. | ||
| 68 | # 1 = The font reference name - this will be used when writing out the data | ||
| 69 | # 2 = The font to be used i.e. Arial or Helvetica etc. | ||
| 70 | # | ||
| 71 | # For this example two fonts will be used as per the example above. | ||
| 72 | |||
| 73 | ($status, $message) = $pdfrep->fontset('F1','Helvetica'); | ||
| 74 | |||
| 75 | if (!$status) | ||
| 76 | { | ||
| 77 | print "<BR><BR><h4>PDF FONT CREATION FAILED </h4>- $message<BR><BR>\n"; | ||
| 78 | ($status, $message) = $pdfrep->crashed(); | ||
| 79 | print "$status - $message<BR>\n"; | ||
| 80 | } | ||
| 81 | else | ||
| 82 | { | ||
| 83 | print "$status - $message<BR>\n"; | ||
| 84 | } | ||
| 85 | |||
| 86 | ($status, $message) = $pdfrep->fontset('F2','Helvetica-Bold'); | ||
| 87 | |||
| 88 | if (!$status) | ||
| 89 | { | ||
| 90 | print "<BR><BR><h4>PDF FONT CREATION FAILED </h4>- $message<BR><BR>\n"; | ||
| 91 | ($status, $message) = $pdfrep->crashed(); | ||
| 92 | print "$status - $message<BR>\n"; | ||
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | print "$status - $message<BR>\n"; | ||
| 97 | } | ||
| 98 | |||
| 99 | # So now we have the PDF file opened and ready for input the fonts have been specified so now lets | ||
| 100 | # start writing the data. OH Joy OH Rapture. | ||
| 101 | # This is done by calling the Pagedata section of PDFREP. This section has various parameters | ||
| 102 | # 1 The type of data passed | ||
| 103 | # np = new page | ||
| 104 | # nl = new line | ||
| 105 | # nc = new column | ||
| 106 | # Used to enable the columinsation of reports by using the offset field. Will only left | ||
| 107 | # Justify so if require right justify user courier font and space pad front of data. | ||
| 108 | # Allows the usage of different font and colours by column. | ||
| 109 | # im = new image | ||
| 110 | # You specify one of these as the first parameter passed to identify what your upto. | ||
| 111 | # Data is written out one line at a time so try not to exceed the characters. | ||
| 112 | # Always start with new page. | ||
| 113 | |||
| 114 | # The other parameters passed are | ||
| 115 | # 2 = The column offset . This is the amount of characters from the left hand column of the line. | ||
| 116 | # 3 = The font size to be used with the font set later. It is the text point size standard is 12 | ||
| 117 | # 4 = The name of the font you want to use as previously set | ||
| 118 | # 5 = The size of the font on the next data line to allow for correct data throws. | ||
| 119 | # 6 = Italic 0 = No 1 = yes > 1 get some weird results. | ||
| 120 | # 7 = Red colour | ||
| 121 | # 8 = Green colour | ||
| 122 | # 9 = Blue colour 0 0 0 = black 1 1 1 = white | ||
| 123 | # 10 = The data. This is the textual data you require. | ||
| 124 | # If image ten contains the name of the image width and hieght ie. I2 300 60 | ||
| 125 | |||
| 126 | # For a new page line there are two extra parameters required page size and orientation. The document | ||
| 127 | # must have the same all the way through | ||
| 128 | |||
| 129 | # 11 = Page Size LE = letter A4 = A4 | ||
| 130 | # 12 = Page Orientation PO = Portrait LA = Landscape | ||
| 131 | |||
| 132 | # Leaving these blank defaults to Letter Portrait | ||
| 133 | |||
| 134 | # Again it is a good idea to check the status after every line of data output just in case an error | ||
| 135 | # occurs like run out of disc space. OK in this example its a bit of a pain but thats life normally | ||
| 136 | # this would be a nice loop. | ||
| 137 | |||
| 138 | ($status, $message) = $pdfrep->pagedata('np','0','12','F2','12','0','0','0','0','This is the first bit of text just to prove it to Mel','A4','PO'); | ||
| 139 | |||
| 140 | if (!$status) | ||
| 141 | { | ||
| 142 | print "<BR><BR><h4>PDF NEW PAGE CREATION FAILED </h4>- $message<BR><BR>\n"; | ||
| 143 | ($status, $message) = $pdfrep->crashed(); | ||
| 144 | print "$status - $message<BR>\n"; | ||
| 145 | } | ||
| 146 | else | ||
| 147 | { | ||
| 148 | print "$status - $message<BR>\n"; | ||
| 149 | } | ||
| 150 | |||
| 151 | ($status, $message) = $pdfrep->pagedata('nl','0','12','F1','12','0','0','0','0','This is the second bit of text'); | ||
| 152 | |||
| 153 | if (!$status) | ||
| 154 | { | ||
| 155 | print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n"; | ||
| 156 | ($status, $message) = $pdfrep->crashed(); | ||
| 157 | print "$status - $message<BR>\n"; | ||
| 158 | } | ||
| 159 | else | ||
| 160 | { | ||
| 161 | print "$status - $message<BR>\n"; | ||
| 162 | } | ||
| 163 | |||
| 164 | ($status, $message) = $pdfrep->pagedata('nl','0','10','F1','10','0','0','0','0','This is the third bit of data'); | ||
| 165 | ($status, $message) = $pdfrep->pagedata('nc','150','10','F1','10','0','0','0','0','This is the four bit of text'); | ||
| 166 | ($status, $message) = $pdfrep->pagedata('nc','300','10','F1','10','0','0','0','0','This is the five bit of text'); | ||
| 167 | ($status, $message) = $pdfrep->pagedata('nc','450','10','F1','10','0','0','0','0','This is the five bit text 2'); | ||
| 168 | ($status, $message) = $pdfrep->pagedata('nl','0','10','F1','10','0','1','0','0','This is the third bit of data'); | ||
| 169 | ($status, $message) = $pdfrep->pagedata('nc','150','10','F2','10','0','0','0','0','This is the four bit of text'); | ||
| 170 | ($status, $message) = $pdfrep->pagedata('nc','300','10','F2','10','0','0','0','0','This is the five bit of text'); | ||
| 171 | ($status, $message) = $pdfrep->pagedata('nl','0','10','F1','10','0','0','0','0','This is the third bit of data'); | ||
| 172 | ($status, $message) = $pdfrep->pagedata('nc','150','10','F1','10','0','0','0','0','This is the four bit of text'); | ||
| 173 | ($status, $message) = $pdfrep->pagedata('nc','300','10','F1','10','0','0','1','0','This is the five bit of text'); | ||
| 174 | ($status, $message) = $pdfrep->pagedata('nl','0','10','F2','10','0','0','0','0','This is the third bit of data'); | ||
| 175 | ($status, $message) = $pdfrep->pagedata('nc','150','10','F2','10','0','0','0','0','This is the four bit of text'); | ||
| 176 | # ($status, $message) = $pdfrep->pagedata('nc','300','10','F2','10','0','0','0','0','Teo'); | ||
| 177 | ($status, $message) = $pdfrep->pagedata('nc','450','10','F1','10','0','1','0','0','FCUK'); | ||
| 178 | # ($status, $message) = $pdfrep->pagedata('nl','0','12','F1','12','0','0','0','0','This is the third bit of data'); | ||
| 179 | # ($status, $message) = $pdfrep->pagedata('nl','0','12','F1','12','0','0','0','0','This is the four bit of text'); | ||
| 180 | # ($status, $message) = $pdfrep->pagedata('nl','0','12','F2','12','0','0','0','0','This is the five bit of text'); | ||
| 181 | ($status, $message) = $pdfrep->pagedata('nl','0','10','F1','10','0','0','0','0','This is the six bit of text'); | ||
| 182 | # ($status, $message) = $pdfrep->pagedata('nl','0','12','F1','12','0','0','0','0','This is the seven bit of text'); | ||
| 183 | # ($status, $message) = $pdfrep->pagedata('nl','0','12','F1','12','0','0','0','0','This is the eight bit of text'); | ||
| 184 | # ($status, $message) = $pdfrep->pagedata('nl','0','12','F1','12','0','0','0','0','This is the ninth bit of text'); | ||
| 185 | # ($status, $message) = $pdfrep->pagedata('nl','0','12','F1','12','0','0','0','0','This is the tenth bit of text'); | ||
| 186 | ($status, $message) = $pdfrep->pagedata('np','0','12','F2','12','0','0','0','0','This is the first bit of text','A4','PO'); | ||
| 187 | |||
| 188 | # The following code uses the Chart module from CPAN to generate a PNG file which is a graph | ||
| 189 | # of data. This is predomently why PDFREP was written. | ||
| 190 | |||
| 191 | use Chart::Composite; | ||
| 192 | my $obj = new Chart::Composite (600,350); | ||
| 193 | |||
| 194 | my %hash=('transparent' => 'true', | ||
| 195 | 'text_space' => 5, | ||
| 196 | 'title' => 'POWERTRAIN QUALITY STATUS Average over 9 CIPE Quality Tools', | ||
| 197 | 'x_label' => 'Date', | ||
| 198 | 'y_label' => '% Complete', | ||
| 199 | 'legend' => 'bottom', | ||
| 200 | 'legend_labels' => ['Target', | ||
| 201 | 'Cat A Systems', | ||
| 202 | 'Other Systems', | ||
| 203 | 'Recovery Plan', | ||
| 204 | 'Checkpoints'], | ||
| 205 | 'tick_len' => 3, | ||
| 206 | 'y_ticks' => 11, | ||
| 207 | 'max_val' => 100, | ||
| 208 | 'min_val' => 0, | ||
| 209 | 'pt_size' => 18, | ||
| 210 | 'colors' => {'x_grid_lines' => [255,255,255], | ||
| 211 | 'y_grid_lines' => [0,0,0]}, | ||
| 212 | 'grey_background' => 'false', | ||
| 213 | 'grid_lines' => 'false', | ||
| 214 | 'brush_size' => 3, | ||
| 215 | 'pt_size' => 20, | ||
| 216 | 'composite_info' => [['LinesPoints',[1,3,5]],['LinesPoints',[2,4]]], | ||
| 217 | 'same_y_axes' => 'true', | ||
| 218 | 'no_cache' => 'true'); | ||
| 219 | |||
| 220 | my @xticks=(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); | ||
| 221 | my @data0=(68,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,80,undef,undef,undef,undef,undef,80,undef);#Target | ||
| 222 | my @data1=(28,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,35,40,46,55,55,55,58,58);#Cat A Systems | ||
| 223 | my @data2=(22,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,35,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,80);#Recovery Plan | ||
| 224 | my @data3=(22,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,35,35,38,42,44,44,46,47);#Other Systems | ||
| 225 | my @data4=(0,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,0,undef,undef,undef,undef,undef,0,undef);#Checkpoints | ||
| 226 | my @stuff=\(@xticks,@data0,@data1,@data2,@data3,@data4); | ||
| 227 | |||
| 228 | $obj->set(%hash); | ||
| 229 | $obj->png('../test1.png',\@stuff); | ||
| 230 | |||
| 231 | # Using this PNG image now we can display it. | ||
| 232 | |||
| 233 | # the include image includes the image into the PDF file. It uses the physical file | ||
| 234 | # on disc and can then be used at any point on any page. | ||
| 235 | |||
| 236 | $pdfrep->include_image('I1','test1.png','600','350', 'png', '../'); | ||
| 237 | |||
| 238 | # This is the im option for displaying page data | ||
| 239 | |||
| 240 | ($status, $message) = $pdfrep->pagedata('im','0','12','F1','12','0','0','0','0','I1 600 350'); | ||
| 241 | |||
| 242 | # ($status, $message) = $pdfrep->include_image('I2','cipetitle.jpg','300','60', 'jpg', '../'); | ||
| 243 | # ($status, $message) = $pdfrep->pagedata('im','100','12','F1','12','0','0','0','0','I2 300 60'); | ||
| 244 | |||
| 245 | # OK so are all the data lines and pages written now. Hope so because it's time to write the | ||
| 246 | # PDF file. This might take a minute because it's playing with reading the temporary file and | ||
| 247 | # outputting this to the PDF File, It will also build the page cross reference list and all | ||
| 248 | # the required trailer records just for fun or to make it easier for the programmer to remember what | ||
| 249 | # is required when producing a PDF like the data haha. | ||
| 250 | # So you think all the parameters are over well think again | ||
| 251 | # 1 = Page size LE = Letter and A4 = A4 defaults to letter | ||
| 252 | # 2 = Page orientation PO = Portrait and LA = Landscape | ||
| 253 | |||
| 254 | ($status, $message) = $pdfrep->writepdf('A4','PO'); | ||
| 255 | |||
| 256 | my $fileout = $filedir . $filenam . ".pdf"; | ||
| 257 | |||
| 258 | print br(); | ||
| 259 | print "\n\n"; | ||
| 260 | print ("<A href=$fileout>"); | ||
| 261 | print ("<FONT COLOR='000000'><B>"); | ||
| 262 | print ("PDF file is ", $filenam); | ||
| 263 | print ("</B></FONT>"); | ||
| 264 | print ("</A>"); | ||
| 265 | print "\n\n"; | ||
| 266 | |||
| 267 | print end_html(); | ||
| 268 | print "\n\n"; | ||
| 269 | } | ||
| 270 | 1; |