+ Start a Discussion
Sindhu AmbarkarSindhu Ambarkar 

How to download pdf page automatically using render as pdf


I want to download the pdf automatically using render as pdf i tried using contenttype attribute.Its throwing error

Thanks & Regards,
Sindhu Ambarkar.
Hi Sindhu,

renderAs="pdf" authomatically download your pdf. If you are using Google chorme browser, it will open pdf. But if using Firefox browser, hitting URL will automatically download page.

Sindhu AmbarkarSindhu Ambarkar
Hi Tarun,

If i want to download in chrome browser then what needs to be done.

Thanks & regards,
Sindhu Ambarkar.
No, There is no way. Because chorme has ability to open pdf. It's not related to salesforce. Its related to browser. If there is any way, so that chrome stops opening pdf, then use this. Else in chorme, open pdf and use Ctrl + S option. :)

Sindhu AmbarkarSindhu Ambarkar
Thanks Tarun.
I have used 
<body style="border:1px solid black;  bgcolor:green; font-family: Italic; font-size: 20px;">
But background color is getting reflected.

Thanks & regards,
Sindhu Ambarkar.
Brandon Chang 9Brandon Chang 9
You can achieve this with two files. 

1) RenderPDF.vfp
<apex:page showHeader="false" applyBodyTag="false" renderAs="PDF" standardController="Lead">
    Hello PDF

2) ExportToPDF.vfp
<!--Override to ALWAYS download -->
<apex:page contentType="application/vnd.ms-pdf#output.pdf">   
    <apex:include pageName="RenderPDF"/>        

One page renders the PDF, the other one includes and downloads. 
Sohit Sharma 4Sohit Sharma 4
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>
  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
  // in case the Blob uses a lot of memory
       setTimeout(() => URL.revokeObjectURL(link.href), 20000);

 <apex:outputText value="{!downloadJS}" escape="false"></apex:outputText>

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.setHeader('Content-Type', 'application/json');
        request.setEndpoint('request url');
       HttpResponse response = http.send(request);
        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>';