35888-vm/export_html.php
2025-12-01 22:56:05 +00:00

174 lines
6.0 KiB
PHP

<?php
// export_html.php
// 1. Validar e obter o ID
if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) {
die("ID da instalação inválido.");
}
$installation_id = $_GET['id'];
// 2. Conectar ao banco de dados
require_once __DIR__ . '/db/config.php';
try {
$pdo = db();
// 3. Buscar dados da instalação
$stmt = $pdo->prepare("SELECT * FROM installations WHERE id = :id");
$stmt->bindParam(':id', $installation_id, PDO::PARAM_INT);
$stmt->execute();
$installation = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$installation) {
die("Instalação não encontrada.");
}
// 4. Buscar imagens da instalação
$img_stmt = $pdo->prepare("SELECT image_path FROM installation_images WHERE installation_id = :id ORDER BY id ASC");
$img_stmt->bindParam(':id', $installation_id, PDO::PARAM_INT);
$img_stmt->execute();
$images = $img_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
die("Erro ao acessar o banco de dados: " . $e->getMessage());
}
// 5. Gerar o nome do arquivo para download
$filename = "instalacao_" . $installation['id'] . "_" . str_replace(' ', '_', $installation['client_name']) . ".doc";
// 6. Enviar cabeçalhos para forçar o download
header("Content-Type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// 7. Iniciar a geração do conteúdo HTML
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Relatório de Instalação - <?php echo htmlspecialchars($installation['client_name']); ?></title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
}
.container {
width: 90%;
margin: 0 auto;
}
h1 {
color: #0056b3;
border-bottom: 2px solid #f0f0f0;
padding-bottom: 10px;
}
.details-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
.details-table td {
padding: 8px;
border: 1px solid #ddd;
}
.details-table td:first-child {
font-weight: bold;
background-color: #f9f9f9;
width: 25%;
}
.images-section img {
max-width: 400px;
margin: 10px;
border: 1px solid #ccc;
padding: 5px;
}
.section-title {
font-size: 1.5em;
color: #0056b3;
margin-top: 20px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Relatório de Instalação</h1>
<h2 class="section-title">Detalhes do Cliente e Instalação</h2>
<table class="details-table">
<tr>
<td>ID da Instalação</td>
<td><?php echo htmlspecialchars($installation['id']); ?></td>
</tr>
<tr>
<td>Cliente</td>
<td><?php echo htmlspecialchars($installation['client_name']); ?></td>
</tr>
<tr>
<td>Endereço</td>
<td><?php echo htmlspecialchars($installation['address']); ?></td>
</tr>
<tr>
<td>Técnico Responsável</td>
<td><?php echo htmlspecialchars($installation['technician_name']); ?></td>
</tr>
<tr>
<td>Data</td>
<td><?php echo date("d/m/Y H:i", strtotime($installation['created_at'])); ?></td>
</tr>
<tr>
<td>Status</td>
<td><?php echo ucfirst(htmlspecialchars($installation['status'])); ?></td>
</tr>
</table>
<h2 class="section-title">Dados Elétricos</h2>
<table class="details-table">
<tr>
<td>Tensão (V)</td>
<td><?php echo htmlspecialchars($installation['voltage'] ?? 'N/A'); ?></td>
</tr>
<tr>
<td>Fase-Neutro-Terra</td>
<td><?php echo htmlspecialchars($installation['phase_neutral_ground'] ?? 'N/A'); ?></td>
</tr>
<tr>
<td>Saída do Disjuntor</td>
<td><?php echo htmlspecialchars($installation['breaker_output'] ?? 'N/A'); ?></td>
</tr>
</table>
<h2 class="section-title">Observações</h2>
<p><?php echo nl2br(htmlspecialchars($installation['observations'] ?? 'Nenhuma observação.')); ?></p>
<?php if (!empty($images)): ?>
<h2 class="section-title">Imagens da Instalação</h2>
<div class="images-section" style="page-break-before: always;">
<?php foreach ($images as $image):
// Construir caminho absoluto de forma mais robusta
$image_full_path = realpath(__DIR__ . '/' . $image['image_path']);
if (file_exists($image_full_path)) {
// Tentar obter o tipo MIME
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$image_mime = finfo_file($finfo, $image_full_path);
finfo_close($finfo);
// Obter conteúdo e codificar
$image_data = base64_encode(file_get_contents($image_full_path));
// Exibir imagem incorporada
echo '<img src="data:' . $image_mime . ';base64,' . $image_data . '" alt="Imagem da Instalação"><br>';
} else {
echo '<p style="color: red;">Imagem não encontrada no caminho: ' . htmlspecialchars($image['image_path']) . '</p>';
}
?><?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</body>
</html>