++ fix: add pdf download visibility setting

This commit is contained in:
2026-04-16 10:37:05 +00:00
parent c85f2aaea0
commit 1534c84d45
6 changed files with 273 additions and 64 deletions

View File

@@ -27,6 +27,7 @@ class AssignmentPdfController extends Controller
return view('assignments.pdf-viewer', [
'assignment' => $assignment,
'pdfUrl' => $pdfUrl,
'showDownload' => (bool) \App\Models\Setting::getValue('pdf_viewer_show_download', true),
]);
}

View File

@@ -13,6 +13,7 @@ class SettingsEdit extends Component
public int $giorni_per_smarrito = 120;
public int $home_limit_list = 10;
public int $assignment_link_ttl_months = 1;
public bool $pdf_viewer_show_download = true;
public int $audit_retention_days = 365;
public function mount()
@@ -24,6 +25,7 @@ class SettingsEdit extends Component
$this->giorni_per_smarrito = $settings->giorni_per_smarrito ?? 120;
$this->home_limit_list = $settings->home_limit_list ?? 10;
$this->assignment_link_ttl_months = $settings->assignment_link_ttl_hours ?? 1;
$this->pdf_viewer_show_download = $settings->pdf_viewer_show_download ?? true;
$this->audit_retention_days = $settings->audit_retention_days ?? 365;
}
@@ -36,6 +38,7 @@ class SettingsEdit extends Component
'giorni_per_smarrito' => 'required|integer|min:30|max:365',
'home_limit_list' => 'required|integer|min:1|max:100',
'assignment_link_ttl_months' => 'required|integer|min:1|max:24',
'pdf_viewer_show_download' => 'required|boolean',
'audit_retention_days' => 'required|integer|min:30|max:3650',
];
}
@@ -52,6 +55,7 @@ class SettingsEdit extends Component
'giorni_per_smarrito' => $this->giorni_per_smarrito,
'home_limit_list' => $this->home_limit_list,
'assignment_link_ttl_hours' => $this->assignment_link_ttl_months,
'pdf_viewer_show_download' => $this->pdf_viewer_show_download,
'audit_retention_days' => $this->audit_retention_days,
]);

View File

@@ -17,6 +17,7 @@ class Setting extends Model
'giorni_per_smarrito',
'home_limit_list',
'assignment_link_ttl_hours',
'pdf_viewer_show_download',
'audit_retention_days',
'setup_completed',
];
@@ -25,6 +26,7 @@ class Setting extends Model
{
return [
'setup_completed' => 'boolean',
'pdf_viewer_show_download' => 'boolean',
'giorni_giacenza_da_assegnare' => 'integer',
'giorni_giacenza_prioritari' => 'integer',
'giorni_per_smarrito' => 'integer',

View File

@@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->boolean('pdf_viewer_show_download')->default(true)->after('assignment_link_ttl_hours');
});
}
public function down(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('pdf_viewer_show_download');
});
}
};

View File

