0

I am trying to download a PDF using the node-html-pdf package and Loopback 4.

This is what I am trying:

@get('/reports/{id}/preview', {
    responses: {
      '200': {
        description: 'PDF',
        content: {
          'application/pdf': {
            schema: { type: 'string' },
          },
        },
      },
    },
  })
  async getPreview(
    @inject(RestBindings.Http.RESPONSE) response: Response,
    @param.path.string('id') id: string,
  ): Promise<void> {
    const reportTemplate = /* This is an html string template */
    pdf.create(reportTemplate).toStream((error, stream) => {
      if (error)
        throw error;

      stream.pipe(response);
    });
  }

But I am getting this error:

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at write_ (_http_outgoing.js:594:17)
    at ServerResponse.write (_http_outgoing.js:586:15)
    at ReadStream.ondata (_stream_readable.js:728:22)
    at ReadStream.emit (events.js:223:5)
    at ReadStream.EventEmitter.emit (domain.js:475:20)

What am I doing wrong?

Rifa Achrinza
  • 1,055
  • 7
  • 15

0 Answers0