Thread: Generating PDF
View Single Post
  #3 (permalink)  
Old 03-03-08, 04:16 AM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Hey mab, thanks for the reply.


Well it can't be my PDF reader since a friend of mine sees the exact same output. You can check it out yourself at www.modernm.info/contractgen/

Basically, I am doing the exact same thing as in the readme.php of the library.
PHP Code:

if(isset($_SESSION['contractname']))

{
    if(
$_SESSION['contract'])
    {
        
//Start generating PDF Document
        
        // 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
        
class Creport extends Cezpdf {
        
            var 
$reportContents = array();
            
            function 
Creport($p,$o){
              
$this->Cezpdf($p,$o);
            }
            
            function 
rf($info){
              
// this callback records all of the table of contents entries, it also places a destination marker there
              // so that it can be linked too
              
$tmp $info['p'];
              
$lvl $tmp[0];
              
$lbl rawurldecode(substr($tmp,1));
              
$num=$this->ezWhatPageNumber($this->ezGetCurrentPageNumber());
              
$this->reportContents[] = array($lbl,$num,$lvl );
              
$this->addDestination('toc'.(count($this->reportContents)-1),'FitH',$info['y']+$info['height']);
            }
            
            function 
dots($info)
            {
              
// draw a dotted line over to the right and put on a page number
              
$tmp $info['p'];
              
$lvl $tmp[0];
              
$lbl substr($tmp,1);
              
$xpos 520;
            
              switch(
$lvl)
              {
                case 
'1':
                  
$size=16;
                  
$thick=1;
                  break;
                case 
'2':
                  
$size=12;
                  
$thick=0.5;
                  break;
              }
            
              
$this->saveState();
              
$this->setLineStyle($thick,'round','',array(0,10));
              
$this->line($xpos,$info['y'],$info['x']+5,$info['y']);
              
$this->restoreState();
              
$this->addText($xpos+5,$info['y'],$size,$lbl);
            
            
            }
        
        }
        
        
//set format to A4
        // this code has been modified to use ezpdf.
        
        
$pdf = new Creport('a4','portrait');
        
        
$pdf -> ezSetMargins(50,70,50,50);
        
        
// put a line top and bottom on all the pages
        
$all $pdf->openObject();
        
$pdf->saveState();
        
$pdf->setStrokeColor(0,0,0,1);
        
$pdf->line(20,40,578,40);
        
$pdf->line(20,822,578,822);
        
$pdf->addText(50,34,6,'Generated by Contractgenerator v1.0');
        
$pdf->restoreState();
        
$pdf->closeObject();
        
// note that object can be told to appear on just odd or even pages by changing 'all' to 'odd'
        // or 'even'.
        
$pdf->addObject($all,'all');
        
        
$pdf->ezSetDy(-100);
        
        
//$mainFont = './fonts/Helvetica.afm';
        
$mainFont './fonts/Times-Roman.afm';
        
$codeFont './fonts/Courier.afm';
        
// select a font
        
$pdf->selectFont($mainFont);
        
        
$pdf->ezText($_SESSION['contractname'] . "\n",30,array('justification'=>'centre'));
        
// modified to use the local file if it can
        
        
$pdf->openHere('Fit');
        
        
$pdf->selectFont($mainFont);
        
        
// load up the document content
        
$data explode("\n"$_SESSION['contract']);
        
        
        
$pdf->ezNewPage();
        
        
$pdf->ezStartPageNumbers(500,28,10,'','',1);
        
        
$size=12;
        
$height $pdf->getFontHeight($size);
        
$textOptions = array('justification'=>'full');
        
$collecting=0;
        
$code='';
        
        foreach (
$data as $line)
        {
          
// go through each line, showing it as required, if it is surrounded by '<>' then 
          // assume that it is a title
          
$line=chop($line);
          if (
strlen($line) && $line[0]=='#')
          {
            
// comment, or new page request
            
switch($line)
            {
              case 
'#NP':
                
$pdf->ezNewPage();
                break;
              case 
'#C':
                
$pdf->selectFont($codeFont);
                
$textOptions = array('justification'=>'left','left'=>20,'right'=>20);
                
$size=10;
                break;
              case 
'#c':
                
$pdf->selectFont($mainFont);
                
$textOptions = array('justification'=>'full');
                
$size=12;
                break;
              case 
'#X':
                
$collecting=1;
                break;
              case 
'#x':
                
$pdf->saveState();
                eval(
$code);
                
$pdf->restoreState();
                
$pdf->selectFont($mainFont);
                
$code='';
                
$collecting=0;
                break;
            }
            
          } 
          else if (
$collecting)
          {
            
$code.=$line;
          } 
          else if (((
strlen($line)>&& $line[1]=='<') ) && $line[strlen($line)-1]=='>'
          {
            
// then this is a title
            
switch($line[0])
            {
              case 
'1':
                
$tmp substr($line,2,strlen($line)-3);
                
$tmp2 $tmp.'<C:rf:1'.rawurlencode($tmp).'>';
                
$pdf->ezText($tmp2,26,array('justification'=>'centre'));
                break;
              default:
                
$tmp substr($line,2,strlen($line)-3);
                
// add a grey bar, highlighting the change
                
$tmp2 $tmp.'<C:rf:2'.rawurlencode($tmp).'>';
                
$pdf->transaction('start');
                
$ok=0;
                while (!
$ok)
                {
                  
$thisPageNum $pdf->ezPageCount;
                  
$pdf->saveState();
                  
$pdf->setColor(0.9,0.9,0.9);
                  
$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));
                  
$pdf->restoreState();
                  
$pdf->ezText($tmp2,18,array('justification'=>'left'));
                  if (
$pdf->ezPageCount==$thisPageNum)
                  {
                    
$pdf->transaction('commit');
                    
$ok=1;
                  } 
                  else 
                  {
                    
// then we have moved onto a new page, bad bad, as the background colour will be on the old one
                    
$pdf->transaction('rewind');
                    
$pdf->ezNewPage();
                  }
                }
                break;
            }
          } 
          else 
          {
            
// then this is just text
            // the ezpdf function will take care of all of the wrapping etc.
            
$pdf->ezText($line,$size,$textOptions);
          }
          
        }
        
        
$pdf->ezStopPageNumbers(1,1);
        
        
        
        if (isset(
$d) && $d)
        {
          
$pdfcode $pdf->ezOutput(1);
          
$pdfcode str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
          echo 
'<html><body>';
          echo 
trim($pdfcode);
          echo 
'</body></html>';
        } 
        else 
        {
          
$pdf->ezStream();
        } 
Just without fancy stuff like tables, index and so on.

I reuploaded all fonts necessary, the class files, but still the same effect.


I really don't have an idea what to do anymore.
Download Class Source (without fonts)

Would be great if someone could help me, in case not, do you know another easy to use Library for generating PDF documents?

Thanks,
Max
Reply With Quote