function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Supriyo Ghosh 5Supriyo Ghosh 5 

Generate vf page report as PDF

Hi,

I want to create a button which will generate my vf page report as PDF.

Controller

public class EmpMasterReport
{
   public List<Employee_Master__c> EmpList {get;set;}
   
    public EmpMasterReport(ApexPages.StandardController stdController) 
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c,Date_Of_Birth__c,Date_Of_Joining__c,Date_of_Leaving__c,Thumbnail__c FROM Employee_Master__c where Active__c=true and Photo__c !=null];
    }
    
    public EmpMasterReport()
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c,Date_Of_Birth__c,Date_Of_Joining__c,Date_of_Leaving__c,Thumbnail__c FROM Employee_Master__c where Active__c=true and Photo__c !=null];
    }
   
}

vf Page

<apex:page id="p1" standardController="Employee_Master__c" extensions="EmpMasterReport" sidebar="false" showHeader="false" cache="true">

<apex:form >
<apex:pageBlock title="Employee Master Details" id="Emp_Master">
<apex:pageBlockTable value="{! EmpList }" var="em" border="2">
<apex:column value="{!em.Name}"/>
<apex:column value="{!em.Address__c}"/>
<apex:column value="{!em.Date_Of_Birth__c}"/>
<apex:column value="{!em.Date_Of_Joining__c}"/>
<apex:column value="{!em.Date_of_Leaving__c}"/>
<apex:column value="{!em.Thumbnail__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Please note I cannot use "renderas=pdf".Please help.
bob_buzzardbob_buzzard
If you can't use the renderas, I think your only option is to create a PageReference for this page, set the id of the Employee_Master__c instance in question, and use the getContentAsPDF method.
Supriyo Ghosh 5Supriyo Ghosh 5
I am new to apex.So could you please write a the code so that it will help me.
bob_buzzardbob_buzzard
There are plenty of examples out there for a couple of seconds googling. For example:

https://www.sundoginteractive.com/blog/a-recipe-for-saving-a-pdf-as-an-attachment-salesforce
Supriyo Ghosh 5Supriyo Ghosh 5
Hi,

I have written a code but unable to fetch the record.

Controller
public class EmpMasterReport
{
   public Transient Date datename{set;get;}  
   public List<Employee_Master__c> EmpList {get;set;}
   
    public EmpMasterReport(ApexPages.StandardController stdController) 
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c,Date_Of_Birth__c,Date_Of_Joining__c,Date_of_Leaving__c,Thumbnail__c FROM Employee_Master__c where Active__c=true and Photo__c !=null];
    }
    
    public EmpMasterReport()
    {
        EmpList = new     List<Employee_Master__c>();
        EmpList = [ SELECT Name,Address__c,Date_Of_Birth__c,Date_Of_Joining__c,Date_of_Leaving__c,Thumbnail__c FROM Employee_Master__c where Active__c=true and Photo__c !=null];
    }
    
    Public pageReference exportReport()
    {
        pageReference newPage;
        String Br='EmpMasterReport';
        if(datename==null)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please select the Date');
            ApexPages.addMessage(myMsg);
            
        }else
        {
            newPage = new pageReference('/apex/EmpMasterReport');
            Blob pdfpageblob;
            pdfpageblob = newpage.getcontentAsPdf();
            newPage.setRedirect(true);
        }
        
        return newPage ;
    }//
   
}

Plase help me to find my mistake.
bob_buzzardbob_buzzard
You aren't doing anything with the blog, you are just redirecting the user to the new page.  You need to store the results somewhere after doing the getcontentaspdf call.
Supriyo Ghosh 5Supriyo Ghosh 5
I have written like below but still not working.

Public pageReference exportReport()
    {
        pageReference newPage;
        String Br='EmpMasterReport';
        if(datename==null)
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please select the Date');
            ApexPages.addMessage(myMsg);
            
        }else
        {
            newPage = new pageReference('/apex/EmpMasterReport');
            Blob pdfpageblob;
            pdfpageblob = newpage.getcontentAsPdf();
            
            Attachment a = new Attachment();
            a.Body = pdfpageblob;
            insert a;
        }
        
        return newPage ;
    }//
bob_buzzardbob_buzzard
You haven't associated your attachment with anything - you need to set the parentid field to the id of the record you want to relate the attachment to.