toondaey / nestjs-pdf

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

Observables<Buffer> to Buffer #38

Closed pietrzakadrian closed 3 years ago

pietrzakadrian commented 3 years ago

I am using the latest version of your package. I'm trying to upload the generated buffer to my aws cloud. it looks like this:

...
    const file = this._pdfService.toBuffer('invoice', { locals: { invoice } });
    await this._fileService.uploadDocumentFile(file);
...
  public async uploadDocumentFile(buffer: any): Promise<string> {
    const date = format(new Date(), 'MM/yyyy');
    const key = `invoices/${date}`;

    await this._s3
      .putObject({
        Bucket: this.configService.get('AWS_PRIVATE_BUCKET_NAME_INVOICES'),
        Body: buffer,
        ACL: 'private',
        Key: key,
      })
      .promise();

    return key;
  }

Error: InvalidParameterType: Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object

How do I do that? I don't have much experience with RxJs

pietrzakadrian commented 3 years ago

Okay, I solved it and thanks to you I found out more about Observable:)

  public async generateInvoice(invoice: InvoiceDto): Promise<any> {
    return this._pdfService
      .toBuffer('invoice', { locals: { invoice } })
      .subscribe(
        (file) => {
          this._fileService.uploadDocumentFile(file, invoice);
        },
        (error) => {
          throw new GenerateInvoiceInvalidException(error);
        },
      );
  }
pietrzakadrian commented 3 years ago

However, this is not the best solution for me. How else can I get just Buffer?

toondaey commented 3 years ago

Hi @pietrzakadrian , you should probably try using this:

  public async generateInvoice(invoice: InvoiceDto): Promise<any> {
    const file = await this._pdfService
      .toBuffer('invoice', { locals: { invoice } })
      .toPromise();
    this._fileService.uploadDocumentFile(file, invoice);
  }

You can read more about it here.