How to Show/Download pdf in visualforce page using iframe with dynamic in Salesforce | Display a blob as a PDF on a Visualforce page | How do I display the contents of a Visualforce page as a PDF | Visualforce iframe download pdf based on condition apply in Salesforce

1,574 views


Hey guys, today in this post we are going to learn about How to Show/Download pdf in visualforce page using iframe with dynamic in Salesforce.

apex:iframe

A component that creates an inline frame within a Visualforce page. A frame allows you to keep some information visible while other information is scrolled or replaced.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

This component supports HTML pass-through attributes using the “html-” prefix. Pass-through attributes are attached to the generated <iframe> tag. To know more about apex:iframe, Click Here.

 

 

Final Output →

iframe visualforce download pdf -- w3web.net

 

You can download file directly from github by Click Here.

 

 

Other related post that would you like to learn in Salesforce

 

  • Find the below steps ▾

 

Create Visualforce Page

Step 1:- Create Visualforce Page : IframeStyle.vfp

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

From Developer Console >> File >> New >> Visualforce Page

IframeStyle.vfp [Visualforce Page]

  1.    <apex:page standardController="Contact"  extensions="IframeStyleVfCtrl" showHeader="false" title="Iframe Style Vf PDF">
  2.  
  3.     <html style="height:100%; width:100%">
  4.         <head><title>Test Title</title></head>
  5.         <body style="height:100%; width:100%">
  6.             <br/>
  7.             <div style="display:{! IF(saved == false, 'block', 'none')};" id ="theButton">
  8.         <apex:form >
  9.             <table class="repeatTableHeader" columns="10" style="width:100%; border-collapse: collapse; page-break-inside: avoid;">
  10.             <tr>
  11. <!--<td colspan="2" style="width:10%; height: 10px;border: 1px solid black"></td>-->
  12.             <td colspan="5" style="width:50%; height: 10px; text-align:right;"><apex:commandButton action="{!cancel}" value="Cancel" id="theCancelButton" style="width:70px;"/></td>
  13.  
  14.                 <td colspan="5" style="width:50%; height: 10px;"><apex:commandButton action="{!iframePDF}" value="Save PDF" id="theButton" style="width:70px;"/></td>
  15.         </tr>
  16.         </table>
  17.  
  18.         </apex:form>
  19.     </div>
  20.     <br/>
  21.  
  22.  
  23.             <div id ="thePDF" style="height:100%; width:100%; display:{! IF(saved == false, 'block', 'none')}">
  24.  
  25.                 <iframe style="display:{! IF(typeName == 'Web', 'block', 'none')};height:100%; width:100%" src="{! baseURL+'/apex/webVf?id='+recordId}" frameBorder="0"/>
  26.                 <iframe style="display:{! IF(typeName == 'Phone Inquiry', 'block', 'none')};height:100%; width:100%" src="{! baseURL+'apex/phoneInquiryVf?id='+recordId}" frameBorder="0"/>
  27.                 <iframe style="display:{! IF(typeName == 'Partner Referral', 'block', 'none')};height:100%; width:100%" src="{! baseURL+'/apex/partnerReferralVf?id='+recordId}" frameBorder="0"/>
  28.             </div>
  29.  
  30.             <div  id ="theMessage" style="height:100%; width:100%; display:{! IF(saved != false, 'block', 'none')}">
  31.  
  32.                 PDF has been saved to the Quote.
  33.             </div>
  34.  
  35.         </body>
  36.     </html>
  37.  
  38. </apex:page>

 

Create Visualforce Page

Step 2:- Create Visualforce Page : webVf.vfp

From Developer Console >> File >> New >> Visualforce Page

webVf.vfp [Visualforce Page]

  1.    <apex:page renderAs="pdf" showHeader="false" applyHtmlTag="false">
  2.     web..
  3. </apex:page>

 

Create Visualforce Page

Step 3:- Create Visualforce Page : phoneInquiryVf.vfp

From Developer Console >> File >> New >> Visualforce Page

phoneInquiryVf.vfp [Visualforce Page]

  1.    <apex:page renderAs="pdf" showHeader="false" applyHtmlTag="false">
  2.      Phone Inquiry...
  3. </apex:page>

 

Create Visualforce Page

Step 4:- Create Visualforce Page : partnerReferralVf.vfp

From Developer Console >> File >> New >> Visualforce Page

