Ver Mensaje Individual
  #6 (permalink)  
Antiguo 17/09/2009, 05:35
samu22
 
Fecha de Ingreso: abril-2008
Mensajes: 453
Antigüedad: 16 años, 10 meses
Puntos: 16
Respuesta: Llenar un PDF desde PHP y XML

hola envipe79, yo utilizo la clase ezpdf a la cual le agregue un extends backgroundPDF para generar pdf de los articulos de una web.

hice un archivo crearPDF.php donde le paso por $_GET el id del post a generar el pdf aqui el codigo

Código php:
Ver original
  1. function HexToRGB($hex) {
  2.  
  3. $hex = ereg_replace("#", "", $hex);
  4.  
  5. $color = array();
  6.  
  7.  
  8.  
  9.     if(strlen($hex) == 3) {
  10.  
  11.         $color['r'] = hexdec(substr($hex, 0, 1) . $r);
  12.        
  13.         $color['g'] = hexdec(substr($hex, 1, 1) . $g);
  14.        
  15.         $color['b'] = hexdec(substr($hex, 2, 1) . $b);
  16.    
  17.     }
  18.    
  19.     else if(strlen($hex) == 6) {
  20.    
  21.         $color['r'] = hexdec(substr($hex, 0, 2));
  22.        
  23.         $color['g'] = hexdec(substr($hex, 2, 2));
  24.        
  25.         $color['b'] = hexdec(substr($hex, 4, 2));
  26.    
  27.     }
  28.    
  29.      $color['r'] = round(($color['r']/255)*100)/100;
  30.      $color['g'] = round(($color['g']/255)*100)/100;
  31.      $color['b'] = round(($color['b']/255)*100)/100;
  32.    
  33.     return $color ;
  34.  
  35. }
  36.  
  37. function RGBToHex($r, $g, $b) {
  38.  
  39.     $hex = "#";
  40.    
  41.     $hex.= dechex($r);
  42.    
  43.     $hex.= dechex($g);
  44.    
  45.     $hex.= dechex($b);
  46.    
  47.      
  48.    
  49.     return $hex;
  50.  
  51. }
  52.  
  53.  
  54. class Creport extends backgroundPDF {
  55.    
  56.     var $reportContents = array();
  57.    
  58.     function Creport($p,$o,$type = 'none', $options = array()){
  59.       $this->backgroundPDF($p,$o,$type,$options);
  60.     }
  61.    
  62.     function rf($info){
  63.       // this callback records all of the table of contents entries, it also places a destination marker there
  64.       // so that it can be linked too
  65.       $tmp = $info['p'];
  66.       $lvl = $tmp[0];
  67.       $lbl = rawurldecode(substr($tmp,1));
  68.       $num=$this->ezWhatPageNumber($this->ezGetCurrentPageNumber());
  69.       $this->reportContents[] = array($lbl,$num,$lvl );
  70.       $this->addDestination('toc'.(count($this->reportContents)-1),'FitH',$info['y']+$info['height']);
  71.     }
  72.    
  73.     function dots($info){
  74.       // draw a dotted line over to the right and put on a page number
  75.       $tmp = $info['p'];
  76.       $lvl = $tmp[0];
  77.       $lbl = substr($tmp,1);
  78.       $xpos = 520;
  79.    
  80.       switch($lvl){
  81.         case '1':
  82.           $size=16;
  83.           $thick=1;
  84.           break;
  85.         case '2':
  86.           $size=12;
  87.           $thick=0.5;
  88.           break;
  89.       }
  90.    
  91.       $this->saveState();
  92.       $this->setLineStyle($thick,'round','',array(0,10));
  93.       $this->line($xpos,$info['y'],$info['x']+5,$info['y']);
  94.       $this->restoreState();
  95.       $this->addText($xpos+5,$info['y'],$size,$lbl);
  96.    
  97.    
  98.     }
  99.  
  100.     function setEstilo($info){
  101.         switch($info['p']){
  102.             case 'titulo':
  103.                 $color = HexToRGB('717C7E');
  104.                 $this->setColor($color['r'],$color['g'],$color['b']);
  105.                 $cS = HexToRgb("e8efdb");
  106.                 $this->setStrokeColor($cS['r'],$cS['g'],$cS['b']);
  107.                 $this->setLineStyle(0.1);
  108.                 $this->line($info['x'],($info['y']-($info['height']/2)),550,($info['y']-($info['height']/2)));
  109.                 $this->ezSetY(($info['y']-($info['height']/2)));
  110.             break;
  111.             case 'texto':
  112.                 $color = HexToRGB('999999');
  113.                 $this->setColor($color['r'],$color['g'],$color['b']);
  114.             break; 
  115.         }
  116.    
  117.     }
  118.    
  119.     function img($info){
  120.        
  121.        
  122.         $tam = getimagesize($info['p']);
  123.                    
  124.         $ext = substr(strrchr($info['p'],"."),1);
  125.        
  126.         if($ext=='jpg'){
  127.             $this->addJpegFromFile($info['p'],$info['x'],($info['y']-($info['height']/2)),($tam[0]/2),($tam[1]/2));
  128.             $this->ezSetY($info['y']-($info['height']*2));
  129.         }else{
  130.             $this->addPngFromFile($info['p'],$info['x'],$info['y'],($tam[0]/2),($tam[1]/2));
  131.         }
  132.        
  133.     }
  134.  
  135.  
  136. }
  137.  
  138.  
  139.  
  140. if(isset($_GET['idPost'])){
  141.     global $DbConn;
  142.    
  143.     //Buscamos la informacion del post
  144.         $sql = 'SELECT * FROM post WHERE id='.$_GET['idPost'];
  145.         $Post = $DbConn->fetchAssoc($DbConn->query($sql));
  146.    
  147.     //limpiamos
  148.         $Post['contenido']= $Post['contenido'];
  149.         $Post['contenido']= str_replace('<br />','',$Post['contenido']);
  150.         $Post['contenido']= str_replace('<li>',' <c:img:../img/estilos/liPag.jpg></c:img>',$Post['contenido']);
  151.         $Post['contenido']= str_replace('</li>','',$Post['contenido']);
  152.         $Post['contenido']= str_replace('<h1>','<b>',$Post['contenido']);
  153.         $Post['contenido']= str_replace('</h1>','</b>',$Post['contenido']);
  154.    
  155.     //instanciamos la clase Cezpdf
  156.         $c = HexToRgb("#FFFFFF");
  157.         $pdf = new Creport('a4','portrait','color',array('r'=>$c['r'],'g'=>$c['g'],'b'=>$c['b']));
  158.         $pdf->selectFont('../include/fonts/Helvetica.afm');
  159.  
  160.     //Configuramos los margenes
  161.         $pdf -> ezSetMargins(50,70,50,50);
  162.    
  163.     // Generamos las cabeceras
  164.         $all = $pdf->openObject();
  165.         $pdf->saveState();
  166.         $cS = HexToRgb("e8efdb");
  167.         $pdf->setStrokeColor($cS['r'],$cS['g'],$cS['b']);
  168.         $tam = getimagesize("../img/png/logo.jpg");
  169.         $pdf->addJpegFromFile('../img/png/logo.jpg',($pdf->ez['pageWidth']-(($tam[0]/2)+5)),($pdf->ez['pageHeight']-($tam[1]/2)),$tam[0]/2,$tam[1]/2);
  170.         $pdf->line(20,40,578,40);
  171.         $pdf->addText(50,34,6,'http://www.xxxx.xxx.xx - Portal de Ecología');  $pdf->restoreState();
  172.        
  173.         $pdf->closeObject();
  174.         $pdf->addObject($all,'all');
  175.    
  176.  
  177.  
  178.     $pdf->ezStartPageNumbers(500,28,5,'','{PAGENUM} de {TOTALPAGENUM}',1);
  179.    
  180.    
  181.     //recorremos los datos
  182.    
  183.         $pdf->ezText("<C:setEstilo:titulo>".$Post['titulo']."</c:setEstilo>\n",10);
  184.         $pdf->ezText("<C:setEstilo:texto>".$Post['contenido']."</c:setEstilo>\n",5.5,array('justification'=>'full'));
  185.        
  186.        
  187.        
  188.     //prueba callback
  189.         //$pdf->ezText("<C:img:></c:img>\n",5.5,array('justification'=>'full'));   
  190.    
  191.    
  192.     $pdf->output();
  193.     $pdf->ezStream();
  194. }