Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting a blob from the stream

This Meteor server code tries to create a pdf in order to attach it to an email. It make the pdf Ok but when puting it into a blob, it returns this error:

TypeError: Blob is not a function

"use strict";
let PDFDocument = require('pdfkit');
let blobStream = require('blob-stream');

'make': function () {
    let doc = new PDFDocument(metaData);  // readable Node streams
    let stream = doc.pipe(blobStream());
    doc.fontSize('14')
      .text('some text')
      .end();
    stream.on('finish', function (blob) {
      return stream.toBlob(blob); //<=== error line
    });
  },
like image 689
Fred J. Avatar asked Sep 03 '25 14:09

Fred J.


1 Answers

As per Documentation , stream.toBlob() is expecting type of output for blob, i.e. application/text, application/pdf.

Can you please try below peice of code,

stream.on('finish', function () {
 //get a blob you can do whatever you like with
 blob = stream.toBlob('application/pdf');
 return blob;
});

Kindly go through the Documentation. It says below,

PDFDocument instances are readable Node streams. They don't get saved anywhere automatically, but you can call the pipe method to send the output of the PDF document to another writable Node stream as it is being written. When you're done with your document, call the end method to finalize it. Here is an example showing how to pipe to a file or an HTTP response.

doc.pipe fs.createWriteStream('/path/to/file.pdf') # write to PDF
doc.pipe res                                       # HTTP response

# add stuff to PDF here using methods described below...

# finalize the PDF and end the stream
doc.end();
like image 118
Ankur Soni Avatar answered Sep 05 '25 06:09

Ankur Soni