toondaey / nestjs-pdf

Nest js pdf generator
MIT License
83 stars 37 forks source link

What would be the proper nestjs-pdf method to use in a response? #197

Open antonioOrtiz opened 1 year ago

antonioOrtiz commented 1 year ago

Thank you for this great library, I love how it totally integrates into the nestjs framework!

So in my case I'm using nestjs-pdf as a service for another app.

The idea is my main app will use this as service as a lamda on my server separately.

e.g. main-app: main app calls ejs-to-pdf to send data to create pdfs! ejs-to-pdf: ejs app provides pdfs when main-app calls or another authorized app!

And ultimate;y it goes to a S3 bucket.

nestjs-pdf app

app.controller.ts

@Controller('/ejs-to-pdf')
export class AppController {
    constructor(private readonly pdfService: PdfService) {}
    @Public()
    @Post('/quote-letter-to-pdf')
    async requestQuoteLetter(@Body() data: string): Promise<DSResponse<DSError<any>, any>> {
            console.log('data ', data);
        const fileName: string = 'Quote-Letter-' + new Date().toUTCString() + '.pdf';
        return new DSResponse(null, await this.pdfService.generatePDFToFile(JSON.stringify(data), fileName));
    }
}

I see the data coming through. and I consoled the response as well.

res  DSResponse {
  status: undefined,
  success: true,
  data: Observable {
    source: Observable {
      source: [Observable],
      operator: [Function (anonymous)]
    },
    operator: [Function (anonymous)]
  },
  error: null,
  token: undefined
}

In the main app I'm seeing the file generated in S3, but I get when I try to open it.

Error Failed to load PDF document.

In the main-app In the service that calls 'ejs-to-pdf` I see the resp in a console.log:

resp <Buffer 7b 22 73 75 63 63 65 73 73 22 3a 74 72 75 65 2c 22 64 61 74 61 22 3a 7b 22 73 6f 75 72 63 65 22 3a 7b 22 73 6f 75 72 63 65 22 3a 7b 22 73 6f 75 72 63 ... 33 more bytes>

And I see the file:


const resp: any = await this.ejsToPdf.makePDF(JSON.stringify(quoteLetterPayload), this.dsConfig.services.webmerge.testMode, true);

console.log('resp ', resp);

const filePost: FileStorageReqInfo = new FileStorageReqInfo();
filePost.bucket = this.dsConfig.aws.s3.bucketName; 
filePost.key = `quote/${quoteId}/${docNameWithExt}`;
filePost.buffer = resp;
filePost.contentEncoding = 'utf-8';
filePost.contentType = this.dsUtil.getContentTypeByFile(filePost.key);
const s3Resp: boolean = await this.dsFileService.uploadFile(filePost);

console.log('JSON.stringify(filePost) ', JSON.stringify(filePost));

Any ideas how to get the pdf to open/render correctly?

toondaey commented 1 year ago

Could this be an issue with encoding to your S3 bucket? How are you trying to view the file?