Вы находитесь на странице: 1из 9

Como crear un pdf a partir de una consula a una base de datos Las imgenes son:

ENCABEZADO

PIE DE PAGINA

ENCABEZADO

<?php include("PDF_MC_Table.php"); include '../Valid.php'; //define('FPDF_FONTPATH', 'font/'); // Encabezado del pdf //$pdf = new PDF_Mc_Table(); /** * 1.- array con los tamaos de cada una de las celda ej: array(celda1->7,celda2->15) o array(7,15) * 2.- array de alineacion de cada una de las celds los cuales pueden ser: * */ $db=Conect(); $pdf = new PDF_Mc_Table('L','mm','legal'); $pdf->AddPage(); $pdf->SetFont('arial','B', 8); //un arreglo con su medida a lo ancho $pdf->SetWidths(array(7, 20, 20, 30, 20, 40, 15, 9, 18, 18, 25, 17, 30, 40,22)); //un arreglo con alineacion de cada celda $pdf->SetAligns(array('r','r','r','r','r','r','r','r','c','c','r','r','r','C')); //OTro arreglo pero con el contenido //utf8_decode es para que escriba bien //los acentos. $header=array(utf8_decode("N"), utf8_decode('PATERNO'), utf8_decode('MATERNO'), utf8_decode('NOMBRES'), utf8_decode('CODIGO'), utf8_decode('CARGO'), utf8_decode('CI'), utf8_decode('EXP'), utf8_decode('FECHA INCOR.'), utf8_decode('FECHA FIN'), utf8_decode('FUENTE'), utf8_decode('SALARIO'), utf8_decode('MODALIDAD DE INGRESO'), utf8_decode('UNIDAD EJECUTORA'), utf8_decode('CONDICION')); //$fila=$pdf->Titulo_table($header); //print_r($fila." +fila1+"); $sql="SELECT contrato.*,cargo.Cargo,cargo.Codigo AS Cargo_Cod,Fuente,Sueldo,UnidadEjecutora,procesos.Tipo,persona.*, CASE WHEN TRIM(cargo.Descripcion)<>'' THEN cargo.Descripcion ELSE cargo.Cargo END AS Cargo_Desc FROM contrato INNER JOIN persona ON contrato.idPersona=persona.idPersona INNER JOIN cargo ON contrato.idCargo=cargo.idCargo INNER JOIN fuente ON contrato.idFuente=fuente.idFuente

INNER JOIN nivelsalarial ON contrato.Haber_Basico=nivelsalarial.idSalario INNER JOIN procesos ON contrato.Proceso=procesos.idProceso LEFT JOIN ejecutor ON contrato.idEjecutor=ejecutor.idEjecutor WHERE (contrato.Estado=1 OR contrato.Estado=0)AND Month(contrato.Fecha_Incor)=1 AND Year(contrato.Fecha_Incor)=2011 AND contrato.DA=1 Order By contrato.Fecha_Incor,persona.appaterno,persona.apmaterno,persona.pnombre,persona.snombre"; $arr=$db->GetAll($sql); $Cont=1; $sw=1;$sw2=0; $pdf->Titulo_table($header); foreach($arr as $val) { $data=array(utf8_decode($Cont), utf8_decode($val["ApPaterno"]), utf8_decode($val["ApMaterno"]), utf8_decode($val["PNombre"]." ".$val["SNombre"]), utf8_decode($val["Cargo_Cod"]), utf8_decode($val["Cargo_Desc"]), utf8_decode($val["CI"]), utf8_decode($val["Exp"]), utf8_decode($val["Fecha_Incor"]), utf8_decode($val["Fecha_Baja"]), utf8_decode($val["Motivo_Baja"]), utf8_decode($val["Sueldo"]), utf8_decode($val["Tipo"]), utf8_decode($val["UnidadEjecutora"]), utf8_decode($val["Condicion"]) ); $pdf->Row2($header, $data); $Cont++; } // fin y entrega del pdf //$pdf->Output("ejemplo.pdf","F"); $pdf->Output(); echo "<script language='javascript'>window.open('ejemplo.pdf','_self','');</script>"; exit; ?>

