How can we download PDF file to local drive in Lightning instead of storing in SF? On click of a button, PDF should be downloaded. The button click will initiate an API call (REST) and the response can either be a Base64 encoded string or a blob.

I am aware of the options on how we can handle the content in SF based on the response received. Either we can use EncodingUtil.base64Decode() or resp.getBodyAsBlob() methods to handle the response and convert to PDF in Salesforce.

Could anyone suggest how to download the same PDF file directly to system instead of storing in Salesforce.

  • For encoding/decoding into Base64 Javascript has atob() and btoa() methods. You can take a look at the docs in MDN Commented Jan 8, 2021 at 3:37

1 Answer 1


I've already mentioned this for Aura, and the solution in LWC is almost identical:

   let downloadLink = document.createElement("a");
   downloadLink.href = "data:application/pdf;base64,"+base64data;
   downloadLink.download = "filename.html";

Where base64data is the data obtained from Apex after performing the callout, encoded in base-64 for easy transport from the server:

 @AuraEnabled public static String getPDF() {
   HttpResponse res = getThePDF();
   return EncodingUtil.base64encode(res.getBodyAsBlob());
  • Just a few minor tweaks, in the template declare an hidden anchor tag, and get its reference within js controller using this.template.queryselector('specifiedclass' ) and then set the href & download properties followed by triggering the click event. Commented Jan 8, 2021 at 3:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .