Re: Pdf - R&os Bueno, haber que tal esto
Lo primero la estructura de la tabla de pruebas Código PHP: CREATE TABLE `tbl_pruebas` ( `Id_key` int(3) NOT NULL auto_increment, `Campo1` varchar(6) NOT NULL default '', `Campo2` varchar(6) NOT NULL default '', `Campo3` varchar(6) NOT NULL default '', `Campo4` varchar(6) NOT NULL default '', `Campo5` varchar(6) NOT NULL default '', `Campo6` varchar(6) NOT NULL default '', `Campo7` varchar(6) NOT NULL default '', PRIMARY KEY (`Id_key`) ) TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=0 ;
Y ahora el código para quenerar el fichero Código PHP: <? //=================================================================== //Raquerir la clase 'pdf' para la generación de ficheros pdf require('../clases/pdf/class.fpdf.php');
///////////////////////////////////////////////////////////// // Conectar a base de datos local ///////////////////////////////////////////////////////////// DEFINE (DB_USER, "MI USUARIO EN EL SERVIDOR DB"); DEFINE (DB_PASSWORD, "MI CONTRASEÑA DB"); DEFINE (DB_HOST, "127.0.0.1"); //conecta con servidor remoto DEFINE (DB_NAME, "NOMBRE DE LA BASE DE DATOS");
///////////////////////////////////////////////////////////// // Conexión a mysql ///////////////////////////////////////////////////////////// $db_connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(Error_Mysql_Conect_DB());
////////////////////////////////////////////////////////////// // Seleccionar db ////////////////////////////////////////////////////////////// mysql_select_db (DB_NAME);
//===================================================================
class PDF extends FPDF { function PDF($orientation='l',$unit='mm',$format='A4') { //Llama al constructor de la clase padre $this->FPDF($orientation,$unit,$format); //Iniciacin de variables $this->B=0; $this->I=0; $this->U=0; }
function Header() { //Logo $this->Image('../imagenes/logo.png',5,8,33); //Arial bold 15 $this->SetFont('Arial','B',15); //Movernos a la derecha $this->Cell(80); //T tulo $this->Cell(80,10,'Detalle de la tabla tbl_pruebas',1,0,'C'); //Salto de línea $this->Ln(15); }
//Pie de página function Footer() { //Posicin: a 1,5 cm del final $this->SetY(-15); //Arial italic 8 $this->SetFont('Arial','I',8); //Número de pgina $this->Cell(0,10,'P gina '.$this->PageNo().'/{nb}',0,0,'C'); }
//colorear la tabla y cargar datos 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(20,20,20,20,20,20,160); 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=0;
$Sql = "SELECT * FROM tbl_pruebas ORDER BY Campo1 ASC"; $Query = mysql_query($Sql); //Bucle para generar los datos while($row = mysql_fetch_array($Query)) { $this->Cell($w[0],6,$row['Campo1'],'LR',0,'C',$fill); $this->Cell($w[1],6,$row['Campo2'],'LR',0,'C',$fill); $this->Cell($w[2],6,$row['Campo3'],'LR',0,'C',$fill); $this->Cell($w[3],6,$row['Campo4'],'LR',0,'C',$fill); $this->Cell($w[4],6,$row['Campo5'],'LR',0,'C',$fill); $this->Cell($w[5],6,$row['Campo6'],'LR',0,'C',$fill); $this->Cell($w[6],6,$row['Campo7'],'LR',0,'L',$fill); $this->Ln(); $fill=!$fill; //contador de línea para establecer líneas por página y cabecera $cuenta +=; if($cuenta == 25) { $this->Cell(array_sum($w),0,'','T'); $this->Ln(); $this->AddPage(); $this->SetFillColor(255,0,0); $this->SetTextColor(255); $this->SetDrawColor(128,0,0); $this->SetLineWidth(.3); $this->SetFont('','B'); //Cabecera $w=array(20,20,20,20,20,20,160); for($i=0;$i<count($header);$i++) $this->Cell($w[$i],7,$header[$i],1,0,'C',1); $this->Ln(); $cuenta = 0; //Restauración de colores y fuentes $this->SetFillColor(224,235,255); $this->SetTextColor(0); $this->SetFont(''); } } $this->Cell(array_sum($w),0,'','T'); }
} //==============================================================
//Iniciar documento PDF $pdf=new PDF(); $pdf->AliasNbPages(); //T tulos de las columnas $header=array('Campo1','Campo2','Campo3','Campo4','Campo5','Campo6','Campo7'); $pdf->SetFont('Arial','',10); $pdf->AddPage(); $pdf->FancyTable($header,$data); $pdf->Output();
?> Si deas que la página tenga menos líneas que las que te he establecido, busca lo siguiente Código PHP: if($cuenta == 25) { $this->Cell(array_sum($w),0,'','T'); $this->Ln(); $this->AddPage(); $this->SetFillColor(255,0,0); $this->SetTextColor(255); $this->SetDrawColor(128,0,0); $this->SetLineWidth(.3); $this->SetFont('','B'); //Cabecera $w=array(20,20,20,20,20,20,160); for($i=0;$i<count($header);$i++) $this->Cell($w[$i],7,$header[$i],1,0,'C',1); $this->Ln(); $cuenta = 0; //Restauración de colores y fuentes $this->SetFillColor(224,235,255); $this->SetTextColor(0); $this->SetFont(''); }
y cambian el valor "25" del condicional por el número de líneas que desees. |