La Librera PDF_MC_Table.php
<?php require 'fpdf.php'; class PDF_MC_Table extends FPDF { var $widths;

var $aligns; function SetWidths($w) { //Set the array of column widths $this->widths=$w; } function SetAligns($a) { //Set the array of column alignments $this->aligns=$a; } function fill($f) { //juego de arreglos de relleno $this->fill=$f; } function Row2($header,$data) { //Calculate the height of the row $nb=0; for($i=0;$i<count($header);$i++) $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i])); $h=5*$nb; //Issue a page break first if needed //$nuevaPag=$this->CheckPageBreak($h); if($this->GetY()+$h>$this->PageBreakTrigger) { $this->AddPage($this->CurOrientation); $this->SetFont('Arial','B',9); $this->SetTextColor(0); $nb=0; for($i=0;$i<count($header);$i++) $nb=max($nb,$this->NbLines($this->widths[$i],$header[$i])); $h=5*$nb; for($i=0;$i<count($header);$i++) { $w=$this->widths[$i]; $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L'; //Save the current position $x=$this->GetX(); $y=$this->GetY(); $t=$y;

//Draw the border $this->Rect($x,$y,$w,$h,$style); //Print the text $this->MultiCell($w,5,$header[$i],'LTR',$a,$fill); //Put the position to the right of the cell $this->SetXY($x+$w,$y); } $this->Ln($h); } $nb=0; for($i=0;$i<count($data);$i++) $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i])); $h=5*$nb; $this->SetFont('Arial','',7); $this->SetTextColor(0); //Draw the cells of the row for($i=0;$i<count($data);$i++) { $w=$this->widths[$i]; $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L'; //Save the current position $x=$this->GetX(); $y=$this->GetY(); $t=$y; //Draw the border $this->Rect($x,$y,$w,$h,$style); //Print the text $this->MultiCell($w,5,$data[$i],'LTR',$a,$fill); //Put the position to the right of the cell $this->SetXY($x+$w,$y); } //Go to the next line $this->Ln($h); } function NbLines($w,$txt) { //Computes the number of lines a MultiCell of width w will take $cw=&$this->CurrentFont['cw']; if($w==0) $w=$this->w-$this->rMargin-$this->x; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize; //$s=str_replace("\r",'',$txt); $s=str_replace("\r",'',$txt); $nb=strlen($s); if($nb>0 and $s[$nb-1]=="\n") $nb;

$sep=-1; $i=0; $j=0; $l=0; $nl=1; while($i<$nb) { $c=$s[$i]; if($c=="\n") { $i++; $sep=-1; $j=$i; $l=0; $nl++; continue; } if($c==' ') $sep=$i; $l+=$cw[$c]; if($l>$wmax) { if($sep==-1) { if($i==$j) $i++; } else $i=$sep+1; $sep=-1; $j=$i; $l=0; $nl++; } else $i++; } return $nl; } El pie de pagina function Footer() { //Posicin: a 1,5 cm del final $this->SetY(-15);

//Arial italic 8 $this->SetTextColor(0); $this->SetFont('Arial','I',8); //Nmero de pgina $this->Cell(0,10,' UGRH - Pagina '.$this->PageNo(),0,0,'C'); } La cabecera function Header() { //Logo $this->Image('INRApdf.jpg',10,8,15); //$this->Image('INRApdf.png',10,8,33); //Arial bold 15 $this->SetFont('Arial','B',12); $this->SetTextColor(0); //Movernos a la derecha $this->Cell(80); //Ttulo /*$this->Cell(150,10,'CUADRO RESUMEN DE '.$opcion,0,0,'C'); $this->Cell(150,20,'CORRESPONDIENTES AL MES DE '.$mes.' '.$gestion,0,0,'C'); $this->Cell(150,30,' '.$DA.' ',0,0,'C');*/ $this->Cell(150,10,'CUADRO RESUMEN DE ALTAS',0,0,'C'); $this->Cell(-150,20,'CORRESPONDIENTES AL MES DE ENERO 2011',0,0,'C'); $this->Cell(150,30,'DIRECCION NACIONAL',0,0,'C'); //Salto de lnea $this->Ln(22); } function Titulo_table($data) { //Calculate the height of the row $nb=0; for($i=0;$i<count($data);$i++) $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i])); $h=5*$nb; //Emitir un salto de pgina si es necesario primero $this->CheckPageBreak($h); //Dibuje las celda de la fila $this->SetFont('Arial','B',9); $this->SetTextColor(0); for($i=0;$i<count($data);$i++) { $w=$this->widths[$i]; $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';

//Guardar la posicin actual $x=$this->GetX(); $y=$this->GetY(); $t=$y; //Draw the border $this->Rect($x,$y,$w,$h,$style); //Imprimir el texto $this->MultiCell($w,5,$data[$i],'LTR',$a,$fill); //Ponga la posicin a la derecha de la celda $this->SetXY($x+$w,$y); } //Ir a la siguiente lnea $this->Ln($h); } } ?>

Вам также может понравиться