partnerReferralVf.vfp [Visualforce Page]

  1.    <apex:page renderAs="pdf">   
  2.     Partner Referral..
  3. </apex:page>

 

Create Apex Class Controller

Step 5:- Create Apex Class : IframeStyleVfCtrl.apxc

From Developer Console >> File >> New >> Apex Class

IframeStyleVfCtrl.apxc [Apex Class Controller]

  1.   public class IframeStyleVfCtrl {
  2.     public static String recordId{GET;SET;}
  3.     public BOOLEAN saved{GET;SET;}    
  4.     public String baseURL{GET;SET;}
  5.     public String currentUrl{GET;SET;}
  6.     public String typeName{GET;SET;}
  7.  
  8.     public IframeStyleVfCtrl(ApexPages.StandardController stdController){
  9.         baseURL = URL.getSalesforceBaseUrl().toExternalForm();
  10.         currentUrl=ApexPages.currentPage().getUrl();
  11.         saved= FALSE;
  12.  
  13.         String recId;
  14.         recId = System.currentPageReference().getParameters().get('id');
  15.         recordId = recId;
  16.         system.debug('recId!!! ' + recId);
  17.  
  18.         IF(recId != NULL){
  19.             Contact con=[SELECT ID,Name, FirstName, LastName, LeadSource FROM Contact WHERE Id = :recordId];
  20.             system.debug('con## ' + con);
  21.             typeName = con.LeadSource;
  22.             System.debug('recordId222' + recordId);
  23.             system.debug('typeName ' + typeName);
  24.             //String yourFileNameName = 'nameofFile.pdf' ;
  25.         }
  26.  
  27.     }
  28.  
  29.  
  30.     public PageReference iframePDF(){
  31.         PageReference PDf;
  32.  
  33.         IF(typeName == 'Web'){           
  34.             PDf =  Page.webVf;
  35.         }ELSE IF(typeName == 'Phone Inquiry'){           
  36.             PDf =  Page.phoneInquiryVf;
  37.         }ELSE IF(typeName == 'Partner Referral'){            
  38.             PDf =  Page.partnerReferralVf;
  39.         }ELSE{
  40.  
  41.         }
  42.  
  43.         IF(pdf!= NULL){
  44.             System.debug('recordId here :::' + recordId);
  45.             System.debug('recordId here2 :::' + System.currentPageReference().getParameters().get('id'));
  46.             PDf.getParameters().put('Id',System.currentPageReference().getParameters().get('id'));
  47.             ContentVersion conVer = NEW ContentVersion();
  48.             conVer.ContentLocation = 'S'; 
  49.             conVer.PathOnClient = 'Contact.pdf'; 
  50.             conVer.Title = 'Contact PDF '+String.valueOf(DATE.today()); 
  51.             conVer.VersionData = PDf.getContent(); 
  52.             INSERT conVer;
  53.  
  54.             Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
  55.             System.debug('recordId :::' + recordId);
  56.             ContentDocumentLink conDocLink = NEW ContentDocumentLink();
  57.             conDocLink.LinkedEntityId = System.currentPageReference().getParameters().get('id'); 
  58.             conDocLink.ContentDocumentId = conDoc;  
  59.             conDocLink.shareType = 'V';
  60.             INSERT conDocLink;
  61.             //PageReference pageRef = NEW PageReference( baseURL+'/lightning/r/Contact/' + System.currentPageReference().getParameters().get('id')+'/related/AttachedContentDocuments/view');
  62.             PageReference pageRef = NEW PageReference( baseURL+'/lightning/r/Contact/' + System.currentPageReference().getParameters().get('id')+'/view');
  63.         	pageRef.setRedirect(TRUE);
  64.             system.debug('pageRef@@@ ' + pageRef);
  65.             RETURN pageRef;
  66.  
  67.         }
  68.  
  69.  
  70.         ELSE{RETURN NULL;}
  71.  
  72.     }
  73.  
  74. }

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

Is iframe supported in Salesforce?

Most iframes are compatible with Visualforce in Lightning Experience.

What is iframe used for?

An inline frame (iframe) is a HTML element that loads another HTML page within the document. It essentially puts another webpage within the parent page. They are commonly used for advertisements, embedded videos, web analytics and interactive content.

Why is iframe not safe?

The iFrame contains a malicious form that can lead the user to submit sensitive information. This threat can be solved by using sandbox with removing allow-forms . The iFrame may unintentionally download malware to the user's computer.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment