Tema: generar pdf
Ver Mensaje Individual
  #13 (permalink)  
Antiguo 02/12/2004, 12:18
Avatar de The_omars
The_omars
 
Fecha de Ingreso: marzo-2004
Mensajes: 271
Antigüedad: 20 años, 1 mes
Puntos: 0
Código PHP:

/*******************************************************************************
*                                                                              *
*                              Protected methods                               *
*                                                                              *
*******************************************************************************/
function _dochecks()
{
    
//Check for locale-related bug
    
if(1.1==1)
        
$this->Error('Don\'t alter the locale before including class file');
    
//Check for decimal separator
    
if(sprintf('%.1f',1.0)!='1.0')
        
setlocale(LC_NUMERIC,'C');
}

function 
_begindoc()
{
    
//Start document
    
$this->state=1;
    
$this->_out('%PDF-1.3');
}

function 
_putpages()
{
    
$nb=$this->page;
    if(!empty(
$this->AliasNbPages))
    {
        
//Replace number of pages
        
for($n=1;$n<=$nb;$n++)
            
$this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
    }
    if(
$this->DefOrientation=='P')
    {
        
$wPt=$this->fwPt;
        
$hPt=$this->fhPt;
    }
    else
    {
        
$wPt=$this->fhPt;
        
$hPt=$this->fwPt;
    }
    
$filter=($this->compress) ? '/Filter /FlateDecode ' '';
    for(
$n=1;$n<=$nb;$n++)
    {
        
//Page
        
$this->_newobj();
        
$this->_out('<</Type /Page');
        
$this->_out('/Parent 1 0 R');
        if(isset(
$this->OrientationChanges[$n]))
            
$this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
        
$this->_out('/Resources 2 0 R');
        if(isset(
$this->PageLinks[$n]))
        {
            
//Links
            
$annots='/Annots [';
            foreach(
$this->PageLinks[$n] as $pl)
            {
                
$rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
                
$annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
                if(
is_string($pl[4]))
                    
$annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
                else
                {
                    
$l=$this->links[$pl[4]];
                    
$h=isset($this->OrientationChanges[$l[0]]) ? $wPt $hPt;
                    
$annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
                }
            }
            
$this->_out($annots.']');
        }
        
$this->_out('/Contents '.($this->n+1).' 0 R>>');
        
$this->_out('endobj');
        
//Page content
        
$p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
        
$this->_newobj();
        
$this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
        
$this->_putstream($p);
        
$this->_out('endobj');
    }
    
//Pages root
    
$this->offsets[1]=strlen($this->buffer);
    
$this->_out('1 0 obj');
    
$this->_out('<</Type /Pages');
    
$kids='/Kids [';
    for(
$i=0;$i<$nb;$i++)
        
$kids.=(3+2*$i).' 0 R ';
    
$this->_out($kids.']');
    
$this->_out('/Count '.$nb);
    
$this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
    
$this->_out('>>');
    
$this->_out('endobj');
}

