<?php session_start(); header("Cache-control: private"); include("../bd/conexion.php"); $conexion = new mysqli($host, $user, $pw, $bd); if ($conexion->connect_error) { die("Error de conexión: " . $conexion->connect_error); } // Configurar headers para Excel header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=Entregas_ST.xls"); header("Pragma: no-cache"); header("Expires: 0"); // Obtener parámetros $cedula = isset($_POST["cedula"]) ? $_POST["cedula"] : ""; $fecha_inicio = isset($_POST["fechaXls1"]) ? $_POST["fechaXls1"] : ""; $fecha_fin = isset($_POST["fechaXls2"]) ? $_POST["fechaXls2"] : ""; // Construir condición WHERE $conditions = array("1=1"); if (!empty($cedula)) { $conditions[] = "cedula = '".$conexion->real_escape_string($cedula)."'"; } if (!empty($fecha_inicio) && !empty($fecha_fin)) { $conditions[] = "fEntrega BETWEEN '".$conexion->real_escape_string($fecha_inicio)."' AND '".$conexion->real_escape_string($fecha_fin)."'"; } $where_clause = implode(" AND ", $conditions); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Reporte de Entregas ST</title> <style> .firma-img { max-width: 100px; max-height: 50px; } </style> </head> <body> <center> <table border="1"> <tr> <th>ID</th> <th>Cédula</th> <th>Nombre</th> <th>Centro de Costo</th> <th>Cargo</th> <th>Fecha Entrega</th> <th>Tipo Entrega</th> <th>Estado</th> <th>Tipo Devolutivo</th> <th>Código EPP</th> <th>EPP</th> <th>Cantidad</th> <th>Firma</th> <th>Entregó</th> </tr> <?php $query = "SELECT * FROM entregasst WHERE $where_clause ORDER BY fEntrega DESC"; $result = $conexion->query($query); if (!$result) { echo "<tr><td colspan='14'>Error en la consulta: " . $conexion->error . "</td></tr>"; } elseif ($result->num_rows == 0) { echo "<tr><td colspan='14'>No se encontraron registros con los filtros aplicados</td></tr>"; } else { while ($row = $result->fetch_assoc()) { // Procesar la firma (BLOB) $firma_html = "Sin firma"; if (!empty($row['FIRMA'])) { // Si el BLOB comienza con 'data:image' es base64 if (strpos($row['FIRMA'], 'data:image') === 0) { $firma_html = '<img src="'.$row['FIRMA'].'" class="firma-img">'; } else { // Si es binario puro, convertirlo a base64 $firma_html = '<img src="data:image/png;base64,'.base64_encode($row['FIRMA']).'" class="firma-img">'; } } echo "<tr>"; echo "<td>".htmlspecialchars($row['ID'])."</td>"; echo "<td>".htmlspecialchars($row['cedula'])."</td>"; echo "<td>".htmlspecialchars($row['nombre'])."</td>"; echo "<td>".htmlspecialchars($row['ccosto'])."</td>"; echo "<td>".htmlspecialchars($row['cargo'])."</td>"; echo "<td>".htmlspecialchars($row['fEntrega'])."</td>"; echo "<td>".htmlspecialchars($row['tpEntrega'])."</td>"; echo "<td>".htmlspecialchars($row['tpEstado'])."</td>"; echo "<td>".htmlspecialchars($row['tpDevolutivo'])."</td>"; echo "<td>".htmlspecialchars($row['codigo'])."</td>"; echo "<td>".htmlspecialchars($row['epp'])."</td>"; echo "<td>".htmlspecialchars($row['cantidad'])."</td>"; echo "<td>".$firma_html."</td>"; // Firma como imagen echo "<td>".htmlspecialchars($row['entrego'])."</td>"; echo "</tr>"; } } ?> </table> </center> </body> </html> <?php $conexion->close(); ?>