4

I am trying to create a blob from a pdf I am creating from pdfmake so that I can send it to a remote api that only handles blobs.

This is how I get my PDF file:

var docDefinition = { content: 'This is an sample PDF printed with pdfMake' };
pdfDoc.pipe(fs.createWriteStream('./pdfs/test.pdf'));   
pdfDoc.end();

The above lines of code do produce a readable pdf.

Now how can I get a blob from there? I have tried many options (creating the blob from the stream with the blob-stream module, creating from the file with fs, creating it from a base64 string with b64toBlob) but all of them require at some point to use the constructor Blob for which I always get an error even if I require the module blob:

TypeError: Blob is not a constructor

After some research I found that it seems that the Blob constructor is only supported client-side.

All the npm packages that I have found and which seem to deal with this issue seem to only work client-side: blob-stream, blob, blob-util, b64toBlob, etc.

So, how can I create a blob server-side on Node?

I don't understand why almost nobody also needs to create a blob server-side? The only thread I could find on the subject is this one.

According to that thread, apparently:

The Solution to this problem is to create a function which can convert between Array Buffers and Node Buffers. :)

Unfortunately this does not help me much as I clearly seem to lack some important knowledge here to be able to comprehend this.

halfer
  • 19,824
  • 17
  • 99
  • 186
jsplugged
  • 41
  • 1
  • 3
  • 1
    In general: Blob is an abstract concept describing a binary object, a local wrapper if you will. You can't really send a "blob" as it's basically just a binary/byte stream when hitting the net (which is why you don't really need Blob on the server). Just send the data as a binary stream from server - the client should be able to receive (via XMLHttpRequest or fetch) either as ArrayBuffer or convert the latter to Blob there. –  Dec 27 '17 at 12:50
  • not a constructor but yet the source says it should work. https://www.npmjs.com/package/blob – conterio Oct 10 '19 at 23:33

1 Answers1

-1

use node-blob npm package

const Blob = require('node-blob');

let myBlob = new Blob(["something"], { type: 'text/plain' });
Dev Matee
  • 5,525
  • 2
  • 27
  • 33