forked from fahrPool/fahrPool
feat: PDF-Uebergabeprotokoll als Download bereitstellen
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dimk8Jo64b4wULp2yAzSNh
This commit is contained in:
parent
93dd6fc46b
commit
f90fe1f347
|
|
@ -159,6 +159,14 @@ export default async function AuftragDetailPage({
|
|||
</Card>
|
||||
)}
|
||||
|
||||
{auftrag.status === "abgeschlossen" && (
|
||||
<div className="mb-6">
|
||||
<a href={`/auftraege/${auftrag.id}/protokoll.pdf`} className={buttonClasses("secondary", false)}>
|
||||
Übergabeprotokoll (PDF)
|
||||
</a>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{auftrag.status === "offen" && (
|
||||
<Card className="mb-6">
|
||||
<form action={uebernehmenAction}>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { renderToBuffer, type DocumentProps } from "@react-pdf/renderer";
|
||||
import { createElement, type ReactElement } from "react";
|
||||
import { loadAuftrag } from "@/lib/auftraege/queries";
|
||||
import { loadAuftragFotoBuffers, loadUnterschriftBuffer } from "@/lib/auftraege/fotos";
|
||||
import { buildProtokollBasisdaten } from "@/lib/auftraege/protokoll";
|
||||
import { ProtokollDocument } from "@/lib/auftraege/ProtokollDocument";
|
||||
|
||||
export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params;
|
||||
const auftrag = await loadAuftrag(id);
|
||||
|
||||
if (!auftrag || auftrag.status !== "abgeschlossen") {
|
||||
return new NextResponse("Not found", { status: 404 });
|
||||
}
|
||||
|
||||
const [fotos, unterschriftBuffer] = await Promise.all([
|
||||
loadAuftragFotoBuffers(id),
|
||||
loadUnterschriftBuffer(auftrag.unterschrift_storage_path),
|
||||
]);
|
||||
|
||||
const props = { ...buildProtokollBasisdaten(auftrag), fotos, unterschriftBuffer };
|
||||
// @react-pdf/renderer v4 typt ProtokollDocument nicht als ReactElement<DocumentProps>,
|
||||
// obwohl es einen <Document>-Baum liefert -- bekannte Typluecke der Lib.
|
||||
const element = createElement(ProtokollDocument, { props }) as unknown as ReactElement<DocumentProps>;
|
||||
const buffer = await renderToBuffer(element);
|
||||
|
||||
return new NextResponse(new Uint8Array(buffer), {
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="protokoll-${auftrag.auftragsnummer}.pdf"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -98,3 +98,50 @@ export async function loadUnterschriftUrl(storagePath: string | null): Promise<s
|
|||
}
|
||||
return toPublicUrl(signiert.signedUrl);
|
||||
}
|
||||
|
||||
// Laedt die Rohdaten (nicht signierte URLs) aller Fotos eines Auftrags fuer
|
||||
// den PDF-Export -- @react-pdf/renderer bettet Bilder als Binaerdaten ein,
|
||||
// nicht als externe Links (siehe ProtokollDocument.tsx).
|
||||
export async function loadAuftragFotoBuffers(
|
||||
auftragId: string
|
||||
): Promise<{ label: string; beschreibung: string | null; buffer: Buffer }[]> {
|
||||
const supabase = await createClient();
|
||||
const { data } = await supabase
|
||||
.from("auftrag_fotos")
|
||||
.select("position, storage_path, uploaded_at, beschreibung")
|
||||
.eq("auftrag_id", auftragId);
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const sortiert = sortiereUndBeschrifteFotos(data);
|
||||
|
||||
const fotos = await Promise.all(
|
||||
sortiert.map(async (foto): Promise<{ label: string; beschreibung: string | null; buffer: Buffer } | null> => {
|
||||
const { data: blob } = await supabase.storage.from("auftrag-fotos").download(foto.storage_path);
|
||||
if (!blob) {
|
||||
return null;
|
||||
}
|
||||
const buffer = Buffer.from(await blob.arrayBuffer());
|
||||
return { label: foto.label, beschreibung: foto.beschreibung, buffer };
|
||||
})
|
||||
);
|
||||
|
||||
return fotos.filter((foto): foto is { label: string; beschreibung: string | null; buffer: Buffer } => foto !== null);
|
||||
}
|
||||
|
||||
// Analoger Rohdaten-Loader fuer die Unterschrift (siehe loadUnterschriftUrl
|
||||
// oben fuer die signierte URL-Variante, die fuer die Web-Anzeige genutzt
|
||||
// wird -- der PDF-Export braucht stattdessen die Bytes direkt).
|
||||
export async function loadUnterschriftBuffer(storagePath: string | null): Promise<Buffer | null> {
|
||||
if (!storagePath) {
|
||||
return null;
|
||||
}
|
||||
const supabase = await createClient();
|
||||
const { data: blob } = await supabase.storage.from("auftrag-unterschriften").download(storagePath);
|
||||
if (!blob) {
|
||||
return null;
|
||||
}
|
||||
return Buffer.from(await blob.arrayBuffer());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue