#Nestjs error write after end

1 messages · Page 1 of 1 (latest)

south widget
#

Hello everyone, I am in my last stage of this project. I am trying to preview the data in angular goes wrong. Does not get send the correct data.
I had succes generating a pdf and saving it but not any succes with trying to preview. First of all i need to press like twice on the button loadpdf before the iframe shows up and then it does not load the data i generate
i feel like i am doing this correct.

 async previewPDF(data: any, res: Response): Promise<void> {
    const doc = new PDFKit();
    const endX = doc.page.width - 20;
    const codelogoPath = path.join(
      __dirname,
      '..',
      '..',
      '..',
      'apps',
      'codex-api',
      'src',
      'assets',
      'logos',
      'mylogo.png'
    );
    doc.image(codelogoPath, 425, 10, {
      fit: [170, 170],
      align: 'center',
    });    
    doc.font('Helvetica');
    doc.text(data.addressclient, 430, 111);
    doc.text(data.postalclient + ' ' + data.city, 430, 125);
    doc.text(data.land, 430, 139);
    doc.font('Helvetica-Bold');
    doc.text('Codex:', 20, 690);
    doc.text('Ondernemingsnummer: BE 0000.111.222', 20, 700);
    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader('Content-Disposition', 'inline; filename=preview.pdf');
    doc.pipe(res);
    doc.end();
  }
}
 @Post('/preview')
  async previewPDF(@Body() data: any, @Res() res: Response) {
    const fileBuffer = await this.pdfService.previewPDF(data, res);

    res.setHeader('Content-Type', 'application/pdf');
    res.send(fileBuffer);
  }```
#

Nestjs error write after end

vast ember
#

I guess you need to remove that doc.end() because you are using doc.pipe(res)

south widget
#

okey i go try that

#

i dont know why but it reset my connection when i press preview, then it still not loading

south widget
#

Well i did some logging and i think this blob is just empty

  loadPdf(): void {
    this.data = this.generateData();
//empty blob
    this.fileservice.previewPDF(this.data).subscribe((blob: Blob) => {
      const pdfBlob = new Blob([blob], { type: 'application/pdf' });
      console.log(pdfBlob);
      const url = URL.createObjectURL(pdfBlob);
      this.pdfSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url);
    }),
      (error: any) => {
        console.error('Error occurred while generating PDF:', error);
      };
  }```
and so it leads back to the screen here implemented
south widget
#

Did not find any way to fix it, if u throw any solution i will anwser tomorrow. This is not a message to spamm u, just want u to know I am back tomorrow

still comet
#

You shouldn't be using doc.pipe(res) and res.send. pipe will send for you

south widget
#

hey, so i should remove doc.pipe and res.send?
or u mean res.send is the only one i need?

still comet
#

I mean if you use doc.pipe(res) you don't need res.send

south widget
#

just tested whitout res.send i get nothing at all not even an error of the page not loading

still comet
#

Do you still have doc.end or no?

south widget
#

no

still comet
#

Add it back, otherwise the doc.pipe() never ends and the response never finishes

south widget
#

well you got it working,
Do you also know why i have to press the button twice? ```ts
'<iframe
*ngIf="pdfSrc"
[src]="pdfSrc"
width="100%"
height="600"
frameborder="0"
></iframe
>'
<codex-ui-button (click)="loadPdf()" class="mybutton" type="button"
>PDF voorbeeld</codex-ui-button
>
loadPdf(): void {
this.data = this.generateData();
this.fileservice.previewPDF(this.data).subscribe((blob: Blob) => {
const pdfBlob = new Blob([blob], { type: 'application/pdf' });
console.log(pdfBlob);
const url = URL.createObjectURL(pdfBlob);
this.pdfSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}),
(error: any) => {
console.error('Error occurred while generating PDF:', error);
};
}

bypassAndSanitize(url: any): SafeResourceUrl {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}

still comet
#

Nope. Seems like a bug of the UI

south widget
#

oh okey t hanks