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()); } // --- Início da Geração MHTML --- $boundary = "----=" . md5(uniqid(time())); $filename = "instalacao_" . $installation['id'] . "_" . str_replace(' ', '_', $installation['client_name']) . ".doc"; // Cabeçalhos principais para MHTML header("Content-Type: multipart/related; boundary=\"$boundary\""); header("Content-Disposition: attachment; filename=\"" . $filename . "\""); // Parte 1: Conteúdo HTML $html_content = << Relatório de Instalação - {htmlspecialchars($installation['client_name'])}

Relatório de Instalação

Detalhes do Cliente e Instalação

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){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; // Preparar a seção de imagens $images_html = ''; $image_parts = ''; 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"; ?>