Files
keyvault/api/src/shared/pdf/key-handover-pdf/key-handover-pdf.service.ts
2026-03-12 12:00:48 +01:00

250 lines
7.5 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { KeyHandoverDto } from 'src/model/dto/key-handover.dto';
import * as puppeteer from 'puppeteer';
import { MinioService } from '../minio/minio.service';
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
import { KeyHandout } from 'src/model/entitites';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class KeyHandoverPdfService {
constructor(private readonly minioService: MinioService, private handoverRepo: KeyHandoutRepository, private configService: ConfigService) {}
async generatePdf(dto: KeyHandoverDto): Promise<Buffer> {
const browser = await puppeteer.launch({
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);
const bucket = this.configService.get('MINIOBUCKET');
const key = `handover-forms/${Date.now()}-schluesseluebergabe.pdf`;
await this.minioService.uploadPdf(bucket, key, buffer);
await this.saveKeyToHandover(dto.handoverId, key)
return buffer;
} finally {
await browser.close();
}
}
private async saveKeyToHandover(handoverId: string, fileKey: string) {
const handover: KeyHandout = await this.handoverRepo.findOneBy({ id: handoverId });
if (!handover) { return; }
handover.pdfFormKey = fileKey;
this.handoverRepo.save(handover);
}
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;');
}
}