从流中获取一个 blob
getting a blob from the stream
此 Meteor 服务器代码尝试创建一个 pdf 以便将其附加到电子邮件中。它使 pdf 正常,但是当把它放入 blob 时,它 returns 这个错误:
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
});
},
根据 Documentation ,stream.toBlob()
期望 blob 的输出类型,即 application/text
、application/pdf
.
你能试试下面的一段代码吗,
stream.on('finish', function () {
//get a blob you can do whatever you like with
blob = stream.toBlob('application/pdf');
return blob;
});
请仔细阅读文档。它在下面说,
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();
此 Meteor 服务器代码尝试创建一个 pdf 以便将其附加到电子邮件中。它使 pdf 正常,但是当把它放入 blob 时,它 returns 这个错误:
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
});
},
根据 Documentation ,stream.toBlob()
期望 blob 的输出类型,即 application/text
、application/pdf
.
你能试试下面的一段代码吗,
stream.on('finish', function () {
//get a blob you can do whatever you like with
blob = stream.toBlob('application/pdf');
return blob;
});
请仔细阅读文档。它在下面说,
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 theend
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();