Having Trouble Getting PDF attachment to save to disk

I am trying to get a PDF that I have in an attachment object saved out to disk.

I am using something like this:

blobFile = results.models.PackingSlip.data[0].Body;
var bf = new Blob([results.models.PackingSlip.data[“0”].Body.realBlobValue.asByteArray ], {type : “application/pdf”});
saveAs(bf, “packingSlip.pdf”);

this is what the Body of the Attachement looks like from the Model. So skuid is retrieving the Attachment PDF correctly.

The PDF is downloaded correctly, however, when I go and view it. I get this:

So my question is:
How do I properly get the Blob from the object above so that its an actual PDF?

Thanks in advance.

I was able to figure it out through trial and error and google :slight_smile:


var params = arguments[0],
$ = skuid.$;

var orderId = params.id,
acctName = params.Account__r.Name,
ordDate = params.Order_Date__c,
fileName =null;

fileName = “PackingSlip_” + ordDate + “_” + acctName + “.pdf”;

modelToFilter = skuid.model.getModel(‘PackingSlip’);
multipleCond = modelToFilter.getConditionByName(‘ParentId’);
modelToFilter.setCondition(multipleCond, orderId);
modelToFilter.updateData( function (results) {

    var theBlobData = null,
    theByteArray = null,
    byteCharacters = null,
    byteNumbers = null;
    byteArray = null,
    theBlobFile = null;

    theBlobData = results.models.PackingSlip.data[0];
    theByteArray = theBlobData.Body.realBlobValue.asByteArray;
    byteCharacters = atob(theByteArray);
    byteNumbers = new Array(byteCharacters.length);
    
    for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    byteArray = new Uint8Array(byteNumbers);
    theBlobFile = new Blob([byteArray], {type : “application/pdf”});
    saveAs( theBlobFile, fileName);
});


this works.
Note it uses FileSaver.js for the saveAs() method.

Hope it helps someone.


This is a little troubling:

Why is the asByteArray attribute in different locations:
for example for the OrderReceipt (which is an attachement)
the results.models.OrderReceipt.data[0]
looks like this:

But for another attachment file:
results.models.PackingSlip.data[0]
it looks like this:

Both the models are pointing to the Attachment object and are similar:

Why am I getting the Body looking differently?