What is blob-stream?
The blob-stream npm package is used to create a writable stream that generates a Blob. This is particularly useful for generating files in the browser, such as PDFs, images, or any other binary data that can be represented as a Blob.
Creating a Blob from a Stream
This feature allows you to create a Blob from a writable stream. In this example, a PDF document is created using the pdfkit library, and its output is piped to a blob-stream. Once the stream is finished, you can get the Blob and use it as needed, such as creating a URL for download.
const blobStream = require('blob-stream');
const PDFDocument = require('pdfkit');
// Create a document
const doc = new PDFDocument();
// Pipe its output somewhere, like to a blob
const stream = doc.pipe(blobStream());
// Add some content to the document
doc.text('Hello, world!');
// Finalize the PDF and end the stream
doc.end();
// When the stream is finished, get the blob
stream.on('finish', function() {
// get a blob you can do whatever you like with
const blob = stream.toBlob('application/pdf');
const url = stream.toBlobURL('application/pdf');
console.log(url);
});