Hey guys, today in this post we are going to learn about How to save instantly my pdf visualforcepage as a file/attachment on click button in Salesforce.
You can use the PageReference.getContentAsPDF() method in Apex to render a Visualforce page as PDF data. Then use Apex code to convert that PDF data to an email attachment, a document, a Chatter post, and so on. To know more details about Render a Visualforce Page, Click Here.
Files we used to Save the Attachment as PDF using Apex Class in Visualforce Page →
savePdfVfp.vfp | Visualforce | It’s holds “Save Attachment” and “Cancel” Button Functionality. |
savePdfVp.vfp | Visualforce | It is render as pdf from visualforce page. |
saveVfPdfCtrl.apxc | Apex Class Controller | It is used to generate pdf using Bolb Apex and Save file as pdf in file/attachment in Salesforce. |
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 : savePdfVfp.vfp
From Developer Console >> File >> New >> Visualforce Page
savePdfVfp.vfp [Visualforce Page]
<apex:page standardController="Account" extensions="saveVfPdfCtrl" showHeader="false" title="Quotation PDF" showQuickActionVfHeader="false" >
<apex:form >
<center>
<apex:commandButton action="{!pdfAction}" value="Save Attachment"/>
<apex:commandButton action="{!Cancel}" value="Cancel" /> </center> <br/>
<center>
<apex:iframe height="700px" width="1100px" src="/apex/savePdfVp?id={!MstrID}"/>
</center>
</apex:form><br/><br/><br/>
<footer class="slds-modal__footer"></footer>
</apex:page>
Create Visualforce Page
Step 2:- Create Visualforce Page : savePdfVp.vfp
From Developer Console >> File >> New >> Visualforce Page
savePdfVp.vfp [Visualforce Page]
<apex:page standardController="Account" extensions="saveVfPdfCtrl" renderAs="pdf" applyBodyTag="false">
<head>
<style>
@page {
size: A4 portrait;
margin: 3mm;
}
body {
font-family: sans-serif;
font-size: 11pt;
}
th {
min-height: 15px;
max-height: auto;
background:#ddd;
}
td {
min-height: 15px;
max-height: auto;
}
</style>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="10" style="width: 100%; border-collapse: collapse; border-color: #000; text-align:left;">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Industry</th>
<th>Rating</th>
<th>Description</th>
<th>Website</th>
</tr>
</thead>
<apex:repeat value="{!accObj}" var="accItem">
<tr width="100%" style="text-align: center;">
<td style="text-align:left;"><apex:outputText value="{!accItem.Name}"/></td>
<td style="text-align:left;"><apex:outputText value="{!accItem.Phone}"/></td>
<td style="text-align:left;"><apex:outputText value="{!accItem.Industry}"/></td>
<td style="text-align:left;"><apex:outputText value="{!accItem.Rating}"/></td>
<td style="text-align:left;"><apex:outputText value="{!accItem.Description}"/></td>
<td style="text-align:left;"><apex:outputText value="{!accItem.Website}"/></td>
</tr>
</apex:repeat>
</table>
</body>
</apex:page>
Create Apex Class Extension Controller in Visualforce
Step 3:- Create Apex Class : saveVfPdfCtrl.apxc
From Developer Console >> File >> New >> Apex Class
saveVfPdfCtrl.apxc [Apex Class Controller]
public class saveVfPdfCtrl {
public String MstrId{GET;SET;}
public Account accObj{GET;SET;}
public String PDFNo{GET;SET;}
public String EFNo{GET;SET;}
public BOOLEAN SHOW{GET;SET;}
public BOOLEAN showpdf{GET;SET;}
public ApexPages.PageReference page2{GET;SET;}
public String baseURL{GET;SET;}
public PageReference Cancel()
{
PageReference Pdf = NEW PageReference('/'+MstrID);
pdf.setredirect(TRUE);
RETURN Pdf;
}
public saveVfPdfCtrl(ApexPages.StandardController Controller){
baseURL = URL.getSalesforceBaseUrl().toExternalForm();
MstrId = ApexPages.currentPage().getParameters().get('id');
accObj = [SELECT Id, Name, Phone, Industry, Rating, Description, Website, TYPE, (SELECT Id, Name, FirstName, LastName, Email, AccountId, Phone, Title FROM Contacts) FROM Account WHERE Id =: MstrId ];
}
public PageReference pdfAction()
{
PageReference savepage ;
savepage = Page.savePdfVp;
savepage.getParameters().put('id',MstrID);
system.debug('id:- '+MstrID);
BLOB pdfBlob;
IF (!Test.isRunningTest()) {
pdfBlob = savepage.getContent(); //generate the pdf BLOB
} ELSE {
pdfBlob = BLOB.valueOf('Test');
}
List<ContentDocumentLink> notesattch = [SELECT id, ContentDocument.Title,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: MstrID ORDER BY ContentDocument.Title ASC];
system.debug('notesattch## ' + notesattch);
IF(notesattch.size() > 0)
{
string title = notesattch[0].ContentDocument.Title;
system.debug('title111 ' + title);
List<String> titleSplit = title.split('R');
//String FinalTitle = titleSplit[0]+'R0'+notesattch.size();
String FinalTitle = 'PO'+notesattch.size();
system.debug('FinalTitle22 ' + FinalTitle);
PDFNo=FinalTitle;
ContentVersion conVer = NEW ContentVersion();
conVer.ContentLocation = 'S'; // TO USE S specify this document IS IN Salesforce, TO USE E FOR external files
conVer.PathOnClient = FinalTitle+'.pdf';
conVer.Title = FinalTitle;
conVer.VersionData = pdfBlob;
system.debug('conVer@@ ' + conVer);
INSERT conVer;
Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
ContentDocumentLink conDocLink = NEW ContentDocumentLink();
conDocLink.LinkedEntityId = MstrID;
conDocLink.ContentDocumentId = conDoc;
conDocLink.shareType = 'V';
INSERT conDocLink;
UPDATE accObj;
PageReference pageRef = NEW PageReference( baseURL+'/lightning/r/Account/' + 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
How do I save a VF page rendered as a PDF?
If you try to render a Visualforce page as a PDF document, usually the PDF document is displayed in the browser. Place the “Render PDF” button on the Account page. If you click the button, the PDF page appears in the browser.
How do I email a VF page from Salesforce?
It is possible to send email using Visualforce by creating a custom controller to deliver the message. The Apex Messaging. SingleEmailMessage class handles the outbound email functionality available to Salesforce.
How do I convert files to attachments in Salesforce?
Switch to Attachments and Notes migrator app from App Menu . Click on “Attachments to Files” tab. range to convert the attachment to files.
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 |