158 lines
5.8 KiB
PHP
158 lines
5.8 KiB
PHP
<?php
|
|
// export_html.php - Geração de Documento Word XML (WordprocessingML) v3
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
ob_start();
|
|
|
|
|
|
// 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) {
|
|
// Em caso de erro, envie um cabeçalho de erro e uma mensagem clara.
|
|
header("HTTP/1.1 500 Internal Server Error");
|
|
die("Erro no servidor: Não foi possível conectar ao banco de dados. Detalhe: " . $e->getMessage());
|
|
}
|
|
|
|
// --- Funções Auxiliares ---
|
|
function xml_escape($string) {
|
|
return htmlspecialchars($string, ENT_XML1, 'UTF-8');
|
|
}
|
|
|
|
// --- Preparar variáveis ---
|
|
$filename = "instalacao_" . $installation['id'] . ".doc";
|
|
|
|
// --- Geração do XML ---
|
|
|
|
$xml_template = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
<?mso-application progid="Word.Document"?>
|
|
<w:wordDocument
|
|
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
|
|
xmlns:v="urn:schemas-microsoft-com:vml"
|
|
xmlns:o="urn:schemas-microsoft-com:office:office"
|
|
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
|
|
xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
|
|
xmlns:w10="urn:schemas-microsoft-com:office:word"
|
|
xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
|
|
|
|
<w:body>
|
|
%s
|
|
</w:body>
|
|
</w:wordDocument>';
|
|
|
|
$xml_content = '';
|
|
|
|
// Título
|
|
$xml_content .= '<w:p><w:r><w:rPr><w:b/><w:sz w:val="32"/></w:rPr><w:t>Relatório de Instalação</w:t></w:r></w:p><w:p/>';
|
|
|
|
// Função para criar linhas da tabela
|
|
function create_table_rows($data) {
|
|
$rows = '';
|
|
foreach ($data as $label => $value) {
|
|
$label = xml_escape($label);
|
|
$value = xml_escape($value);
|
|
$rows .= <<<XML
|
|
<w:tr>
|
|
<w:tc><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>{$label}</w:t></w:r></w:p></w:tc>
|
|
<w:tc><w:p><w:r><w:t>{$value}</w:t></w:r></w:p></w:tc>
|
|
</w:tr>
|
|
XML;
|
|
}
|
|
return $rows;
|
|
}
|
|
|
|
// Tabela de Informações
|
|
$info_data = [
|
|
"ID da Instalação" => $installation['id'],
|
|
"Cliente" => $installation['client_name'],
|
|
"Endereço" => $installation['address'],
|
|
"Técnico" => $installation['technician_name'],
|
|
"Data" => date("d/m/Y H:i", strtotime($installation['created_at'])),
|
|
"Status" => ucfirst($installation['status'])
|
|
];
|
|
$xml_content .= '<w:tbl><w:tblPr><w:tblW w:w="10000" w:type="pct"/></w:tblPr>' . create_table_rows($info_data) . '</w:tbl><w:p/>';
|
|
|
|
// Tabela de Dados Elétricos
|
|
$electric_data = [
|
|
"Tensão (V)" => $installation['voltage'] ?? 'N/A',
|
|
"Fase-Neutro-Terra" => $installation['phase_neutral_ground'] ?? 'N/A',
|
|
"Saída do Disjuntor" => $installation['breaker_output'] ?? 'N/A'
|
|
];
|
|
$xml_content .= '<w:p><w:r><w:rPr><w:b/><w:sz w:val="28"/></w:rPr><w:t>Dados Elétricos</w:t></w:r></w:p>';
|
|
$xml_content .= '<w:tbl><w:tblPr><w:tblW w:w="10000" w:type="pct"/></w:tblPr>' . create_table_rows($electric_data) . '</w:tbl><w:p/>';
|
|
|
|
// Observações
|
|
$observations = xml_escape($installation['observations'] ?? 'Nenhuma observação.');
|
|
$xml_content .= '<w:p><w:r><w:rPr><w:b/><w:sz w:val="28"/></w:rPr><w:t>Observações</w:t></w:r></w:p>';
|
|
$xml_content .= "<w:p><w:r><w:t>{$observations}</w:t></w:r></w:p>";
|
|
|
|
// Imagens
|
|
if (!empty($images)) {
|
|
$xml_content .= '<w:p><w:r><w:br w:type="page"/></w:r></w:p>';
|
|
$xml_content .= '<w:p><w:r><w:rPr><w:b/><w:sz w:val="28"/></w:rPr><w:t>Imagens da Instalação</w:t></w:r></w:p>';
|
|
|
|
foreach ($images as $image) {
|
|
$image_path = __DIR__ . '/' . $image['image_path'];
|
|
if (file_exists($image_path)) {
|
|
$image_data = base64_encode(file_get_contents($image_path));
|
|
list($width_px, $height_px) = getimagesize($image_path);
|
|
|
|
$width_pt = $width_px * 0.75; // Convert pixels to points
|
|
$height_pt = $height_px * 0.75;
|
|
|
|
$max_width_pt = 450; // Largura máxima de aprox. 6 polegadas
|
|
if ($width_pt > $max_width_pt) {
|
|
$ratio = $max_width_pt / $width_pt;
|
|
$width_pt = $max_width_pt;
|
|
$height_pt = $height_pt * $ratio;
|
|
}
|
|
|
|
$shape_id = '_x0000_i' . substr(uniqid(), 5);
|
|
$image_rid = 'rId' . substr(uniqid(), 5);
|
|
|
|
$xml_content .= '
|
|
<w:p>
|
|
<w:r>
|
|
<w:pict>
|
|
<w:binData w:name="wordml://' . $image_rid . '.jpg">' . $image_data . '</w:binData>
|
|
<v:shape id="' . $shape_id . '" type="#_x0000_t75" style="width:' . round($width_pt) . 'pt;height:' . round($height_pt) . 'pt">
|
|
<v:imagedata src="wordml://' . $image_rid . '.jpg" o:title="image"/>
|
|
</v:shape>
|
|
</w:pict>
|
|
</w:r>
|
|
</w:p>
|
|
<w:p/>';
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Enviar para o navegador ---
|
|
header("Content-Type: application/vnd.ms-word; charset=UTF-8");
|
|
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
|
|
echo sprintf($xml_template, $xml_content);
|
|
ob_end_flush();
|
|
?>
|