0

I am working on a nodejs service where I need to read a PDF document from a file.

At a high level, here is the workflow. First time a user requests a PDF, I generate it (using pdfkitjs) and save it to the server. Then, when the user request the same document again, I need to read it from the server and send it back.

Is there a way that I can use PDFDocument from pdfkitjs to read the contents from the file and create a corresponding PDFDocument? Almost all searches come up with how to pipe the PDF to a stream, but now how to read from a stream.

Joe.b
  • 422
  • 1
  • 6
  • 16
  • Note the `pipe()` method isn't the only way, it is possible to `read()` directly from the PDFDocument object. Not the answer to your question but might help your workflow. https://stackoverflow.com/a/73215035/10365982 – Matteljay Aug 18 '22 at 12:01

1 Answers1

0

If "the stream" is your server response then you shouldn't have to do much more than add the appropriate headers to the response first.

lecstor
  • 5,619
  • 21
  • 27
  • I have a service that writes (returns a PDFDocument) and reads the files. The controller uses the PDFDocument returned to pipe it to the res pdf.pipe(res) – Joe.b Mar 17 '17 at 03:05
  • once you have an existing PDF file you shouldn't need to involve PDFDocument, you can just serve it up as any other regular file. – lecstor Mar 17 '17 at 03:21
  • 1
    Thanks. Your comment pointed me in the right direction. So according to pdfkit documentation, a PDFDocument instance is readable Node stream. So then the service client gets back a node stream and should not care how it was obtained. To this end, I used fs.createReadStream to get a stream for the file (in case the file exists). – Joe.b Mar 18 '17 at 02:02