Wednesday 13 January 2016

Download PDF as Zip file Without Saving to Attachments and Documents In Salesforce


                        Hi all. I already wrote about Downloading PDF as zip files from the list view of the object(you can check here the Post). But that process will save to Attachments and document and from Documents it will download the zip file. So, come up with the solution to without saving to Anywhere(Notes and Attachment and Document).

I used two things

  1. Webservice Method
  2. Onclick Javascript
step 1 :
            Am using Invoice__c Object 

 so here is the code for Webservice Method.

 
    global class InvoiceDownload {

        //Private List<Invoice__c> selected;
          private static String API_STATUS_NORMAL = '200';

         webservice static String getFilesToDownload(string sfdcId)
        {
            List<id> ids = new List<id>();
            string[] idsArray = sfdcId.split(',');
            for(integer i=0; i<idsArray.size();i++)
           {
              ids.add(idsArray[i]);
           }
          List<Invoice__c> selected = [select name from Invoice__c where id in : ids];
          Map<String,String> files = new Map<String,String>();
          for(Invoice__c inv : selected)
          {
              Blob pdf = new PageReference('/apex/InvoicePDF?id='+inv.Id).getContentAsPDF();
              files.put(inv.Name+'.pdf', EncodingUtil.base64Encode(pdf));
          }
          return JSON.serialize(files);
        }
    }


step 2 :

          Create a List view button and choose Onclick Javascript. Click here for Placing List view Button if you have any doubt Refer step 5.



{!REQUIRESCRIPT("/resource/JSzip")}
{!REQUIRESCRIPT("/resource/filesaver")}
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var download = function() {
        var records = {!GetRecordIds($ObjectType.Invoice__c)};
        var SelectedIds='';
        var fileDatafinal;
        for(var i=0;i < records.length; i++)
           {
           SelectedIds+=records[i]+",";
           }
           SelectedIds=SelectedIds.substring(0,SelectedIds.length - 1);
           if(SelectedIds.length > 0){
               alert(' Please wait until end of the process ');
               var response = sforce.apex.execute("InvoiceDownload","getFilesToDownload",{sfdcId:SelectedIds});
               var files = JSON.parse(response);      
               var zip = new JSZip();
               for(fileName in files){
     var fileBase64String = files[fileName];
     zip.file(fileName, fileBase64String, { base64: true });
               }
            saveAs(zip.generate({type:"blob"}), 'Invoice Download.zip', false);
    confirm('Please confirm to download');
           }
           else
   {
 alert('Please select atlease one record');
   }
}
download();


      Am getting the selected id's record by getRecordId method in javascript and am passing those id's to the webservice method that returns the pdf file and saving those to filesaver using saveAs method and JsZip is used to zip the file.

You can download JsZip.js, FileSaver.js from Here.

Happy Downloading!!. Hope you got solution.

Thanks.
Salesforce Maniacs.

Go to my blog and search for more Saleforce post.

1 comment:

  1. I personally wants you to try File ZIPO once. As it helps you to ZIP files directly from your Salesforce org eliminating the need to follow the tedious standard process. Try it now!
    https://filezipo.io/zip-files/

    ReplyDelete