[SalesForce] Salesforce PDF download using JSZip

SFDC developer here. I have created a VF page which accepts Account id & displays some information on a VF page in tabular form.

I need to make multiple calls to this page with different account ids on a button click & download the tables in them as PDF/Text files in a zip. I was thinking about using JSZip to download all PDF/Text files as one zip.

I have got Blob value of this VF page in a variable using getContentAsPDF.

Basically i need to know how to use this Blob value for each account table and put it in separate files, download those files one Zip. Any ideas how to proceed?

EDIT 1:

So in the code below, we can see that we generating a list of Blob in the controller. On click of a VF page button i send the Blob list as a param to a JavaScript function. In that function i am trying to add each and every blob to the zip and download. But nothing is getting downloaded for me.

Apex Code:

CkIds = returnCommaSeparatedString(setOFCommKitID);
        pr = New PageReference('/apex/PDFPage?id=' + iICPRec +'&CKIds='+CkIds);
        pdf = pr.getContentAsPDF();
        lstOfBlob.add(pdf);

JavaScript Code:

   var IdString;
    var ArrayOfBlob = []; 
    function CallMe(TempVar, VarTemp){
        alert(TempVar);alert(VarTemp);
        IdString = TempVar;
        ArrayOfBlob = VarTemp;
        var zip = new JSZip();
        for (var i=0; i < ArrayOfBlob.length; i++)
        {
            zip.file('Test',ArrayOfBlob[0]);
        }
        var blobLink = document.getElementById('CustomLink');
        blobLink.download = "CommunicationKit.zip";
        blobLink.href = window.URL.createObjectURL(zip.generate());
        blobLink.click();

Best Answer

So i gave up on trying to convert the files into a PDF and download. Instead what i did was i downloaded the files as HTML. The following code did the trick for me.

function initZip{!randomInt}(TempVar){
        var obj = TempVar;
        var zip = new JSZip();

        for (var key in obj) 
        {
            if (obj.hasOwnProperty(key)) 
            {
                zip.file(key.toString()+".html", (obj[key]).toString());                    
            }
        }           

        try
        {
            var content = zip.generate({type:"blob"});  
            saveAs(content, "CommunicationKit.zip");                
        }
        catch(e)
        {
            alert(e);                
        }

        IdString = TempVar;


    }

Where TempVar is controller map of Account id & a string which consists of HTMLText that has the source of the HTML page. So, each record in the map has {account id ==> "HTML code here....."}. And then i download the files as HTML pages in the zip

Related Topic