This commit is contained in:
Bastian Wagner
2026-03-12 14:52:35 +01:00
parent faa5003df0
commit a36b76eda4
3 changed files with 256 additions and 1 deletions

View File

@@ -0,0 +1,17 @@
export class KeyHandoverDto {
handoverId!: string;
handoverDate!: Date;
place!: string;
giverName!: string;
giverAddress?: string;
receiverName!: string;
receiverAddress?: string;
keyType!: string;
keyNumber?: string;
quantity!: number;
objectDescription?: string;
notes?: string;
}

View File

@@ -1,6 +1,7 @@
import { Controller, Get, Header, Res } from "@nestjs/common"; import { Body, Controller, Get, Header, Res } from "@nestjs/common";
import { PdfService } from "./pdf.service"; import { PdfService } from "./pdf.service";
import { Response } from 'express'; import { Response } from 'express';
import { KeyHandoverDto } from "src/model/key-handover.dto";
@Controller('pdf') @Controller('pdf')
export class PdfController { export class PdfController {
@@ -14,4 +15,14 @@ export class PdfController {
res.setHeader('Content-Disposition', 'inline; filename="test.pdf"'); res.setHeader('Content-Disposition', 'inline; filename="test.pdf"');
res.send(pdfBuffer); res.send(pdfBuffer);
} }
@Get('keyhandover')
@Header('Content-Type', 'application/pdf')
async generatePdf(@Res() res: Response, @Body() dto: KeyHandoverDto): Promise<void> {
const pdfBuffer = await this.pdfService.generatePdf(dto);
res.setHeader('Content-Disposition', 'inline; filename="test.pdf"');
res.send(pdfBuffer);
}
} }

View File

@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import puppeteer from 'puppeteer-core'; import puppeteer from 'puppeteer-core';
import { KeyHandoverDto } from 'src/model/key-handover.dto';
@Injectable() @Injectable()
export class PdfService { export class PdfService {
@@ -66,4 +67,230 @@ export class PdfService {
await browser.close(); await browser.close();
} }
} }
async generatePdf(dto: KeyHandoverDto): Promise<Buffer> {
const browser = await puppeteer.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || '/usr/bin/chromium',
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
try {
const page = await browser.newPage();
const html = this.buildHtml(dto);
await page.setContent(html, {
waitUntil: 'networkidle0',
});
const pdf = await page.pdf({
format: 'A4',
printBackground: true,
margin: {
top: '20mm',
right: '15mm',
bottom: '20mm',
left: '15mm',
},
displayHeaderFooter: true,
headerTemplate: `<div></div>`,
footerTemplate: `
<div style="
width: 100%;
font-size: 10px;
padding: 0 20px;
color: #666;
display: flex;
justify-content: space-between;
">
<span>Übergabeprotokoll</span>
<span>${new Date().toLocaleString()}</span>
</div>
`,
});
const buffer = Buffer.from(pdf);
return buffer;
} finally {
await browser.close();
}
}
private buildHtml(dto: KeyHandoverDto): string {
return `
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<title>Übergabeprotokoll</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #222;
margin: 0;
padding: 0;
}
h1 {
text-align: center;
margin: 0 0 24px 0;
font-size: 22px;
}
.section {
margin-bottom: 18px;
}
.section-title {
font-weight: bold;
font-size: 14px;
margin-bottom: 8px;
border-bottom: 1px solid #ccc;
padding-bottom: 4px;
}
.row {
margin-bottom: 6px;
}
.label {
display: inline-block;
width: 170px;
font-weight: bold;
vertical-align: top;
}
.value {
display: inline-block;
max-width: 360px;
word-break: break-word;
}
.note-box {
min-height: 60px;
border: 1px solid #ccc;
padding: 8px;
margin-top: 6px;
white-space: pre-wrap;
}
.declaration {
margin-top: 24px;
line-height: 1.5;
}
.signature-wrapper {
margin-top: 60px;
display: flex;
justify-content: space-between;
gap: 40px;
}
.signature-block {
width: 45%;
}
.signature-line {
border-top: 1px solid #000;
margin-top: 50px;
padding-top: 6px;
text-align: center;
}
</style>
</head>
<body>
<h1>Übergabeprotokoll</h1>
<div class="section">
<div class="section-title">Übergabedaten</div>
<div class="row">
<span class="label">Datum der Übergabe:</span>
<span class="value">${this.escapeHtml(new Date(dto.handoverDate).toLocaleDateString())}</span>
</div>
<div class="row" style="display: none">
<span class="label">Ort:</span>
<span class="value">${this.escapeHtml(dto.place)}</span>
</div>
</div>
<div class="section">
<div class="section-title">Übergeber</div>
<div class="row">
<span class="label">Name:</span>
<span class="value">${this.escapeHtml(dto.giverName)}</span>
</div>
<div class="row">
<span class="label">Adresse:</span>
<span class="value">${this.escapeHtml(dto.giverAddress ?? '-')}</span>
</div>
</div>
<div class="section">
<div class="section-title">Empfänger</div>
<div class="row">
<span class="label">Name:</span>
<span class="value">${this.escapeHtml(dto.receiverName)}</span>
</div>
<div class="row">
<span class="label">Adresse:</span>
<span class="value">${this.escapeHtml(dto.receiverAddress ?? '-')}</span>
</div>
</div>
<div class="section">
<div class="section-title">Schlüsselangaben</div>
<div class="row" style="display: none;">
<span class="label">Schlüsselart:</span>
<span class="value">${this.escapeHtml(dto.keyType)}</span>
</div>
<div class="row">
<span class="label">Schlüsselnummer:</span>
<span class="value">${this.escapeHtml(dto.keyNumber ?? '-')}</span>
</div>
<div class="row" style="display: none;">
<span class="label">Anzahl:</span>
<span class="value">${dto.quantity}</span>
</div>
<div class="row">
<span class="label">Objekt / Raum:</span>
<span class="value">${this.escapeHtml(dto.objectDescription ?? '-')}</span>
</div>
<div class="row">
<span class="label">Bemerkungen:</span>
</div>
<div class="note-box">${this.escapeHtml(dto.notes ?? '-')}</div>
</div>
<div class="declaration">
Hiermit bestätigt der Empfänger den Erhalt der oben aufgeführten Schlüssel.
<br />
Mit ihrer Unterschrift bestätigen beide Parteien die ordnungsgemäße Übergabe.
</div>
<div class="signature-wrapper">
<div class="signature-block">
<div class="signature-line">Unterschrift Übergeber</div>
</div>
<div class="signature-block">
<div class="signature-line">Unterschrift Empfänger</div>
</div>
</div>
</body>
</html>
`;
}
private escapeHtml(value: string): string {
if (!value) { return ""; }
return value
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}
} }