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.
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 β
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
From Developer Console >> File >> New >> Visualforce Page
IframeStyle.vfp [Visualforce Page]
<apex:page standardController="Contact" extensions="IframeStyleVfCtrl" showHeader="false" title="Iframe Style Vf PDF">
<html style="height:100%; width:100%">
<head><title>Test Title</title></head>
<body style="height:100%; width:100%">
<br/>
<div style="display:{! IF(saved == false, 'block', 'none')};" id ="theButton">
<apex:form >
<table class="repeatTableHeader" columns="10" style="width:100%; border-collapse: collapse; page-break-inside: avoid;">
<tr>
<!--<td colspan="2" style="width:10%; height: 10px;border: 1px solid black"></td>-->
<td colspan="5" style="width:50%; height: 10px; text-align:right;"><apex:commandButton action="{!cancel}" value="Cancel" id="theCancelButton" style="width:70px;"/></td>
<td colspan="5" style="width:50%; height: 10px;"><apex:commandButton action="{!iframePDF}" value="Save PDF" id="theButton" style="width:70px;"/></td>
</tr>
</table>
</apex:form>
</div>
<br/>
<div id ="thePDF" style="height:100%; width:100%; display:{! IF(saved == false, 'block', 'none')}">
<iframe style="display:{! IF(typeName == 'Web', 'block', 'none')};height:100%; width:100%" src="{! baseURL+'/apex/webVf?id='+recordId}" frameBorder="0"/>
<iframe style="display:{! IF(typeName == 'Phone Inquiry', 'block', 'none')};height:100%; width:100%" src="{! baseURL+'apex/phoneInquiryVf?id='+recordId}" frameBorder="0"/>
<iframe style="display:{! IF(typeName == 'Partner Referral', 'block', 'none')};height:100%; width:100%" src="{! baseURL+'/apex/partnerReferralVf?id='+recordId}" frameBorder="0"/>
</div>
<div id ="theMessage" style="height:100%; width:100%; display:{! IF(saved != false, 'block', 'none')}">
PDF has been saved to the Quote.
</div>
</body>
</html>
</apex:page>
Create Visualforce Page
Step 2:- Create Visualforce Page : webVf.vfp
From Developer Console >> File >> New >> Visualforce Page
webVf.vfp [Visualforce Page]
<apex:page renderAs="pdf" showHeader="false" applyHtmlTag="false">
web..
</apex:page>
Create Visualforce Page
Step 3:- Create Visualforce Page : phoneInquiryVf.vfp
From Developer Console >> File >> New >> Visualforce Page
phoneInquiryVf.vfp [Visualforce Page]
<apex:page renderAs="pdf" showHeader="false" applyHtmlTag="false">
Phone Inquiry...
</apex:page>
Create Visualforce Page
Step 4:- Create Visualforce Page : partnerReferralVf.vfp
From Developer Console >> File >> New >> Visualforce Page
partnerReferralVf.vfp [Visualforce Page]
<apex:page renderAs="pdf">
Partner Referral..
</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]
public class IframeStyleVfCtrl {
public static String recordId{GET;SET;}
public BOOLEAN saved{GET;SET;}
public String baseURL{GET;SET;}
public String currentUrl{GET;SET;}
public String typeName{GET;SET;}
public IframeStyleVfCtrl(ApexPages.StandardController stdController){
baseURL = URL.getSalesforceBaseUrl().toExternalForm();
currentUrl=ApexPages.currentPage().getUrl();
saved= FALSE;
String recId;
recId = System.currentPageReference().getParameters().get('id');
recordId = recId;
system.debug('recId!!! ' + recId);
IF(recId != NULL){
Contact con=[SELECT ID,Name, FirstName, LastName, LeadSource FROM Contact WHERE Id = :recordId];
system.debug('con## ' + con);
typeName = con.LeadSource;
System.debug('recordId222' + recordId);
system.debug('typeName ' + typeName);
//String yourFileNameName = 'nameofFile.pdf' ;
}
}
public PageReference iframePDF(){
PageReference PDf;
IF(typeName == 'Web'){
PDf = Page.webVf;
}ELSE IF(typeName == 'Phone Inquiry'){
PDf = Page.phoneInquiryVf;
}ELSE IF(typeName == 'Partner Referral'){
PDf = Page.partnerReferralVf;
}ELSE{
}
IF(pdf!= NULL){
System.debug('recordId here :::' + recordId);
System.debug('recordId here2 :::' + System.currentPageReference().getParameters().get('id'));
PDf.getParameters().put('Id',System.currentPageReference().getParameters().get('id'));
ContentVersion conVer = NEW ContentVersion();
conVer.ContentLocation = 'S';
conVer.PathOnClient = 'Contact.pdf';
conVer.Title = 'Contact PDF '+String.valueOf(DATE.today());
conVer.VersionData = PDf.getContent();
INSERT conVer;
Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
System.debug('recordId :::' + recordId);
ContentDocumentLink conDocLink = NEW ContentDocumentLink();
conDocLink.LinkedEntityId = System.currentPageReference().getParameters().get('id');
conDocLink.ContentDocumentId = conDoc;
conDocLink.shareType = 'V';
INSERT conDocLink;
//PageReference pageRef = NEW PageReference( baseURL+'/lightning/r/Contact/' + System.currentPageReference().getParameters().get('id')+'/related/AttachedContentDocuments/view');
PageReference pageRef = NEW PageReference( baseURL+'/lightning/r/Contact/' + System.currentPageReference().getParameters().get('id')+'/view');
pageRef.setRedirect(TRUE);
system.debug('pageRef@@@ ' + pageRef);
RETURN pageRef;
}
ELSE{RETURN NULL;}
}
}
Further post that would you like to learn in Salesforce
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
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 |