function 
_putfonts()
{
    
$nf=$this->n;
    foreach(
$this->diffs as $diff)
    {
        
//Encodings
        
$this->_newobj();
        
$this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
        
$this->_out('endobj');
    }
    
$mqr=get_magic_quotes_runtime();
    
set_magic_quotes_runtime(0);
    foreach(
$this->FontFiles as $file=>$info)
    {
        
//Font file embedding
        
$this->_newobj();
        
$this->FontFiles[$file]['n']=$this->n;
        if(
defined('FPDF_FONTPATH'))
            
$file=FPDF_FONTPATH.$file;
        
$size=filesize($file);
        if(!
$size)
            
$this->Error('Font file not found');
        
$this->_out('<</Length '.$size);
        if(
substr($file,-2)=='.z')
            
$this->_out('/Filter /FlateDecode');
        
$this->_out('/Length1 '.$info['length1']);
        if(isset(
$info['length2']))
            
$this->_out('/Length2 '.$info['length2'].' /Length3 0');
        
$this->_out('>>');
        
$f=fopen($file,'rb');
        
$this->_putstream(fread($f,$size));
        
fclose($f);
        
$this->_out('endobj');
    }
    
set_magic_quotes_runtime($mqr);
    foreach(
$this->fonts as $k=>$font)
    {
        
//Font objects
        
$this->fonts[$k]['n']=$this->n+1;
        
$type=$font['type'];
        
$name=$font['name'];
        if(
$type=='core')
        {
            
//Standard font
            
$this->_newobj();
            
$this->_out('<</Type /Font');
            
$this->_out('/BaseFont /'.$name);
            
$this->_out('/Subtype /Type1');
            if(
$name!='Symbol' and $name!='ZapfDingbats')
                
$this->_out('/Encoding /WinAnsiEncoding');
            
$this->_out('>>');
            
$this->_out('endobj');
        }
        elseif(
$type=='Type1' or $type=='TrueType')
        {
            
//Additional Type1 or TrueType font
            
$this->_newobj();
            
$this->_out('<</Type /Font');
            
$this->_out('/BaseFont /'.$name);
            
$this->_out('/Subtype /'.$type);
            
$this->_out('/FirstChar 32 /LastChar 255');
            
$this->_out('/Widths '.($this->n+1).' 0 R');
            
$this->_out('/FontDescriptor '.($this->n+2).' 0 R');
            if(
$font['enc'])
            {
                if(isset(
$font['diff']))
                    
$this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
                else
                    
$this->_out('/Encoding /WinAnsiEncoding');
            }
            
$this->_out('>>');
            
$this->_out('endobj');
            
//Widths
            
$this->_newobj();
            
$cw=&$font['cw'];
            
$s='[';
            for(
$i=32;$i<=255;$i++)
                
$s.=$cw[chr($i)].' ';
            
$this->_out($s.']');
            
$this->_out('endobj');
            
//Descriptor
            
$this->_newobj();
            
$s='<</Type /FontDescriptor /FontName /'.$name;
            foreach(
$font['desc'] as $k=>$v)
                
$s.=' /'.$k.' '.$v;
            
$file=$font['file'];
            if(
$file)
                
$s.=' /FontFile'.($type=='Type1' '' '2').' '.$this->FontFiles[$file]['n'].' 0 R';
            
$this->_out($s.'>>');
            
$this->_out('endobj');
        }
        else
        {
            
//Allow for additional types
            
$mtd='_put'.strtolower($type);
            if(!
method_exists($this,$mtd))
                
$this->Error('Unsupported font type: '.$type);
            
$this->$mtd($font);
        }
    }
}

function 
_putimages()
{
    
$filter=($this->compress) ? '/Filter /FlateDecode ' '';
    
reset($this->images);
    while(list(
$file,$info)=each($this->images))
    {
        
$this->_newobj();
        
$this->images[$file]['n']=$this->n;
        
$this->_out('<</Type /XObject');
        
$this->_out('/Subtype /Image');
        
$this->_out('/Width '.$info['w']);
        
$this->_out('/Height '.$info['h']);
        if(
$info['cs']=='Indexed')
            
$this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
        else
        {
            
$this->_out('/ColorSpace /'.$info['cs']);
            if(
$info['cs']=='DeviceCMYK')
                
$this->_out('/Decode [1 0 1 0 1 0 1 0]');
        }
        
$this->_out('/BitsPerComponent '.$info['bpc']);
        if(isset(
$info['f'])) 
          
$this->_out('/Filter /'.$info['f']);
        if(isset(
$info['parms']))
            
$this->_out($info['parms']);
        if(isset(
$info['trns']) and is_array($info['trns']))
        {
            
$trns='';
            for(
$i=0;$i<count($info['trns']);$i++)
                
$trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
            
$this->_out('/Mask ['.$trns.']');
        }
        
$this->_out('/Length '.strlen($info['data']).'>>');
        
$this->_putstream($info['data']);
        unset(
$this->images[$file]['data']);
        
$this->_out('endobj');
        
//Palette
        
if($info['cs']=='Indexed')
        {
            
$this->_newobj();
            
$pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
            
$this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
            
$this->_putstream($pal);
            
$this->_out('endobj');
        }
    }
}

function 
_putresources()
{
    
$this->_putfonts();
    
$this->_putimages();
    
//Resource dictionary
    
$this->offsets[2]=strlen($this->buffer);
    
$this->_out('2 0 obj');
    
$this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
    
$this->_out('/Font <<');
    foreach(
$this->fonts as $font)
        
$this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
    
$this->_out('>>');
    if(
count($this->images))
    {
        
$this->_out('/XObject <<');
        foreach(
$this->images as $image)
            
$this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
        
$this->_out('>>');
    }
    
$this->_out('>>');
    
$this->_out('endobj');