From a19cdf60740a2423d6f278faab6d8101112aedf9 Mon Sep 17 00:00:00 2001 From: solocla Date: Tue, 18 Aug 2026 16:00:06 +0200 Subject: [PATCH] pdf test routine --- .gitignore | 4 + ...815054854_add_pdf_support_to_templates.php | 48 ++ ...0815055418_add_pdf_to_source_type_enum.php | 24 + public/userarea/import_dashboard.php | 4 +- public/userarea/import_pdf.php | 321 +++++++++ public/userarea/include/navbar.php | 2 +- public/userarea/insert_template_xls.php | 6 + .../userarea/mapping_template_xls_scheme2.php | 112 +++- public/userarea/pdf_extract_lib.php | 241 +++++++ public/userarea/pdf_mapping.js | 631 ++++++++++++++++++ public/userarea/process_import_pdf.php | 256 +++++++ public/userarea/save_pdf_regions.php | 143 ++++ public/userarea/schemi_base_response.json | 6 + public/userarea/templates_dashboard.php | 8 + public/userarea/test_pdf_extract.php | 112 ++++ public/userarea/testpoppler.php | 21 + public/userarea/update_pdf_page_meta.php | 57 ++ public/userarea/upload_pdf_example.php | 95 +++ 18 files changed, 2077 insertions(+), 14 deletions(-) create mode 100644 db/migrations/20260815054854_add_pdf_support_to_templates.php create mode 100644 db/migrations/20260815055418_add_pdf_to_source_type_enum.php create mode 100644 public/userarea/import_pdf.php create mode 100644 public/userarea/pdf_extract_lib.php create mode 100644 public/userarea/pdf_mapping.js create mode 100644 public/userarea/process_import_pdf.php create mode 100644 public/userarea/save_pdf_regions.php create mode 100644 public/userarea/test_pdf_extract.php create mode 100644 public/userarea/testpoppler.php create mode 100644 public/userarea/update_pdf_page_meta.php create mode 100644 public/userarea/upload_pdf_example.php diff --git a/.gitignore b/.gitignore index 2d042e3..fb2dc42 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,7 @@ public/userarea/schemi_base_response.json public/userarea/cache/ public/userarea/error_log.txt + +# File PDF di esempio e importati +/public/userarea/pdftemplates/*.pdf +/public/userarea/pdftemplates/imported/*.pdf \ No newline at end of file diff --git a/db/migrations/20260815054854_add_pdf_support_to_templates.php b/db/migrations/20260815054854_add_pdf_support_to_templates.php new file mode 100644 index 0000000..df06fab --- /dev/null +++ b/db/migrations/20260815054854_add_pdf_support_to_templates.php @@ -0,0 +1,48 @@ +table('excel_templates'); + + if (!$templates->hasColumn('sample_pdf')) { + $templates->addColumn('sample_pdf', 'string', [ + 'limit' => 255, + 'null' => true, + 'default' => null, + 'after' => 'sample_xlsx', + 'comment' => 'Filename of the sample PDF used to draw regions', + ]); + } + + if (!$templates->hasColumn('pdf_page_meta')) { + $templates->addColumn('pdf_page_meta', 'text', [ + 'null' => true, + 'default' => null, + 'comment' => 'JSON: pages count and rendered dimensions per page', + ]); + } + + $templates->update(); + + // 2) Riquadri (etichetta + dato) per ogni campo mappato + $mapping = $this->table('template_mapping'); + + if (!$mapping->hasColumn('pdf_regions')) { + $mapping->addColumn('pdf_regions', 'text', [ + 'null' => true, + 'default' => null, + 'after' => 'json_node', + 'comment' => 'JSON: {page, label:{x,y,w,h}, value:{x,y,w,h}}', + ]); + } + + $mapping->update(); + } +} diff --git a/db/migrations/20260815055418_add_pdf_to_source_type_enum.php b/db/migrations/20260815055418_add_pdf_to_source_type_enum.php new file mode 100644 index 0000000..80ef767 --- /dev/null +++ b/db/migrations/20260815055418_add_pdf_to_source_type_enum.php @@ -0,0 +1,24 @@ +execute( + "ALTER TABLE `excel_templates` + MODIFY `source_type` ENUM('XLS','API','PDF') NOT NULL DEFAULT 'XLS'" + ); + } + + public function down(): void + { + $this->execute( + "ALTER TABLE `excel_templates` + MODIFY `source_type` ENUM('XLS','API') NOT NULL DEFAULT 'XLS'" + ); + } +} diff --git a/public/userarea/import_dashboard.php b/public/userarea/import_dashboard.php index 057b2bd..31d245b 100644 --- a/public/userarea/import_dashboard.php +++ b/public/userarea/import_dashboard.php @@ -297,7 +297,9 @@ wrapper.className = "template-btn-wrapper"; const btn = document.createElement("a"); - btn.href = `import_xls2.php?id=${template.id}`; + btn.href = (sourceType === 'PDF') ? + `import_pdf.php?id=${template.id}` : + `import_xls2.php?id=${template.id}`; btn.className = `btn ${sizeClass}`; btn.style.backgroundColor = template.button_bg_color || '#0d6efd'; btn.style.color = template.button_text_color || '#ffffff'; diff --git a/public/userarea/import_pdf.php b/public/userarea/import_pdf.php new file mode 100644 index 0000000..60c317b --- /dev/null +++ b/public/userarea/import_pdf.php @@ -0,0 +1,321 @@ +getConnection(); + +$stmt = $pdo->prepare("SELECT id, name, source_type, idclient, sample_pdf FROM excel_templates WHERE id = ?"); +$stmt->execute([$id]); +$template = $stmt->fetch(PDO::FETCH_ASSOC); + +if (!$template) { + header("Location: template_dashboard.php?status=error&message=" . urlencode("Template not found")); + exit; +} + +$sourceType = strtoupper($template['source_type'] ?? ''); +if ($sourceType !== 'PDF') { + // Non è un template PDF: rimanda al flusso corretto + header("Location: import_xls2.php?id=" . $id); + exit; +} + +// Conta i mapping con riquadri, per avvisare se non è configurato +$stmt = $pdo->prepare("SELECT COUNT(*) FROM template_mapping WHERE template_id = ? AND pdf_regions IS NOT NULL AND pdf_regions <> ''"); +$stmt->execute([$id]); +$mappedRegions = (int)$stmt->fetchColumn(); +?> + + + + + + + + + <?= htmlspecialchars($template['name']) ?> - PDF Import + + + + +
+ + +
+
+ + +
+
+
PDF
+ Template ID: — Ogni PDF diventa una riga importata. +
+
+ + +
+ Questo template non ha ancora riquadri PDF configurati. Configura il mapping prima di importare. +
+ + +
+
📄⬇
+
Trascina qui i PDF oppure clicca per selezionarli
+
Puoi caricarne più di uno insieme
+ +
+ +
+ 0 file + +
+ +
+ + +
+
+
+
+
+ + +
+ + + + + + \ No newline at end of file diff --git a/public/userarea/include/navbar.php b/public/userarea/include/navbar.php index 101588c..9901744 100644 --- a/public/userarea/include/navbar.php +++ b/public/userarea/include/navbar.php @@ -39,7 +39,7 @@ diff --git a/public/userarea/insert_template_xls.php b/public/userarea/insert_template_xls.php index 9217b25..a5d40b9 100644 --- a/public/userarea/insert_template_xls.php +++ b/public/userarea/insert_template_xls.php @@ -291,6 +291,7 @@ $apiConfigurations = $stmt->fetchAll(PDO::FETCH_ASSOC); const isXls = selectedSource === 'XLS'; const isApiJson = selectedSource === 'API'; + const isPdf = selectedSource === 'PDF'; if (isXls) { headerRowWrapper.style.display = 'block'; @@ -327,6 +328,8 @@ $apiConfigurations = $stmt->fetchAll(PDO::FETCH_ASSOC); apiConfigSelect.required = true; apiConfigSelect.disabled = false; } else { + // PDF (o qualsiasi altro): niente config API, niente campi XLS. + // I riquadri PDF si tracciano nella pagina di configurazione. apiConfigWrapper.style.display = 'none'; apiConfigSelect.required = false; apiConfigSelect.disabled = true; @@ -516,6 +519,9 @@ $apiConfigurations = $stmt->fetchAll(PDO::FETCH_ASSOC); return; } + // PDF: nessuna validazione extra qui. Il PDF di esempio e i + // riquadri (etichetta + dato) si configurano nella pagina successiva. + const clientSelect = document.getElementById("clientSelect"); const clientId = clientSelect.value; const selectedClientOption = clientSelect.options[clientSelect.selectedIndex]; diff --git a/public/userarea/mapping_template_xls_scheme2.php b/public/userarea/mapping_template_xls_scheme2.php index 07a68ce..a21dbff 100644 --- a/public/userarea/mapping_template_xls_scheme2.php +++ b/public/userarea/mapping_template_xls_scheme2.php @@ -24,7 +24,9 @@ $stmt = $pdo->prepare(" schemajson, xls_headers, api_sample_json, - json_nodes + json_nodes, + sample_pdf, + pdf_page_meta FROM excel_templates WHERE id = ? "); @@ -61,6 +63,7 @@ $stmt = $pdo->prepare(" field_order, excel_column, json_node, + pdf_regions, is_manual, manual_default, auto_value, @@ -117,6 +120,14 @@ if (!is_array($jsonNodes)) { } $apiSampleJson = $template['api_sample_json'] ?? ''; + +// Dati PDF: nome file di esempio e metadati pagine +$samplePdf = $template['sample_pdf'] ?? ''; + +$pdfPageMeta = $template['pdf_page_meta'] ? json_decode($template['pdf_page_meta'], true) : []; +if (!is_array($pdfPageMeta)) { + $pdfPageMeta = []; +} ?> @@ -129,6 +140,7 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; Configure Template <?= htmlspecialchars($template['name'], ENT_QUOTES, 'UTF-8'); ?> +