Having Trouble Getting PDF attachment to save to disk

  • 1
  • Question
  • Updated 1 year ago
  • Answered
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.
Photo of Sofware Developer Guy

Sofware Developer Guy

  • 1,354 Points 1k badge 2x thumb

Posted 1 year ago

  • 1
Photo of Sofware Developer Guy

Sofware Developer Guy

  • 1,354 Points 1k badge 2x thumb
I was able to figure it out through trial and error and google :)


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.
Photo of Sofware Developer Guy

Sofware Developer Guy

  • 1,354 Points 1k badge 2x thumb
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:

  <model id="PackingSlip" limit="1" query="true" createrowifnonefound="false" adapter="" type="" sobject="Attachment" doclone="">
         <fields>
            <field id="Body"/>
            <field id="Id"/>
            <field id="ContentType"/>
            <field id="Name"/>
            <field id="ParentId"/>
            <field id="Parent.Name"/>
         </fields>
         <conditions>
            <condition type="fieldvalue" value="" enclosevalueinquotes="true" field="ParentId" operator="=" state="filterableon" inactive="false" name="ParentId"/>
            <condition type="fieldvalue" value="PackingSlip" enclosevalueinquotes="true" field="Name" operator="contains"/>
         </conditions>
         <actions/>
      </model>
      <model id="OrderReceipt" limit="1" query="true" createrowifnonefound="false" adapter="" type="" sobject="Attachment" doclone="">
         <fields>
            <field id="Body"/>
            <field id="Id"/>
            <field id="ContentType"/>
            <field id="Name"/>
            <field id="ParentId"/>
            <field id="Parent.Name"/>
         </fields>
         <conditions>
            <condition type="fieldvalue" value="" enclosevalueinquotes="true" field="ParentId" operator="=" state="filterableon" inactive="false" name="ParentId"/>
            <condition type="fieldvalue" value="OrderReceipt" enclosevalueinquotes="true" field="Name" operator="contains"/>
         </conditions>
         <actions/>
      </model>


Why am I getting the Body looking differently?