最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getting a blob from the stream - Stack Overflow

programmeradmin5浏览0评论

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
    });
  },

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
    });
  },
Share Improve this question asked Sep 21, 2017 at 2:02 Fred J.Fred J. 6,04913 gold badges60 silver badges113 bronze badges 1
  • from where does metaData e from? – Ankur Soni Commented Sep 21, 2017 at 2:28
Add a ment  | 

1 Answer 1

Reset to default 3

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();
发布评论

评论列表(0)

  1. 暂无评论