versao word 4

This commit is contained in:
Flatlogic Bot 2025-12-01 23:03:27 +00:00
parent 369b14c872
commit 716bb2a115

View File

@ -33,141 +33,104 @@ try {
die("Erro ao acessar o banco de dados: " . $e->getMessage());
}
// 5. Gerar o nome do arquivo para download
// --- Início da Geração MHTML ---
$boundary = "----=" . md5(uniqid(time()));
$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");
// Cabeçalhos principais para MHTML
header("Content-Type: multipart/related; boundary=\"$boundary\"");
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
?>
// Parte 1: Conteúdo HTML
$html_content = <<<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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Relatório de Instalação - {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;
}
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>
<tr><td>ID da Instalação</td><td>{htmlspecialchars($installation['id'])}</td></tr>
<tr><td>Cliente</td><td>{htmlspecialchars($installation['client_name'])}</td></tr>
<tr><td>Endereço</td><td>{htmlspecialchars($installation['address'])}</td></tr>
<tr><td>Técnico Responsável</td><td>{htmlspecialchars($installation['technician_name'])}</td></tr>
<tr><td>Data</td><td>{date("d/m/Y H:i", strtotime($installation['created_at']))}</td></tr>
<tr><td>Status</td><td>{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>
<tr><td>Tensão (V)</td><td>{htmlspecialchars($installation['voltage'] ?? 'N/A')}</td></tr>
<tr><td>Fase-Neutro-Terra</td><td>{htmlspecialchars($installation['phase_neutral_ground'] ?? 'N/A')}</td></tr>
<tr><td>Saída do Disjuntor</td><td>{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>
<p>{nl2br(htmlspecialchars($installation['observations'] ?? 'Nenhuma observação.'))}</p>
HTML;
<?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
// Preparar a seção de imagens
$images_html = '';
$image_parts = '';
if (!empty($images)) {
$images_html = '<h2 class="section-title">Imagens da Instalação</h2><div class="images-section" style="page-break-before: always;">';
foreach ($images as $i => $image) {
$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));
$cid = "image" . $i . "@" . md5($image['image_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>';
// Adiciona a tag <img> ao HTML
$images_html .= '<img src="cid:' . $cid . '" alt="Imagem da Instalação ' . ($i+1) . '"><br>';
// Cria a parte MIME para a imagem
$image_parts .= "--$boundary\r\n";
$image_parts .= "Content-Type: $image_mime\r\n";
$image_parts .= "Content-Transfer-Encoding: base64\r\n";
$image_parts .= "Content-ID: <$cid>\r\n";
$image_parts .= "Content-Location: " . basename($image['image_path']) . "\r\n\r\n";
$image_parts .= chunk_split($image_data) . "\r\n";
}
}
$images_html .= '</div>';
}
?><?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</body>
</html>
// Finaliza o HTML
$html_content .= $images_html;
$html_content .= '</div></body></html>';
// Monta a saída final
echo "--$boundary\r\n";
echo "Content-Type: text/html; charset=\"UTF-8\"\r\n";
echo "Content-Transfer-Encoding: 7bit\r\n\r\n";
echo $html_content . "\r\n";
// Adiciona as partes das imagens
echo $image_parts;
// Fecha o boundary final
echo "--$boundary--\r\n";
?>