From 716bb2a11511f40a0839b93d4ad09aaa1ef446b3 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 1 Dec 2025 23:03:27 +0000 Subject: [PATCH] versao word 4 --- export_html.php | 189 +++++++++++++++++++----------------------------- 1 file changed, 76 insertions(+), 113 deletions(-) diff --git a/export_html.php b/export_html.php index 8be546b..b0de847 100644 --- a/export_html.php +++ b/export_html.php @@ -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 = << - - Relatório de Instalação - <?php echo htmlspecialchars($installation['client_name']); ?> + + Relatório de Instalação - {htmlspecialchars($installation['client_name'])}

Relatório de Instalação

-

Detalhes do Cliente e Instalação

- - - - - - - - - - - - - - - - - - - - - - - - + + + + + +
ID da Instalação
Cliente
Endereço
Técnico Responsável
Data
Status
ID da Instalação{htmlspecialchars($installation['id'])}
Cliente{htmlspecialchars($installation['client_name'])}
Endereço{htmlspecialchars($installation['address'])}
Técnico Responsável{htmlspecialchars($installation['technician_name'])}
Data{date("d/m/Y H:i", strtotime($installation['created_at']))}
Status{ucfirst(htmlspecialchars($installation['status']))}

Dados Elétricos

- - - - - - - - - - - - + + +
Tensão (V)
Fase-Neutro-Terra
Saída do Disjuntor
Tensão (V){htmlspecialchars($installation['voltage'] ?? 'N/A')}
Fase-Neutro-Terra{htmlspecialchars($installation['phase_neutral_ground'] ?? 'N/A')}
Saída do Disjuntor{htmlspecialchars($installation['breaker_output'] ?? 'N/A')}

Observações

-

+

{nl2br(htmlspecialchars($installation['observations'] ?? 'Nenhuma observação.'))}

+HTML; - -

Imagens da Instalação

-
-
'; - } else { - echo '

Imagem não encontrada no caminho: ' . htmlspecialchars($image['image_path']) . '

'; - } - ?> -
- +if (!empty($images)) { + $images_html = '

Imagens da Instalação

'; + + foreach ($images as $i => $image) { + $image_full_path = realpath(__DIR__ . '/' . $image['image_path']); + + if (file_exists($image_full_path)) { + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $image_mime = finfo_file($finfo, $image_full_path); + finfo_close($finfo); + + $image_data = base64_encode(file_get_contents($image_full_path)); + $cid = "image" . $i . "@" . md5($image['image_path']); -
- - + // Adiciona a tag ao HTML + $images_html .= 'Imagem da Instalação ' . ($i+1) . '
'; + + // 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 .= '
'; +} + +// Finaliza o HTML +$html_content .= $images_html; +$html_content .= ''; + +// 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"; + +?>