Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
Convert pdf stream into buffer

I've a stream of pdf file which I need to convert into a buffer and I am tying to read the stream using stream.on but its not working and I keep getting error

UnhandledPromiseRejectionWarning: TypeError: stream.on is not a function

My stream starts looks like this

%PDF-1.3
%����
12 0 obj
<<
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/Filter [/FlateDecode /DCTDecode]
/Height 117
/Length 2703
/Mask [ 253 255 253 255 253 255 ]
/Name /Obj0
/Subtype /Image
/Type /XObject
/Width 73
>>
stream

and it ends with

%%EOF

Here is my code

// api call
 instance.post(GENERATE_LABEL, strigifiedBody, { headers: {
      'Content-Type': 'application/json; charset=utf-8'
    }, responseType: 'stream' } ).then(async (res) => {
      streamToString(res.data).then(async (strData) => {
          const stream = extractStreamFromResponse(res, strData);
        buffer = await getBuffer(stream);
        console.log(buffer);       
      });
    }).catch(err => {
        console.log(err);
    });

//function to extract stream from multipart/mixed data
  function extractStreamFromResponse(res, data) {
    let header = res.headers['content-type'];
    let boundary = header.split(' ')[1];
    boundary = boundary.split('=')[1];
    boundary = boundary.split('"')[1];
    const rawStream = data.split('--' + boundary)[2];
    const stream = rawStream.substring(100);
    return stream;
  }

  // function to get buffer from stream
  function getBuffer(stream) {
  const chunks = [];
  return new Promise((resolve, reject) => {
      stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
      stream.on('error', (err) => reject(err));
      stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
    })
  }

How can I create a buffer of this stream?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!