@@ -2,27 +2,33 @@
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
<title>Territorio {{ $assignment->territorio?->numero }}</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.9.155/pdf_viewer.min.css" integrity="sha512-kMgaLfnBSAM0MFgr8fMDCMr2SYGQiMIFRbkBxRfFEqDqw/0hNh2GpcjYKjR0z4VoVVhYx1VlJdvfO1HCkhpg==" crossorigin="anonymous" referrerpolicy="no-referrer">
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
height: 100%;
}
body {
font-family: system-ui, -apple-system, sans-serif;
background: #f1f5f9;
min-height: 100dvh;
display: flex;
flex-direction: column;
overflow: hidden;
}
header {
background: #fff;
border-bottom: 1px solid #e2e8f0;
padding: 14px 20px;
padding: 10px 16px;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px 24px;
gap: 8px 16px;
z-index: 10;
}
.logo {
@@ -36,24 +42,24 @@
.info {
display: flex;
flex-wrap: wrap;
gap: 6px 20px;
gap: 4px 12px;
flex: 1;
}
.chip {
display: inline-flex;
align-items: center;
gap: 5px;
gap: 4px;
background: #f1f5f9;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 4px 10px;
font-size: 13px;
border-radius: 6px;
padding: 3px 8px;
font-size: 12px;
color: #374151;
}
.chip .label {
font-size: 11px;
font-size: 10px;
color: #94a3b8;
font-weight: 500;
text-transform: uppercase;
@@ -68,58 +74,127 @@
.open-btn {
display: inline-flex;
align-items: center;
gap: 6px;
gap: 5px;
background: #4f46e5;
color: #fff;
text-decoration: none;
font-size: 13px;
font-size: 12px;
font-weight: 600;
padding: 7px 14px;
border-radius: 8px;
padding: 6px 12px;
border-radius: 6px;
flex: none;
transition: background .15s;
}
.open-btn:hover { background: #4338ca; }
.pdf-area {
flex: 1;
/* Toolbar */
.pdf-toolbar {
background: #1e293b;
color: #fff;
padding: 6px 12px;
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
font-size: 13px;
flex: none;
z-index: 10;
}
.pdf-toolbar button {
background: rgba(255,255,255,.1);
border: none;
color: #fff;
width: 32px;
height: 32px;
border-radius: 6px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
transition: background .15s;
font-size: 16px;
}
.pdf-toolbar button:hover { background: rgba(255,255,255,.2); }
.pdf-toolbar button:disabled { opacity: .3; cursor: default; }
.page-info {
font-variant-numeric: tabular-nums;
min-width: 80px;
text-align: center;
}
/* PDF viewport */
.pdf-viewport {
flex: 1 1 0;
min-height: 0;
overflow-y: auto;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
background: #64748b;
display: flex;
flex-direction: column;
padding: 16px;
align-items: center;
padding: 12px 0;
gap: 12px;
}
.pdf-embed {
flex: 1;
min-height: 60vh;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 24px rgba(0,0,0,.08);
.pdf-viewport canvas {
display: block;
max-width: 100%;
height: auto;
box-shadow: 0 2px 16px rgba(0,0,0,.25);
background: #fff;
}
.pdf-embed embed,
.pdf-embed iframe {
width: 100%;
height: 100%;
min-height: 60vh;
border: 0;
display: block;
/* Loading spinner */
.pdf-loading {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
padding: 40px;
color: #fff;
font-size: 14px;
}
.fallback-notice {
background: #fef9c3;
border: 1px solid #fde047;
border-radius: 10px;
padding: 12px 16px;
font-size: 13px;
color: #854d0e;
.spinner {
width: 36px;
height: 36px;
border: 3px solid rgba(255,255,255,.2);
border-top-color: #fff;
border-radius: 50%;
animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.pdf-error {
display: none;
flex-direction: column;
align-items: center;
gap: 12px;
padding: 40px;
text-align: center;
color: #fff;
}
.pdf-error a {
background: #4f46e5;
color: #fff;
text-decoration: none;
font-weight: 600;
font-size: 14px;
padding: 10px 20px;
border-radius: 8px;
}
@media (max-width: 480px) {
header { padding: 10px 14px; }
.chip { font-size: 12px; padding: 3px 8px; }
header { padding: 8px 10px; }
.chip { font-size: 11px; padding: 2px 6px; }
.info { gap: 3px 8px; }
.pdf-toolbar { padding: 4px 8px; gap: 8px; }
.pdf-toolbar button { width: 28px; height: 28px; }
}
</style>
</head>
@@ -142,39 +217,136 @@
</div>
</div>
@if($showDownload)
<a href="{{ $pdfUrl }}" download class="open-btn">
<svg width="14" height="14" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M4 16v2a2 2 0 002 2h12a2 2 0 002-2v-2M7 10l5 5m0 0l5-5m-5 5V4"/></svg>
<svg width="13" height="13" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M4 16v2a2 2 0 002 2h12a2 2 0 002-2v-2M7 10l5 5m0 0l5-5m-5 5V4"/></svg>
Scarica PDF
</a>
@endif
</header>
<div class="pdf-area">
<div class="pdf-embed" id="pdfContainer">
<embed src="{{ $pdfUrl }}" type="application/pdf" id="pdfEmbed">
<div class="pdf-toolbar">
<button id="prevPage" title="Pagina precedente" disabled></button>
<span class="page-info" id="pageInfo"></span>
<button id="nextPage" title="Pagina successiva" disabled></button>
<button id="zoomOut" title="Riduci"></button>
<button id="zoomIn" title="Ingrandisci">+</button>
</div>
<div class="fallback-notice" id="fallbackNotice">
Il tuo dispositivo non supporta la visualizzazione del PDF nel browser.
<a href="{{ $pdfUrl }}" download style="color:#4f46e5;font-weight:600;margin-left:6px;">Scarica il file PDF</a>
<div class="pdf-viewport" id="pdfViewport">
<div class="pdf-loading" id="pdfLoading">
<div class="spinner"></div>
Caricamento PDF…
</div>
<div class="pdf-error" id="pdfError">
<svg width="48" height="48" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"/></svg>
<p>Impossibile caricare il PDF.</p>
@if($showDownload)
<a href="{{ $pdfUrl }}" download>Scarica PDF</a>
@endif
</div>
</div>
<script>
// Detect if embed loaded - fallback for mobile devices that don't support PDF embed
var embed = document.getElementById('pdfEmbed');
var notice = document.getElementById('fallbackNotice');
embed.onerror = function() {
notice.style.display = 'block';
};
// iOS Safari: embed always renders but PDF viewer doesn't work - show download link anyway
var ua = navigator.userAgent;
if (/iP(hone|ad|od)/i.test(ua) || (/Macintosh/i.test(ua) && 'ontouchend' in document)) {
document.getElementById('pdfContainer').innerHTML =
'<div style="display:flex;align-items:center;justify-content:center;height:100%;min-height:60vh;flex-direction:column;gap:16px;padding:24px;text-align:center;">' +
'<svg width="48" height="48" fill="none" viewBox="0 0 24 24" stroke="#94a3b8" stroke-width="1.5"><path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z"/></svg>' +
'<p style="font-size:14px;color:#64748b;max-width:240px">Safari su iOS non supporta la visualizzazione PDF nel browser.</p>' +
'<a href="{{ $pdfUrl }}" download style="background:#4f46e5;color:#fff;text-decoration:none;font-weight:600;font-size:14px;padding:10px 20px;border-radius:8px;">Scarica PDF</a>' +
'</div>';
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.9.155/pdf.min.mjs" type="module"></script>
<script type="module">
import * as pdfjsLib from 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.9.155/pdf.min.mjs';
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.9.155/pdf.worker.min.mjs';
const url = @json($pdfUrl);
const viewport = document.getElementById('pdfViewport');
const loading = document.getElementById('pdfLoading');
const error = document.getElementById('pdfError');
const pageInfo = document.getElementById('pageInfo');
const prevBtn = document.getElementById('prevPage');
const nextBtn = document.getElementById('nextPage');
const zoomIn = document.getElementById('zoomIn');
const zoomOut = document.getElementById('zoomOut');
let pdfDoc = null;
let scale = 1.5;
let rendering = false;
const canvases = [];
// Determine initial scale based on screen width
if (window.innerWidth <= 480) {
scale = 1.0;
} else if (window.innerWidth <= 768) {
scale = 1.2;
}
async function renderPage(pageNum) {
const page = await pdfDoc.getPage(pageNum);
const vp = page.getViewport({ scale });
// Reuse or create canvas
let canvas = canvases[pageNum - 1];
if (!canvas) {
canvas = document.createElement('canvas');
canvases[pageNum - 1] = canvas;
}
const ctx = canvas.getContext('2d');
const dpr = window.devicePixelRatio || 1;
canvas.width = Math.floor(vp.width * dpr);
canvas.height = Math.floor(vp.height * dpr);
canvas.style.width = Math.floor(vp.width) + 'px';
canvas.style.height = Math.floor(vp.height) + 'px';
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
await page.render({ canvasContext: ctx, viewport: vp }).promise;
return canvas;
}
async function renderAllPages() {
if (rendering) return;
rendering = true;
// Clear viewport
viewport.querySelectorAll('canvas').forEach(c => c.remove());
for (let i = 1; i <= pdfDoc.numPages; i++) {
const canvas = await renderPage(i);
viewport.appendChild(canvas);
}
pageInfo.textContent = pdfDoc.numPages + (pdfDoc.numPages === 1 ? ' pagina' : ' pagine');
rendering = false;
}
async function init() {
try {
pdfDoc = await pdfjsLib.getDocument({ url, withCredentials: false }).promise;
loading.style.display = 'none';
prevBtn.disabled = true;
nextBtn.disabled = true;
// Render all pages as continuous scroll
await renderAllPages();
zoomIn.disabled = false;
zoomOut.disabled = false;
} catch (err) {
console.error('PDF load error:', err);
loading.style.display = 'none';
error.style.display = 'flex';
}
}
zoomIn.addEventListener('click', () => {
scale = Math.min(scale + 0.25, 4);
canvases.length = 0;
renderAllPages();
});
zoomOut.addEventListener('click', () => {
scale = Math.max(scale - 0.25, 0.5);
canvases.length = 0;
renderAllPages();
});
init();
</script>
</body>
</html>

View File

@@ -63,6 +63,14 @@
@error('assignment_link_ttl_months') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
</div>
<div>
<label class="flex items-center gap-2 text-sm font-medium text-gray-700 cursor-pointer">
<input wire:model="pdf_viewer_show_download" type="checkbox" value="1" class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
Mostra pulsante "Scarica PDF" nel viewer link temporaneo
</label>
<p class="text-xs text-gray-500 mt-1">Se disattivato, il proclamatore potrà solo visualizzare il PDF senza scaricarlo.</p>
</div>
<div>
<label for="audit_retention_days" class="block text-sm font-medium text-gray-700">Conservazione Audit (giorni)</label>
<p class="text-xs text-gray-500 mb-1">I log più vecchi di questo periodo verranno cancellati automaticamente.</p>