devongovett / blob-stream

A Node-style writable stream for HTML5 Blobs
MIT License
118 stars 21 forks source link

TypeError: Blob is not a constructor #5

Open Alain00 opened 4 years ago

Alain00 commented 4 years ago

I'm getting the following error

\blob-stream\index.js:33
    this._blob = new Blob(this._chunks, {
                 ^

TypeError: Blob is not a constructor
    at BlobStream.toBlob (H:\Work\projects\node\subzero\node_modules\blob-stream\index.js:33:18)
    at BlobStream.toBlobURL (H:\Work\projects\node\subzero\node_modules\blob-stream\index.js:48:35)
    at BlobStream.stream.on (H:\Work\projects\node\subzero\routes\cpanel\orderadmin.router.js:185:32)
    at BlobStream.emit (events.js:203:15)
    at finishMaybe (_stream_writable.js:646:14)
    at afterWrite (_stream_writable.js:486:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

This is the code

const PDFDocument = require('pdfkit');
const blobStream  = require('blob-stream');
let doc = new PDFDocument();
const stream = doc.pipe(blobStream());
doc.text("HOLA MUNDO!!");
doc.end();
stream.on('finish', ()=>{
    const url = stream.toBlobURL('application/pdf');
    res.send(url);
})
dhatawesomedude commented 4 years ago

@Alain00 I'm facing this same issue. Were you able to figure this out ?

eveningkid commented 4 years ago

Hey guys,

tldr: Blob is web-only

I had the same issue and it just occurred to me that Blob does not exist on Node side (it's using buffers instead).

Basically, it is already a stream so you don't need to make a blob out of it.

In my case, I am creating a PDF from Express and I just had to pipe res to the document instance:

router.get('/document.pdf', async (req, res) => {
  const doc = new PDFDocument();

  // vv The following line is the one you're looking for
  doc.pipe(res);

  doc
    .text('And here is some wrapped text...', 100, 300)
    .font('Times-Roman', 13);

  doc.end();

  res.writeHead(200, {
    'Content-Type': 'application/pdf',
  });
});

If this can be of any help :)

Ryan-F3107 commented 3 years ago

facing the same issue too, I wanted to create a pdf on backend and then use the blob and save it on firebase storage.

dansalado commented 3 years ago

Hi! Maybe these could help :)

https://github.com/foliojs/pdfkit/issues/625

https://stackoverflow.com/questions/35658438/node-js-piping-pdfkit-to-a-memory-stream

adeayobandung commented 1 year ago

Hey guys,

tldr: Blob is web-only

I had the same issue and it just occurred to me that Blob does not exist on Node side (it's using buffers instead).

Basically, it is already a stream so you don't need to make a blob out of it.

In my case, I am creating a PDF from Express and I just had to pipe res to the document instance:

router.get('/document.pdf', async (req, res) => {
  const doc = new PDFDocument();

  // vv The following line is the one you're looking for
  doc.pipe(res);

  doc
    .text('And here is some wrapped text...', 100, 300)
    .font('Times-Roman', 13);

  doc.end();

  res.writeHead(200, {
    'Content-Type': 'application/pdf',
  });
});

If this can be of any help :)

Many Thanks, this is help me more.

Ian-Balijawa commented 1 year ago

blobs are web only

use something like this incase you're in an express/nodejs server

import PDFDocument from "pdfkit";
import express from "express";
const router = express.Router();

router.get( '/', async ( req, res ) => {
    const doc = new PDFDocument();

    doc.pipe( res );

    doc.text( 'And here is some wrapped text...', 100, 300 ).font( 'Times-Roman', 13 );

    doc.end();

    res.writeHead( 200, {
        'Content-Type': 'application/pdf',
    } );
} );

export default router;
Lukenickerson commented 3 weeks ago

Looks like Blob was added to node in v14.18.0 https://nodejs.org/api/all.html#all_buffer_class-blob