Ver Mensaje Individual
  #2 (permalink)  
Antiguo 17/06/2010, 08:25
Avatar de wiwi74
wiwi74
 
Fecha de Ingreso: marzo-2008
Mensajes: 515
Antigüedad: 16 años, 2 meses
Puntos: 10
Respuesta: problema con fpdf

Te doy dos codigos del tutorial completo de fpdf que me parecen apropiados para tu necesidad.


Codigo independiente
deberas tener en el mismo directorio una imagen, como en este caso:
logo_pb.png
Código PHP:

<?php
require('fpdf.php');

class 
PDF extends FPDF
{
//Cabecera de página
function Header()
{
    
//Logo
    
$this->Image('logo_pb.png',10,8,33);
    
//Arial bold 15
    
$this->SetFont('Arial','B',15);
    
//Movernos a la derecha
    
$this->Cell(80);
    
//Título
    
$this->Cell(30,10,'Title',1,0,'C');
    
//Salto de línea
    
$this->Ln(20);
}

//Pie de página
function Footer()
{
    
//Posición: a 1,5 cm del final
    
$this->SetY(-15);
    
//Arial italic 8
    
$this->SetFont('Arial','I',8);
    
//Número de página
    
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

//Creación del objeto de la clase heredada
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for(
$i=1;$i<=40;$i++)
    
$pdf->Cell(0,10,'Imprimiendo línea número '.$i,0,1);
$pdf->Output();
?>


Cargar un archivo externo:

Código PHP:


<?php
require('fpdf.php');

class 
PDF extends FPDF
{
//Cargar los datos
function LoadData($file)
{
    
//Leer las líneas del fichero
    
$lines=file($file);
    
$data=array();
    foreach(
$lines as $line)
        
$data[]=explode(';',chop($line));
    return 
$data;
}

//Tabla simple
function BasicTable($header,$data)
{
    
//Cabecera
    
foreach($header as $col)
        
$this->Cell(40,7,$col,1);
    
$this->Ln();
    
//Datos
    
foreach($data as $row)
    {
        foreach(
$row as $col)
            
$this->Cell(40,6,$col,1);
        
$this->Ln();
    }
}

//Una tabla más completa
function ImprovedTable($header,$data)
{
    
//Anchuras de las columnas
    
$w=array(40,35,40,45);
    
//Cabeceras
    
for($i=0;$i<count($header);$i++)
        
$this->Cell($w[$i],7,$header[$i],1,0,'C');
    
$this->Ln();
    
//Datos
    
foreach($data as $row)
    {
        
$this->Cell($w[0],6,$row[0],'LR');
        
$this->Cell($w[1],6,$row[1],'LR');
        
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
        
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
        
$this->Ln();
    }
    
//Línea de cierre
    
$this->Cell(array_sum($w),0,'','T');
}

//Tabla coloreada
function FancyTable($header,$data)
{
    
//Colores, ancho de línea y fuente en negrita
    
$this->SetFillColor(255,0,0);
    
$this->SetTextColor(255);
    
$this->SetDrawColor(128,0,0);
    
$this->SetLineWidth(.3);
    
$this->SetFont('','B');
    
//Cabecera
    
$w=array(40,35,40,45);
    for(
$i=0;$i<count($header);$i++)
        
$this->Cell($w[$i],7,$header[$i],1,0,'C',1);
    
$this->Ln();
    
//Restauración de colores y fuentes
    
$this->SetFillColor(224,235,255);
    
$this->SetTextColor(0);
    
$this->SetFont('');
    
//Datos
    
$fill=false;
    foreach(
$data as $row)
    {
        
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        
$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
        
$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
        
$this->Ln();
        
$fill=!$fill;
    }
    
$this->Cell(array_sum($w),0,'','T');
}
}

$pdf=new PDF();
//Títulos de las columnas
$header=array('País','Capital','Superficie (km2)','Pobl. (en miles)');
//Carga de datos
$data=$pdf->LoadData('paises.txt');
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->BasicTable($header,$data);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>
El archivo a cargar se llamara paises.txt y contendra esto:

Austria;Vienna;83859;8075
Belgium;Brussels;30518;10192
Denmark;Copenhagen;43094;5295
Finland;Helsinki;304529;5147
France;Paris;543965;58728
Germany;Berlin;357022;82057
Greece;Athens;131625;10511
Ireland;Dublin;70723;3694
Italy;Roma;301316;57563
Luxembourg;Luxembourg;2586;424
Netherlands;Amsterdam;41526;15654
Portugal;Lisbon;91906;9957
Spain;Madrid;504790;39348
Sweden;Stockholm;410934;8839
United Kingdom;London;243820;58862