+ Start a Discussion
Arvind Singh 68Arvind Singh 68 

How I make pdf from vf page

Best Answer chosen by Arvind Singh 68
Akshay_DhimanAkshay_Dhiman
Hi Arvind

Please try the below code:
<apex:page standardController="Account" extensions="ControllerPDFAccountPage" renderAs="pdf">
    <apex:form >
    <apex:pageBlock mode="maindetail">
            <apex:pageBlockSection columns="3" >
                <apex:pageBlockSectionItem >
                    {!$Organization.Name} <br/>
                    {!$Organization.Street} <br/>
                    {!$Organization.City} <br/>
                    {!$Organization.State} <br/>
                    {!$Organization.PostalCode} <br/>
                    {!$Organization.Country} <br/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <h1 style="width:400px;">
                        
                    </h1>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                      <apex:image url="{!URLFOR($Resource.cloud, 'cloud.jpg')}" width="200"  />
                </apex:pageBlockSectionItem>
              
        </apex:pageBlockSection>
        <hr/>
        <apex:pageBlockSection columns="3">
                <apex:pageBlockSectionItem dataStyle="color:red;" >
           <!--      Account Name :&nbsp; &nbsp; -->  <apex:outputField value="{!accObj.name}" /> &nbsp; &nbsp;
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <h1 style="width:400px;">
                        
                    </h1>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem labelStyle="float:right;" >
                    {!accObj.BillingCity  }<br/>
                    {!accObj.BillingState}<br/>
                    {!accObj.BillingPostalCode}<br/>
                    {!accObj.BillingCountry}
                </apex:pageBlockSectionItem>
                <br/><br/>
            </apex:pageBlockSection>
       
            <apex:pageBlockTable value="{!conList}" var="con" border="1" >
               <apex:column value="{!con.Name}" style="width:300px;"/>
                <apex:column value="{!con.Email}" style="width:300px;"/>
                <apex:column value="{!con.Phone}" style="width:300px;"/>
        </apex:pageBlockTable>
    
    </apex:pageBlock>
        </apex:form>
    
</apex:page>

*******************Controller************************************
public class ControllerPDFAccountPage {
    public Account accObj{get;set;}
    public List<Contact> conList{get;set;}
    public ControllerPDFAccountPage(ApexPages.StandardController controller) {
        conList = new List<Contact>();
        conList = [SELECT Name,
                   Email,
                   Phone
                   FROM Contact 
                 WHERE AccountId =:ApexPages.currentPage().getParameters().get('id')];
        accObj = new Account();
        accObj = [SELECT Name,
                  BillingStreet,
                  BillingCity,
                  BillingState,
                  BillingPostalCode,
                  BillingCountry
                  FROM Account
                 WHERE id =: ApexPages.currentPage().getParameters().get('id')];
    }

}




Please mark as best answer if it helps you.

Thank You
Akshay



 

All Answers

Raj VakatiRaj Vakati

You can render any page as a PDF by adding the renderAs attribute to the <apex:page> component, and specifying “pdf” as the rendering service. For example:
 
<apex:page renderAs="pdf">

Example
 
<apex:page standardController="Account" renderAs="pdf" applyBodyTag="false">
    <head>
        <style> 
            body { font-family: 'Arial Unicode MS'; }
            .companyName { font: bold 30px; color: red; }  
        </style>
    </head>
    <body>
        <center>
        <h1>New Account Name!</h1>
     
        <apex:panelGrid columns="1" width="100%">
            <apex:outputText value="{!account.Name}" styleClass="companyName"/>
            <apex:outputText value="{!NOW()}"></apex:outputText>
        </apex:panelGrid>
        </center>
    </body>
</apex:page>

Niraj Kr SinghNiraj Kr Singh
Hi Arvind,
You can use renderAs in your apex tag.
Ex:
RenderAs: providing extension of page to create output like pdf, doc, excel.

<apex:page standardController=”Account” renderAs=”pdf”>
<apex:pageBlock >
<apex:outputField value=”{!Account.name}”/>
<apex:outputField value=”{!Account.AccountNumber}”/>
</apex:pageBlock>
</apex:page>

Try this and mark ur ans if it works for you.

Thanks
Niraj
Akshay_DhimanAkshay_Dhiman
Hi Arvind

Please try the below code:
<apex:page standardController="Account" extensions="ControllerPDFAccountPage" renderAs="pdf">
    <apex:form >
    <apex:pageBlock mode="maindetail">
            <apex:pageBlockSection columns="3" >
                <apex:pageBlockSectionItem >
                    {!$Organization.Name} <br/>
                    {!$Organization.Street} <br/>
                    {!$Organization.City} <br/>
                    {!$Organization.State} <br/>
                    {!$Organization.PostalCode} <br/>
                    {!$Organization.Country} <br/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <h1 style="width:400px;">
                        
                    </h1>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                      <apex:image url="{!URLFOR($Resource.cloud, 'cloud.jpg')}" width="200"  />
                </apex:pageBlockSectionItem>
              
        </apex:pageBlockSection>
        <hr/>
        <apex:pageBlockSection columns="3">
                <apex:pageBlockSectionItem dataStyle="color:red;" >
           <!--      Account Name :&nbsp; &nbsp; -->  <apex:outputField value="{!accObj.name}" /> &nbsp; &nbsp;
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <h1 style="width:400px;">
                        
                    </h1>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem labelStyle="float:right;" >
                    {!accObj.BillingCity  }<br/>
                    {!accObj.BillingState}<br/>
                    {!accObj.BillingPostalCode}<br/>
                    {!accObj.BillingCountry}
                </apex:pageBlockSectionItem>
                <br/><br/>
            </apex:pageBlockSection>
       
            <apex:pageBlockTable value="{!conList}" var="con" border="1" >
               <apex:column value="{!con.Name}" style="width:300px;"/>
                <apex:column value="{!con.Email}" style="width:300px;"/>
                <apex:column value="{!con.Phone}" style="width:300px;"/>
        </apex:pageBlockTable>
    
    </apex:pageBlock>
        </apex:form>
    
</apex:page>

*******************Controller************************************
public class ControllerPDFAccountPage {
    public Account accObj{get;set;}
    public List<Contact> conList{get;set;}
    public ControllerPDFAccountPage(ApexPages.StandardController controller) {
        conList = new List<Contact>();
        conList = [SELECT Name,
                   Email,
                   Phone
                   FROM Contact 
                 WHERE AccountId =:ApexPages.currentPage().getParameters().get('id')];
        accObj = new Account();
        accObj = [SELECT Name,
                  BillingStreet,
                  BillingCity,
                  BillingState,
                  BillingPostalCode,
                  BillingCountry
                  FROM Account
                 WHERE id =: ApexPages.currentPage().getParameters().get('id')];
    }

}




Please mark as best answer if it helps you.

Thank You
Akshay



 
This was selected as the best answer