Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda