I have created a VF page with renderas=PDF and added it as a quick action on the Account object in Lightning Experience. When I click the quick action, a PDF view displays and is correct. How can I add a button or icon to Download that PDF and also a button or icon to Print that PDF?
3 Answers
Print & download option comes from the browser's pdf viewer.
If you still want to have a button somewhere in your org to download the page, have the button, onclick of which redirect the user to the VF page and when the page load, have the following line in the constructor of the controller
Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=YourFileName.pdf');
It should save a PDF in your local drive.
You can have a salesforce button for print as well.
window.print()
is the function you need to call from the button, it will send the current page for print.
Please note: you can not have this Javascript in a VF page which is rendered as PDF, JS doesn't run when the page is rendered as PDF.
-
Hi @santanu Halder pdf is getting download for me but the page is not showing/displaying the pdf now it is blank page Commented Jul 11, 2019 at 12:25
-
my requirement is while clicking the Quick action button it should display the dpf and download the pdf automatically Commented Jul 11, 2019 at 12:26
-
@nubie, I would suggest you to ask a new question about your issue, so that larger audience gets to help you. Commented Jul 16, 2019 at 5:17
I have the solution, I am using the javaScript download function in two ways
Visualforce page:
<apex:page controller="VFPageDownloaderController" action="{!downloadAutomatic}">
<apex:outputText value="{!msg}" escape="false"></apex:outputText>
<script>
function download(s,filename,ext)
{
console.log(s+'Function call');
const link = document.createElement('a');
// create a blobURI pointing to our Blob
link.href = 'data:application/'+ext+';base64,'+s;
link.download = filename+'.'+ext;
// some browser needs the anchor to be in the doc
document.body.append(link);
link.click();
link.remove();
// in case the Blob uses a lot of memory
setTimeout(() => URL.revokeObjectURL(link.href), 20000);
}
</script>
<apex:outputText value="{!downloadJS}" escape="false"></apex:outputText>
</apex:page>
Visualforce controller:
public class VFPageDownloaderController {
public String msg {get;set;}
public String downloadJS {get;set;}
//created a function to make the API request
public void downloadAutomatic(){
String base64String = '';
// download file as a blob from url request
/*
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setMethod('GET');
request.setHeader('Content-Type', 'application/json');
request.setEndpoint('request url');
HttpResponse response = http.send(request);
System.debug(response.getBodyAsBlob());
Blob b = response.getBodyAsBlob();
base64String = EncodingUtil.base64Encode(b); */
// download file from ContentVersion
ContentVersion c = [SELECT Id, VersionData, FileExtension, Title FROM ContentVersion limit 1];
base64String = EncodingUtil.base64Encode(c.VersionData);
String fileName = 'abc';
if (base64String == '') {
msg = 'file Not Found';
}else {
downloadJS ='<script> download("'+base64String+'","'+fileName+'","'+c.FileExtension+'"); </script>';
}
}
}
If this question is still open, we can follow below steps to implement the download in lightning experience
- You should have a visualforce page for generating pdf.
In lightning component use following markup
<!--lightning button to download the pdf as generated reports --> <div class="slds-float--right"> <button class="slds-button" onclick="{!c.download}"> <lightning:icon class="slds-icon slds-input__icon slds-input__icon_right slds-icon-text-default" iconName="utility:download" size="small" alternativeText="Indicates approval"/> </button> </div>
Lightning controller should have following markup
//Download Reports as Pdf File download : function (component,event,helper) { helper.downloadPdfFile(component,event,helper); },
Lightning helper should have following markup
// Generating Pdf through VisualForce Page downloadPdfFile : function(component,event,helper){ var selectedwarehouse = component.get("v.selectedProduct"); //pass this value into vf page var fromDate = component.find("fromDate").get("v.value"); /pass this value into vf page var toDate = component.find("toDate").get("v.value"); /pass this value into vf page window.setTimeout( $A.getCallback(function() { // visualforce page URL window.location="/apex/WarehousePdf?fromDate="+fromDate+"&toDate="+toDate+"&selectedwarehouse="+selectedwarehouse; }), 1000 ); },