How can I get the data of a query in a PDF file?

1

Greetings,

I am trying to obtain the data generated by a query in the PDF file, I do not know how to do it, that is why I request the help here, I would like the data that the query gives me when I click on the PDF button, open the PDF file with those data.

SEARCH AND REFERENCE CODE

<?php
	$TIPO_BUSQUEDA = $_POST["TIPO_BUSQUEDA"];
    function ejecuta_consulta($labusqueda)
    {
        include("conexiond.php");

        $conexion= mysqli_connect($db_host, $db_usuario, $db_contra);

        if (mysqli_connect_errno()) {
            echo "Fallo al conectar con la base de datos";
            exit();
        }

        mysqli_select_db($conexion, $db_nombre) or die("No se encuentra la base de datos.");


        $consulta = "SELECT datosbasicos.CED_PAC,datosbasicos.NOM_PAC,datosbasicos.APE_PAC,datosbasicos.SEX_PAC,datosmedicos.COD_CONSULTA,datosmedicos.ALT_PAC,datosmedicos.PESO_PAC,datosmedicos.FECHA,datosmedicos.TIPO_CONSULTA,datosmedicos.SINTOMAS,datosmedicos.OBSERV,datosmedicos.HIS_PAC,datosmedicos.MEDI_PAC,datosmedicos.OPERADO,datosmedicos.ALERGIAS FROM datosbasicos INNER JOIN datosmedicos ON datosbasicos.CED_PAC=datosmedicos.CED_PAC WHERE datosbasicos.NOM_PAC LIKE '$labusqueda' OR datosbasicos.CED_PAC LIKE '$labusqueda' OR datosmedicos.COD_CONSULTA LIKE '%$labusqueda%' OR datosbasicos.CED_PAC LIKE '$labusqueda' OR datosmedicos.FECHA LIKE '$labusqueda'  ";

        $resultados = mysqli_query($conexion, $consulta);

        $filas = array(); // Crea la variable $filas y se le asigna un array vacío
        // (Si la consulta no devuelve ningún resultado, la función por lo menos va a retornar un array vacío)

        while ($fila=mysqli_fetch_array($resultados, MYSQLI_ASSOC)) {
            $filas[] = $fila; // Añade el array $fila al final de $filas
        }

        mysqli_close($conexion);

        return $filas; // Devuelve el array $filas
    }
?>

<!DOCTYPE html>
<html>

<head>
    <title>Sistema de historias médicas - Dr. Darling Davila</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/estilo.css">
    <link href="https://fonts.googleapis.com/css?family=Lato|Roboto" rel="stylesheet">
</head>

