How can I add a header that is repeated in all the pages?

1

As my question says, I use the tcpdf library and here I leave the code:

<?php
	require_once('tcpdf/config/lang/eng.php');
	require_once('tcpdf/tcpdf.php');
	require_once('../config.php');

	$pdf = new TCPDF('P', 'mm', 'legal', true, 'UTF-8', false);
	$pdf->SetTitle('PDF Autogenerado en PHP'); //Titlo del pdf
	$pdf->setPrintHeader(false); //No se imprime cabecera
	$pdf->setPrintFooter(false); //No se imprime pie de pagina
	$pdf->SetMargins(10, 20, 10, false); //Se define margenes izquierdo, alto, derecho
	$pdf->SetAutoPageBreak(true, 5); //Se define un salto de pagina con un limite de pie de pagina
	$pdf->addPage();
	
 if(isset($_GET['id']))
    {
		$id=$_GET['id'];
	
				
	}

	$sql = "SELECT * FROM acarga WHERE bl3 = '$id' ORDER by cod_it ";
	$cosas = $connex->query($sql);
	$html = '';
	$item = 1;
	foreach($cosas as $row){
		$bl = $row['bl3'];
		$item = $row['cod_it'];
		$conductor = $row['noap'];
		$unidad = $row['uni_ca'];
		$placa = $row['pla_ca'];
		$tipopa = $row['tipo_pa'];		
		$registro = date('d/m/Y', strtotime($row['fecha']));
		$estado = $row['estado'];
		$hora = date('h:i A', strtotime($row['hora']));
		$imagen = $row['ruta_imagen'];
		$destino = $row['dest'];
		$coment = $row['comenta'];
		//$barcode = $row['cosa_codigo_barra'];
		//$barcode = $pdf->serializeTCPDFtagParameters(array($barcode, 'C128', '', '', 72, 25, 0.5, array('position'=>'S', 'border'=>false, 'padding'=>2, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>7, 'stretchtext'=>6), 'N'));

		$html .= '
		<table border="1" cellpadding="5">
					<tr>
						<td bgcolor="#E6E6E6"><b>Caso: </b></td>
						<td>'.$bl.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Item: </b></td>
						<td>'.$item.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Unidad/Camion: </b></td>
						<td>'.$unidad.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Placa del camión: </b></td>
						<td>'.$placa.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Tipo de Mesa/chasis: </b></td>
						<td>'.$tipopa.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Conductor: </b></td>
						<td>'.$conductor.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Fecha: </b></td>
						<td>'.$registro.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Hora: </b></td>
						<td>'.$hora.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Estado: </b></td>
						<td>'.$estado.'<font color="red">*</font> </td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Destino: </b></td>
						<td>'.$destino.'</td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Foto: </b></td>
						<td><img src="../img/'.$imagen.'"width="250px" height="180px"></td>
					</tr>
					<tr>
						<td bgcolor="#E6E6E6"><b>Comentario: </b></td>
						<td>'.$coment.'</td>
					</tr>
				 </table><br><br>
			
				 ';
				 

		$item = $item+1;
	}

	$pdf->SetFont('Helvetica', '', 10);
	$pdf->writeHTML($html, true, 0, true, 0);

	$pdf->lastPage();
	$pdf->output('Reporte.pdf', 'I');
?>
    
asked by leonardo rodriguez2 07.06.2018 в 07:00
source

1 answer

0

Basically it does not print because in the configuration you are setting to "false" the impression of both header and footer:

$pdf->setPrintHeader(false); //No se imprime cabecera
$pdf->setPrintFooter(false); //No se imprime pie de pagina

Both lines must be set to true, that is, change the previous lines to the following:

$pdf->setPrintHeader(true); //Ahora si imprimirá cabecera
$pdf->setPrintFooter(true); //Ahora si imprimirá pie de página

Also remember to add the header content:

$PDF_HEADER_TITLE="Título cabecera"; // Colocas el titulo de tu PDF por ejemplo
$PDF_HEADER_STRING="Descripción de la cabecera"; // Lo que deseas que esté en cabecera
$PDF_HEADER_LOGO="imagen"; 

$this->pdf->SetHeaderData($PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING); // Se establece el header en el documento.

Observation: $PDF_HEADER_LOGO you can skip if you do not want the header to carry an image, if you want an image then leave this line as is and in order for the image to work it must be inside the folder images that the same library brings by default.

I hope it helps. Greetings!

    
answered by 07.06.2018 / 10:45
source