Main Page | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

readme.php

Go to the documentation of this file.
00001 <?php
00002 //===================================================================================================
00003 // this is the php file which creates the readme.pdf file, this is not seriously 
00004 // suggested as a good way to create such a file, nor a great example of prose,
00005 // but hopefully it will be useful
00006 //
00007 // adding ?d=1 to the url calling this will cause the pdf code itself to ve echoed to the 
00008 // browser, this is quite useful for debugging purposes.
00009 // there is no option to save directly to a file here, but this would be trivial to implement.
00010 //
00011 // note that this file comprisises both the demo code, and the generator of the pdf documentation
00012 //
00013 //===================================================================================================
00014 
00015 
00016 // don't want any warnings turning up in the pdf code if the server is set to 'anal' mode.
00017 //error_reporting(7);
00018 error_reporting(E_ALL);
00019 set_time_limit(1800);
00020 
00021 include 'class.ezpdf.php';
00022 
00023 // define a clas extension to allow the use of a callback to get the table of contents, and to put the dots in the toc
00024 class Creport extends Cezpdf {
00025 
00026 var $reportContents = array();
00027 
00028 function Creport($p,$o){
00029   $this->Cezpdf($p,$o);
00030 }
00031 
00032 function rf($info){
00033   // this callback records all of the table of contents entries, it also places a destination marker there
00034   // so that it can be linked too
00035   $tmp = $info['p'];
00036   $lvl = $tmp[0];
00037   $lbl = rawurldecode(substr($tmp,1));
00038   $num=$this->ezWhatPageNumber($this->ezGetCurrentPageNumber());
00039   $this->reportContents[] = array($lbl,$num,$lvl );
00040   $this->addDestination('toc'.(count($this->reportContents)-1),'FitH',$info['y']+$info['height']);
00041 }
00042 
00043 function dots($info){
00044   // draw a dotted line over to the right and put on a page number
00045   $tmp = $info['p'];
00046   $lvl = $tmp[0];
00047   $lbl = substr($tmp,1);
00048   $xpos = 520;
00049 
00050   switch($lvl){
00051     case '1':
00052       $size=16;
00053       $thick=1;
00054       break;
00055     case '2':
00056       $size=12;
00057       $thick=0.5;
00058       break;
00059   }
00060 
00061   $this->saveState();
00062   $this->setLineStyle($thick,'round','',array(0,10));
00063   $this->line($xpos,$info['y'],$info['x']+5,$info['y']);
00064   $this->restoreState();
00065   $this->addText($xpos+5,$info['y'],$size,$lbl);
00066 
00067 
00068 }
00069 
00070 
00071 }
00072 // I am in NZ, so will design my page for A4 paper.. but don't get me started on that.
00073 // (defaults to legal)
00074 // this code has been modified to use ezpdf.
00075 
00076 //$pdf = new Cezpdf('a4','portrait');
00077 $pdf = new Creport('a4','portrait');
00078 
00079 $pdf -> ezSetMargins(50,70,50,50);
00080 
00081 // put a line top and bottom on all the pages
00082 $all = $pdf->openObject();
00083 $pdf->saveState();
00084 $pdf->setStrokeColor(0,0,0,1);
00085 $pdf->line(20,40,578,40);
00086 $pdf->line(20,822,578,822);
00087 $pdf->addText(50,34,6,'http://ros.co.nz/pdf - http://www.sourceforge.net/projects/pdf-php');
00088 $pdf->restoreState();
00089 $pdf->closeObject();
00090 // note that object can be told to appear on just odd or even pages by changing 'all' to 'odd'
00091 // or 'even'.
00092 $pdf->addObject($all,'all');
00093 
00094 $pdf->ezSetDy(-100);
00095 
00096 //$mainFont = './fonts/Helvetica.afm';
00097 $mainFont = './fonts/Times-Roman.afm';
00098 $codeFont = './fonts/Courier.afm';
00099 // select a font
00100 $pdf->selectFont($mainFont);
00101 
00102 $pdf->ezText("PHP Pdf Creation\n",30,array('justification'=>'centre'));
00103 $pdf->ezText("Module-free creation of Pdf documents\nfrom within PHP\n",20,array('justification'=>'centre'));
00104 $pdf->ezText("developed by R&OS Ltd\n<c:alink:http://www.ros.co.nz/pdf/>http://www.ros.co.nz/pdf</c:alink>",18,array('justification'=>'centre'));
00105 $pdf->ezText("\n<c:alink:http://sourceforge.net/projects/pdf-php>http://sourceforge.net/projects/pdf-php</c:alink>\n\nversion 0.09",18,array('justification'=>'centre'));
00106 
00107 $pdf->ezSetDy(-100);
00108 // modified to use the local file if it can
00109 
00110 $pdf->openHere('Fit');
00111 
00112 function ros_logo(&$pdf,$x,$y,$height,$wl=0,$wr=0){
00113   $pdf->saveState();
00114   $h=100;
00115   $factor = $height/$h;
00116   $pdf->selectFont('./fonts/Helvetica-Bold.afm');
00117   $text = 'R&OS';
00118   $ts=100*$factor;
00119   $th = $pdf->getFontHeight($ts);
00120   $td = $pdf->getFontDecender($ts);
00121   $tw = $pdf->getTextWidth($ts,$text);
00122   $pdf->setColor(0.6,0,0);
00123   $z = 0.86;
00124   $pdf->filledRectangle($x-$wl,$y-$z*$h*$factor,$tw*1.2+$wr+$wl,$h*$factor*$z);
00125   $pdf->setColor(1,1,1);
00126   $pdf->addText($x,$y-$th-$td,$ts,$text);
00127   $pdf->setColor(0.6,0,0);
00128   $pdf->addText($x,$y-$th-$td,$ts*0.1,'http://www.ros.co.nz');
00129   $pdf->restoreState();
00130   return $height;
00131 }
00132 
00133 ros_logo($pdf,150,$pdf->y-100,80,150,200);
00134 $pdf->selectFont($mainFont);
00135 
00136 /*
00137 if (file_exists('ros.jpg')){
00138   $pdf->addJpegFromFile('ros.jpg',199,$pdf->y-100,200,0);
00139 } else {
00140   // comment out these two lines if you do not have GD jpeg support
00141   // I couldn't quickly see a way to test for this support from the code.
00142   // you could also copy the file from the locatioin shown and put it in the directory, then 
00143   // the code above which doesn't use GD will be activated.
00144   $img = ImageCreatefromjpeg('http://www.ros.co.nz/pdf/ros.jpg');
00145   $pdf-> addImage($img,199,$pdf->y-100,200,0);
00146 }
00147 */
00148 //-----------------------------------------------------------
00149 // load up the document content
00150 $data=file('./data.txt');
00151 
00152 // try adding the faq's to the document, this will not work for people re-building the file from the 
00153 // download as I am not going to put in the faq file with that
00154 $faqFile = '../ros/pdf/faqs.inc';
00155 if (file_exists($faqFile)){
00156   // then the file is there.
00157   $data[] = "#NP\n";
00158   $data[] = "1<FAQs>\n";
00159   include($faqFile);
00160   foreach($faqs as $v){
00161     $data[] = "2<".$v['t'].">\n";
00162     // these are some VERY rough conversions, I am not recommending others do this
00163     $s = array('<br>','<p>','<pre>','</pre>','<ul>','</ul>','<li>');
00164     $r = array("\n","\n\n","\n#C","\n#c","","","   - ");
00165     if (is_array($v['d'])){
00166       $tmp='';
00167       foreach($v['d'] as $section){
00168         $tmp.=$section."\n\n";
00169       }
00170     } else {
00171       $tmp = $v['d'];
00172     }
00173     $tmp = str_replace($s,$r,$tmp);
00174     $bits = explode("\n",$tmp);
00175     foreach($bits as $bit){
00176       $data[] = $bit."\n";
00177     }
00178   }
00179 }
00180 
00181 $pdf->ezNewPage();
00182 
00183 $pdf->ezStartPageNumbers(500,28,10,'','',1);
00184 
00185 $size=12;
00186 $height = $pdf->getFontHeight($size);
00187 $textOptions = array('justification'=>'full');
00188 $collecting=0;
00189 $code='';
00190 
00191 foreach ($data as $line){
00192   // go through each line, showing it as required, if it is surrounded by '<>' then 
00193   // assume that it is a title
00194   $line=chop($line);
00195   if (strlen($line) && $line[0]=='#'){
00196     // comment, or new page request
00197     switch($line){
00198       case '#NP':
00199         $pdf->ezNewPage();
00200         break;
00201       case '#C':
00202         $pdf->selectFont($codeFont);
00203         $textOptions = array('justification'=>'left','left'=>20,'right'=>20);
00204         $size=10;
00205         break;
00206       case '#c':
00207         $pdf->selectFont($mainFont);
00208         $textOptions = array('justification'=>'full');
00209         $size=12;
00210         break;
00211       case '#X':
00212         $collecting=1;
00213         break;
00214       case '#x':
00215         $pdf->saveState();
00216         eval($code);
00217         $pdf->restoreState();
00218         $pdf->selectFont($mainFont);
00219         $code='';
00220         $collecting=0;
00221         break;
00222     }
00223   } else if ($collecting){
00224     $code.=$line;
00225 //  } else if (((strlen($line)>1 && $line[1]=='<') || (strlen($line) && $line[0]=='<')) && $line[strlen($line)-1]=='>') {
00226   } else if (((strlen($line)>1 && $line[1]=='<') ) && $line[strlen($line)-1]=='>') {
00227     // then this is a title
00228     switch($line[0]){
00229       case '1':
00230         $tmp = substr($line,2,strlen($line)-3);
00231         $tmp2 = $tmp.'<C:rf:1'.rawurlencode($tmp).'>';
00232         $pdf->ezText($tmp2,26,array('justification'=>'centre'));
00233         break;
00234       default:
00235         $tmp = substr($line,2,strlen($line)-3);
00236         // add a grey bar, highlighting the change
00237         $tmp2 = $tmp.'<C:rf:2'.rawurlencode($tmp).'>';
00238         $pdf->transaction('start');
00239         $ok=0;
00240         while (!$ok){
00241           $thisPageNum = $pdf->ezPageCount;
00242           $pdf->saveState();
00243           $pdf->setColor(0.9,0.9,0.9);
00244           $pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y-$pdf->getFontHeight(18)+$pdf->getFontDecender(18),$pdf->ez['pageWidth']-$pdf->ez['leftMargin']-$pdf->ez['rightMargin'],$pdf->getFontHeight(18));
00245           $pdf->restoreState();
00246           $pdf->ezText($tmp2,18,array('justification'=>'left'));
00247           if ($pdf->ezPageCount==$thisPageNum){
00248             $pdf->transaction('commit');
00249             $ok=1;
00250           } else {
00251             // then we have moved onto a new page, bad bad, as the background colour will be on the old one
00252             $pdf->transaction('rewind');
00253             $pdf->ezNewPage();
00254           }
00255         }
00256         break;
00257     }
00258   } else {
00259     // then this is just text
00260     // the ezpdf function will take care of all of the wrapping etc.
00261     $pdf->ezText($line,$size,$textOptions);
00262   }
00263   
00264 }
00265 
00266 $pdf->ezStopPageNumbers(1,1);
00267 
00268 // now add the table of contents, including internal links
00269 $pdf->ezInsertMode(1,1,'after');
00270 $pdf->ezNewPage();
00271 $pdf->ezText("Contents\n",26,array('justification'=>'centre'));
00272 $xpos = 520;
00273 $contents = $pdf->reportContents;
00274 foreach($contents as $k=>$v){
00275   switch ($v[2]){
00276     case '1':
00277       $y=$pdf->ezText('<c:ilink:toc'.$k.'>'.$v[0].'</c:ilink><C:dots:1'.$v[1].'>',16,array('aright'=>$xpos));
00278 //      $y=$pdf->ezText($v[0].'<C:dots:1'.$v[1].'>',16,array('aright'=>$xpos));
00279       break;
00280     case '2':
00281       $pdf->ezText('<c:ilink:toc'.$k.'>'.$v[0].'</c:ilink><C:dots:2'.$v[1].'>',12,array('left'=>50,'aright'=>$xpos));
00282       break;
00283   }
00284 }
00285 
00286 
00287 if (isset($d) && $d){
00288   $pdfcode = $pdf->ezOutput(1);
00289   $pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
00290   echo '<html><body>';
00291   echo trim($pdfcode);
00292   echo '</body></html>';
00293 } else {
00294   $pdf->ezStream();
00295 }
00296 ?>