Quantcast
Channel: Appcelerator Developer Center Q&A Tag Feed (ti.blob)
Viewing all articles
Browse latest Browse all 4

How do I create a Blob from my own generated data?

$
0
0

I am searching but finding any examples woefully incomplete. Here is my situation: I am trying to get pdfkit.js working in Titanium. https://github.com/devongovett/pdfkit

Actually, so far, that has been successful. The web/html5 version just needed the bind function to exist in titanium and requiring this bind.js file was all it needed:

if (!Function.prototype.bind) {
  Function.prototype.bind = function(oThis) {
    if ( typeof this !== "function") {
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
    }
    var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, fNOP = function() {
    }, fBound = function() {
      return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
    };
    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();
    return fBound;
  };
}
I'm just including this in case anyone feels like running pdfkit.js. Just add this code somewhere before you require pdfkit.js and all is well.

However, PDF kit wants to pipe it's data to a blob. This is where I get stuck. The documentation on how to get this data into a Titanium.Blob is lacking.

One thing I decided to try was to get blob-stream working in Titanium: https://github.com/devongovett/blob-stream

Again, this worked ok, almost Blob-Stream requires support for TypedArrays. Lucky, there was this: https://www.npmjs.org/package/typedarray And through that I was able to require what I needed and add in the line to the start of blob-stream.js:

var Uint8Array = require('typedarray').Uint8Array;
Here is where I am completely stuck. Blob stream, at some final point, creates a Uint8Array assigned to the object _chunks and this is the code that wants to return a blob:
BlobStream.prototype.toBlob = function(type) {
  return new Blob(this._chunks, {
    type: type || 'application/octet-stream'
  });
};
With just pdfkit.js, I have a stream of data that I can look at and JSON.stringify into an array of int byte values.

Running through blob-stream, I can generate a Uint8Array all ready to be shoved into a blob.

And now.... I just need to get this data into a ti.Blob object. There I'll want to print, store and display it. But I assume, being a blob, I can move it around as we would expect to.

I am hoping that someone with far more experience with Ti.Blobs can help this bridge. This is a great idea to support, as the pdfkit.js library is a great start in a javascript PDF creation library.

Thanks in advance.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images