<body>
    <?php
        $mibusqueda=$_GET["buscar"];

        $mipag=$_SERVER["PHP_SELF"];

        if ($mibusqueda!=null) {
            $pacientes = ejecuta_consulta($mibusqueda);
    ?>

        <div id="main-container">
        <img src='imagenes/header.png' class='img'>
            <table>
                <thead>
                    <tr>
                        <th>Codigo</th>
                        <th>Fecha</th>
                        <th>Cedula</th>
                        <th>Nombres</th>
                        <th>Apellidos</th>
                        <th>Sexo</th>
                        <th>Altura</th>
                        <th>Peso</th>
                        <th>Sintomas</th>
                        <th>Observaciones</th>
                        <th>Tipo de consulta</th>
                        <th>Medicamentos actuales</th>
                        <th>Alergias</th>
                        <th>Operado</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    // Si la variable $pacientes esta definida y no está vacía
                    if (isset($pacientes) && !empty($pacientes)) {
                        // Recorre cada $paciente dentro del array $pacientes
                        foreach ($pacientes as $paciente) {
                            ?>
                        <tr>
                            <td><?php echo $paciente['COD_CONSULTA'] ?></td>
                            <td><?php echo $paciente['FECHA'] ?></td>
                            <td><?php echo $paciente['CED_PAC'] ?></td>
                            <td><?php echo $paciente['NOM_PAC'] ?></td>
                            <td><?php echo $paciente['APE_PAC'] ?></td>
                            <td><?php echo $paciente['SEX_PAC'] ?></td>
                            <td><?php echo $paciente['ALT_PAC'] ?></td>
                            <td><?php echo $paciente['PESO_PAC'] ?></td>
                            <td><?php echo $paciente['SINTOMAS'] ?></td>
                            <td><?php echo $paciente['OBSERV'] ?></td>
                            <td><?php echo $paciente['TIPO_CONSULTA'] ?></td>
                            <td><?php echo $paciente['MEDI_PAC'] ?></td>
                            <td><?php echo $paciente['ALERGIAS'] ?></td>
                            <td><?php echo $paciente['OPERADO'] ?></td>
                        </tr>
                    <?php
                        }
                    } ?>
                </tbody>
        </div>
        <div>
        <form name="reporte" action="database.php" method="post">
        <input type='submit' name='generar' value='PDF' class='text-center inline-block col-md-12 espacio-arriba btn-enviar2'>
        </form>
        </div>
    <?php
        } else {
            echo("<form action='". $mipag . "' method='GET'>
                <img src='imagenes/header.png'>
                    <h2>Busqueda de paciente</h2>
                    <div class='contenedor'>
                    <select name='TIPO_BUSQUEDA' class='input-100 text-center col-md-12'>
						<option value='Cedula' selected='selected' <?PHP if($TIPO_BUSQUEDA=='Cedula'){ echo 'selected='selected'; } ?> Cedula</option>
	 					<option value='Edad' <?PHP if($TIPO_BUSQUEDA=='Edad'){ echo 'selected='selected'; } ?> Edad</option>
      					<option value='Nombre' <?PHP if($TIPO_BUSQUEDA=='Nombre'){ echo 'selected='selected'; } ? >Nombre</option>
      					<option value='Fecha' <?PHP if($TIPO_BUSQUEDA=='Fecha'){ echo 'selected='selected'; } ?> Fecha</option>
					</select>
                    <input type='text' name='buscar' class='input-100 text-center inline-block col-md-6 btn-enviar espacio-arriba'></label>

                    <input type='submit' name='enviando' value='Consulta' class='text-center inline-block col-md-12 espacio-arriba btn-enviar'>
                </div>
                </form>");
        }
     ?>

</body>

</html>

THIS IS MY DOCUMENT TO GENERATE THE PDF: Currently I only have it to make a query to the BD in general, I want it for what is generated in my query.

<?php
require('tfpdf.php');
$con=mysqli_connect('localhost','root','12345678');
mysqli_select_db($con,'pacientes');


class PDF extends TFPDF {
	function Header(){
		$this->SetFont('Arial','B',15);
		
		//dummy cell to put logo
		//$this->Cell(12,0,'',0,0);
		//is equivalent to:
		$this->Cell(12);
		
		//put logo
		$this->Image('imagenes/header.png',150,10,60);
		
		$this->Cell(1,50,'Lista de pacientes',0,1);
		
		//dummy cell to give line spacing
		//$this->Cell(0,5,'',0,1);
		//is equivalent to:
		$this->Ln(5);
		$this->SetFont('Arial','B',11);
		
		$this->SetFillColor(54,150,129);
		$this->SetDrawColor(36,99,85);
		$this->Cell(40,5,'Nombre ',1,0,'',true);
		$this->Cell(40,5,'Apellido',1,0,'',true);
		$this->Cell(25,5,'Edad',1,0,'',true);
		$this->Cell(25,5,'Sexo',1,0,'',true);
		$this->Cell(30,5,'Telefono',1,0,'',true);
		$this->Cell(30,5,'Celular',1,0,'',true);
		$this->Cell(130,5,'Direccion',1,1,'',true);

		
	}
	function Footer(){
		//add table's bottom line
		$this->Cell(190,0,'','T',1,'',true);
		
		//Go to 1.5 cm from bottom
		$this->SetY(-15);
				
		$this->SetFont('Arial','',8);
		
		//width = 0 means the cell is extended up to the right margin
		$this->Cell(0,10,'Page '.$this->PageNo()." / {pages}",0,0,'C');
	}
}


//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm


$pdf = new PDF('L','mm',array(400,200)); //use new class

//define new alias for total page numbers
$pdf->AliasNbPages('{pages}');

$pdf->SetAutoPageBreak(true,15);
$pdf->AddPage();

$pdf->SetFont('Arial','',9);
$pdf->SetDrawColor(36,99,85);

// Add a Unicode font (uses UTF-8)
$pdf->AddFont('Arial','','Arial.ttf',true);
$pdf->SetFont('Arial','',11);

$query=mysqli_query($con,"select * from datosbasicos");
while($data=mysqli_fetch_array($query)){
	$pdf->Cell(40,5,$data['NOM_PAC'],'LR',0);
	$pdf->Cell(40,5,$data['APE_PAC'],'LR',0);
	$pdf->Cell(25,5,$data['EDAD_PAC'],'LR',0);
	$pdf->Cell(25,5,$data['SEX_PAC'],'LR',0);
	$pdf->Cell(30,5,$data['TEL_PAC'],'LR',0);
	$pdf->Cell(30,5,$data['CEL_PAC'],'LR',0);
	$pdf->Cell(130,5,$data['DIR_PAC'],'LR',0);
	
	if($pdf->GetStringWidth($data['email']) > 65){
		$pdf->SetFont('Arial','',7);
		$pdf->Cell(65,5,$data['email'],'LR',0);
		$pdf->SetFont('Arial','',9);
	}else{
		$pdf->Cell(65,5,$data['email'],'LR',0);
	}
	$pdf->Cell(60,5,$data['address'],'LR',1);
}

$pdf->Output();
?>
    
asked by Pablo Pernia 23.08.2017 в 03:36
source

1 answer

0

and you have to do in your HTML a form to request the pdf to the page.php that generates the pdf.

<form name=’pdf’ method=’post’ action=’paginaquegeneraelPDF.php’>
<input type=’submit’ value=’Generar’ />

<?php
require('tfpdf.php');
$con=mysqli_connect('localhost','root','12345678');
mysqli_select_db($con,'pacientes');


class PDF extends TFPDF {
	function Header(){
		$this->SetFont('Arial','B',15);
		
		//dummy cell to put logo
		//$this->Cell(12,0,'',0,0);
		//is equivalent to:
		$this->Cell(12);
		
		//put logo
		$this->Image('imagenes/header.png',150,10,60);
		
		$this->Cell(1,50,'Lista de pacientes',0,1);
		
		//dummy cell to give line spacing
		//$this->Cell(0,5,'',0,1);
		//is equivalent to:
		$this->Ln(5);
		$this->SetFont('Arial','B',11);
		
		$this->SetFillColor(54,150,129);
		$this->SetDrawColor(36,99,85);
		$this->Cell(40,5,'Nombre ',1,0,'',true);
		$this->Cell(40,5,'Apellido',1,0,'',true);
		$this->Cell(25,5,'Edad',1,0,'',true);
		$this->Cell(25,5,'Sexo',1,0,'',true);
		$this->Cell(30,5,'Telefono',1,0,'',true);
		$this->Cell(30,5,'Celular',1,0,'',true);
		$this->Cell(130,5,'Direccion',1,1,'',true);

		
	}
	function Footer(){
		//add table's bottom line
		$this->Cell(190,0,'','T',1,'',true);
		
		//Go to 1.5 cm from bottom
		$this->SetY(-15);
				
		$this->SetFont('Arial','',8);
		
		//width = 0 means the cell is extended up to the right margin
		$this->Cell(0,10,'Page '.$this->PageNo()." / {pages}",0,0,'C');
	}
}


//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm


$pdf = new PDF('L','mm',array(400,200)); //use new class

//define new alias for total page numbers
$pdf->AliasNbPages('{pages}');

$pdf->SetAutoPageBreak(true,15);
$pdf->AddPage();

$pdf->SetFont('Arial','',9);
$pdf->SetDrawColor(36,99,85);

// Add a Unicode font (uses UTF-8)
$pdf->AddFont('Arial','','Arial.ttf',true);
$pdf->SetFont('Arial','',11);

$query=mysqli_query($con,"select * from datosbasicos");
while($data=mysqli_fetch_array($query)){
	$pdf->Cell(40,5,$data['NOM_PAC'],'LR',0);
	$pdf->Cell(40,5,$data['APE_PAC'],'LR',0);
	$pdf->Cell(25,5,$data['EDAD_PAC'],'LR',0);
	$pdf->Cell(25,5,$data['SEX_PAC'],'LR',0);
	$pdf->Cell(30,5,$data['TEL_PAC'],'LR',0);
	$pdf->Cell(30,5,$data['CEL_PAC'],'LR',0);
	$pdf->Cell(130,5,$data['DIR_PAC'],'LR',0);
	
	if($pdf->GetStringWidth($data['email']) > 65){
		$pdf->SetFont('Arial','',7);
		$pdf->Cell(65,5,$data['email'],'LR',0);
		$pdf->SetFont('Arial','',9);
	}else{
		$pdf->Cell(65,5,$data['email'],'LR',0);
	}
	$pdf->Cell(60,5,$data['address'],'LR',1);
}

    $pdf->Output("prueba.pdf",'F');
    echo "<script language='javascript'>window.open('prueba.pdf','_self','');</script>"; 

exit;

?>

When you create the pdf assuming that everything is fine, the only thing you have to do is show the PDF with the instructions that you put at the end.

    
answered by 23.08.2017 в 03:55