PDF Download

This commit is contained in:
Bastian Wagner
2026-03-13 21:19:52 +01:00
parent 7973d4563e
commit 8a9295c309
17 changed files with 182 additions and 423 deletions

View File

@@ -0,0 +1,52 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AxiosResponse } from 'axios';
import { firstValueFrom } from 'rxjs';
import { KeyHandoverPDFDataDto } from 'src/model/dto/key-handover.dto';
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
@Injectable()
export class PdfService {
private readonly STORAGESERVER = this.configService.get('STORAGE_HOST')
constructor(
private readonly configService: ConfigService,
private readonly httpService: HttpService,
private readonly handoverRepo: KeyHandoutRepository
) { }
public async generatePDF(dto: KeyHandoverPDFDataDto): Promise<{ pdf: Buffer, response: AxiosResponse}> {
const h = await this.handoverRepo.findOneByOrFail({ id: dto.handoverId });
const response = await this.post(`${this.STORAGESERVER}/pdf/keyhandover`, dto);
console.log(response)
if (response?.data == null) { return; }
h.pdfFormKey = response.data;
await this.handoverRepo.save(h);
return this.getPDFByHandoverKey(response.data)
}
public async getPDFByHandoverKey(key: string): Promise<{ pdf: Buffer, response: AxiosResponse}> {
return new Promise(resolve => {
this.httpService.get(`${this.STORAGESERVER}/pdf/keyhandover/${key}`, {
responseType: 'arraybuffer',
}).subscribe({
next: pdf => {
const pdfBuffer = Buffer.from(pdf.data);
resolve({ pdf: pdfBuffer, response: pdf})
},
error: e => console.log(e)
})
})
}
private async post(url: string, data: any): Promise<AxiosResponse> {
return new Promise(resolve => {
this.httpService.post(url, data).subscribe({
next: data => resolve(data)
})
})
}
}