113 lines
4.2 KiB
PHP
113 lines
4.2 KiB
PHP
<?php
|
|
/*
|
|
* test_pdf_extract.php
|
|
* ------------------------------------------------------------------
|
|
* Pagina di test ISOLATA per verificare l'estrazione PDF.
|
|
* Da aprire nel browser passando ?id=<template_id> di un template PDF
|
|
* che ha già i riquadri tracciati e un sample_pdf caricato.
|
|
*
|
|
* http://localhost/trf_certest/test_pdf_extract.php?id=67
|
|
*
|
|
* Mostra, per ogni campo mappato con pdf_regions, il valore estratto dal
|
|
* PDF di esempio. Serve a confermare che i riquadri prendano i dati giusti
|
|
* PRIMA di collegare l'inserimento in datadb.
|
|
*
|
|
* DA CANCELLARE dopo il test.
|
|
* ------------------------------------------------------------------
|
|
*/
|
|
|
|
include('include/headscript.php');
|
|
require_once __DIR__ . '/pdf_extract_lib.php';
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
|
|
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
|
if ($id <= 0) {
|
|
die('Passa ?id=<template_id> di un template PDF.');
|
|
}
|
|
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
|
|
// Template
|
|
$stmt = $pdo->prepare("SELECT id, name, source_type, sample_pdf FROM excel_templates WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$template = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$template) {
|
|
die('Template non trovato.');
|
|
}
|
|
|
|
if (strtoupper($template['source_type']) !== 'PDF') {
|
|
die('Questo template non è di tipo PDF (source_type = ' . htmlspecialchars($template['source_type']) . ').');
|
|
}
|
|
|
|
if (empty($template['sample_pdf'])) {
|
|
die('Nessun PDF di esempio caricato per questo template.');
|
|
}
|
|
|
|
// Percorso del PDF di esempio (adatta se la cartella è diversa)
|
|
$pdfPath = __DIR__ . '/pdftemplates/' . $template['sample_pdf'];
|
|
|
|
// Mapping con eventuali riquadri
|
|
$stmt = $pdo->prepare("
|
|
SELECT id, field_label, data_type, pdf_regions
|
|
FROM template_mapping
|
|
WHERE template_id = ?
|
|
ORDER BY field_order ASC, id ASC
|
|
");
|
|
$stmt->execute([$id]);
|
|
$mappings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
echo "<h2>Test estrazione PDF — Template: " . htmlspecialchars($template['name']) . "</h2>";
|
|
echo "<p><strong>PDF:</strong> " . htmlspecialchars($template['sample_pdf']) . "<br>";
|
|
echo "<strong>Percorso:</strong> " . htmlspecialchars($pdfPath) . "</p>";
|
|
|
|
if (!is_file($pdfPath)) {
|
|
die('<p style="color:red">❌ File PDF non trovato al percorso indicato. Correggi $pdfPath nel test.</p>');
|
|
}
|
|
|
|
echo "<p><strong>Binario pdftotext:</strong> " . htmlspecialchars(pdftotext_binary()) . "</p>";
|
|
|
|
try {
|
|
$values = pdf_extract_values($pdfPath, $mappings);
|
|
} catch (Throwable $e) {
|
|
die('<p style="color:red">❌ Errore estrazione: ' . htmlspecialchars($e->getMessage()) . '</p>');
|
|
}
|
|
|
|
echo '<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;font-family:sans-serif;font-size:14px;">';
|
|
echo '<tr style="background:#f0f0f0;"><th>ID</th><th>Campo</th><th>Tipo</th><th>Pagina</th><th>Riquadro value</th><th>VALORE ESTRATTO</th></tr>';
|
|
|
|
foreach ($mappings as $m) {
|
|
$hasRegions = !empty($m['pdf_regions']);
|
|
$regions = $hasRegions ? json_decode($m['pdf_regions'], true) : null;
|
|
|
|
$page = $regions['page'] ?? '';
|
|
$valueRect = isset($regions['value'])
|
|
? sprintf(
|
|
'x=%.0f y=%.0f w=%.0f h=%.0f',
|
|
$regions['value']['x'],
|
|
$regions['value']['y'],
|
|
$regions['value']['w'],
|
|
$regions['value']['h']
|
|
)
|
|
: '—';
|
|
|
|
$extracted = $values[(int)$m['id']] ?? '';
|
|
$rowBg = $hasRegions ? ($extracted !== '' ? '#eaffea' : '#fff3cd') : '#ffffff';
|
|
|
|
echo '<tr style="background:' . $rowBg . ';">';
|
|
echo '<td>' . (int)$m['id'] . '</td>';
|
|
echo '<td>' . htmlspecialchars($m['field_label'] ?? '') . '</td>';
|
|
echo '<td>' . htmlspecialchars($m['data_type'] ?? '') . '</td>';
|
|
echo '<td style="text-align:center;">' . htmlspecialchars((string)$page) . '</td>';
|
|
echo '<td style="font-family:monospace;font-size:12px;">' . htmlspecialchars($valueRect) . '</td>';
|
|
echo '<td><strong>' . htmlspecialchars($extracted) . '</strong></td>';
|
|
echo '</tr>';
|
|
}
|
|
|
|
echo '</table>';
|
|
|
|
echo '<p style="color:#666;margin-top:15px;">Verde = valore estratto. Giallo = riquadro presente ma nessun testo trovato. Bianco = campo senza riquadro PDF.</p>';
|
|
echo '<p style="color:#a00;"><strong>Ricorda di cancellare questo file dopo il test.</strong></p>';
|