trf_certest/public/userarea/test_pdf_value.php

118 lines
4.2 KiB
PHP

<?php
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/class/VisualLimsApiClient.class.php';
ini_set('display_errors', '1');
error_reporting(E_ALL);
set_time_limit(60);
/**
* Uso: get_pdf.php?id=591749
*/
try {
$api = VisualLimsApiClient::getInstance();
$idRapportoFile = isset($_GET['id']) ? intval($_GET['id']) : 0;
if (!$idRapportoFile) {
throw new Exception("Parametro id mancante. Usa ?id=591749");
}
// STEP 1: leggi metadati entita
$entityData = $api->get("RapportoFile(" . $idRapportoFile . ")");
$pdfBody = null;
$strategyUsed = null;
$debugLog = array();
// Strategia A - campo FileContent base64
if (!empty($entityData['FileContent'])) {
$decoded = base64_decode($entityData['FileContent'], true);
if ($decoded !== false && substr($decoded, 0, 4) === '%PDF') {
$pdfBody = $decoded;
$strategyUsed = 'A: FileContent base64';
}
}
// Strategia B - campo Contenuto base64
if (!$pdfBody && !empty($entityData['Contenuto'])) {
$decoded = base64_decode($entityData['Contenuto'], true);
if ($decoded !== false && substr($decoded, 0, 4) === '%PDF') {
$pdfBody = $decoded;
$strategyUsed = 'B: Contenuto base64';
}
}
// Recupera token e baseUrl dalla classe
$token = $api->getToken();
$baseUrl = rtrim($api->getBaseUrl(), '/');
$rawEndpoints = array(
'C: $value' => $baseUrl . '/api/odata/RapportoFile(' . $idRapportoFile . ')/$value',
'D: FileContent/$value' => $baseUrl . '/api/odata/RapportoFile(' . $idRapportoFile . ')/FileContent/$value',
'E: Contenuto/$value' => $baseUrl . '/api/odata/RapportoFile(' . $idRapportoFile . ')/Contenuto/$value',
);
foreach ($rawEndpoints as $label => $url) {
if ($pdfBody) break;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token,
'Accept: application/pdf, application/octet-stream, */*',
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$body = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$ct = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
$debugLog[$label] = array(
'url' => $url,
'http_code' => $httpCode,
'content_type' => $ct,
'body_start' => substr((string)$body, 0, 300),
);
if ($httpCode === 200 && is_string($body) && substr($body, 0, 4) === '%PDF') {
$pdfBody = $body;
$strategyUsed = $label;
}
}
// RISPOSTA
if ($pdfBody) {
$fileName = isset($entityData['FileName']) ? $entityData['FileName'] : 'rapporto_' . $idRapportoFile . '.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $fileName . '"');
header('Content-Length: ' . strlen($pdfBody));
header('Cache-Control: private, max-age=0, must-revalidate');
echo $pdfBody;
exit;
}
// Nessuna strategia ha funzionato
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array(
'success' => false,
'message' => 'Nessuna strategia ha restituito un PDF valido.',
'id_rapporto_file' => $idRapportoFile,
'entity_fields' => array_keys($entityData ? $entityData : array()),
'entity_data' => $entityData,
'strategies_debug' => $debugLog,
), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
http_response_code(500);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array(
'success' => false,
'error' => $e->getMessage(),
), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}