0

I have created custom Controller extension and VF page for PDF generation:

public with sharing class AccountPDFController {

   public String fileName {get;set;}    
   public AccountPDFController(ApexPages.StandardController controller)
   {
      Account acc = (Account)controller.getRecord();
      String accNum = acc.AccountNumber;
      String fileName = 'Account_' + accNum + '.pdf';
   }

   public PageReference printpdf()
   {

      Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename='+ fileName+'.pdf');

        return null;
    }  

}

and VF Page:

<apex:page standardController="Account" extensions="AccountPDFController" >
    <apex:form >
        <apex:commandButton value="PrintPDF" action="{!printpdf}"/>
    </apex:form>
</apex:page>

PDF is creating and downloading fine, but it is not opening. I guess because of not data. How can I put one account record data in it?

1

You are on the right track but there are some amendments that need to be made on how you are designing the pdf file. You should begin with the concept of converting the Visualforce page to pdf and this can be done using the render attribute to the <apex:page> component and specifying pdf as the rendering service.

<apex:page renderAs="pdf">

Lets start with the VF page.

You will need to desing your page using some styling techinques. The manner in which you display your page will reflect your created pdf. Visualforce pages rendered as PDFs will either display in the browser or download as a PDF file, depending on your browser settings.

Here is an example of a VF page displaying the Account Name and Account Number. Again, consider how your page is going to look as this is an example.

<apex:page standardController="Account" extensions="AccountPDFController" renderAs="pdf">
    <head>
        <style> 
            body { font-family: 'Arial Unicode MS'; }
            .companyName { font: bold 30px; color: red; }  
        </style>
    </head>
    <body>
        <center>
            <h1>{!account.Name}</h1>
        </center>
        <br /> Account Number: {!account.AccountNumber}
    </body>
</apex:page> 

The data you want displayed for the record will be obtained through your extension controller AccountPDFController. I have modified your controller to query the data for a single Account and also build the pdf file name. Take a look.

public class AccountPDFController {
    public String fileName {get;set;}
    public Account account {get;set;}
    public Id accountId {get;set;}

    public AccountPDFController(ApexPages.StandardController controller) {
        init(controller.getId());
    }

    private void init(Id selectedItemId) {
        if (selectedItemId != null) {
            accountId = selectedItemId;
            populateAccountFields();
            buildFilename();
        }
    }

    // Retrieve all the Account related data   
    public void populateAccountFields() {
        String accSoql = ''
            + 'select'
            + ' Name, AccountNumber'
            + ' from Account where Id = :accountId';
        Account[] accounts = (Account[]) Database.query(accSoql);

        if (accounts.size() > 0) {
            // There will only be one Account returned   
            account = accounts[0];
        }
    } 

    private void buildFilename() {
        fileName = 'Account - ' + account.AccountNumber + '.pdf';
        Apexpages.currentPage().getHeaders().put('content-disposition', 'inline; filename=' + fileName);
    }
}

You can extend this dynaminc query to obtain the results your business need requests.

Also, please feel free to make any imporvements to the above answer.

| improve this answer | |
  • OK , so If i want to generate pdf on Custom button click , I need to fire buildFilename on action event , right ? – neeraj Mar 15 '17 at 14:29
  • No, the init method will cover all that for you. Set your custom link up to point at your VG page and let Salesforce take care of the rest :) – TSmith Mar 15 '17 at 14:36
  • ok , I am working on it , will get back to you – neeraj Mar 15 '17 at 14:40
  • render as pdf , will generate a pdf , then where should a set button and press it on page ? i want to do download action on button click – neeraj Mar 15 '17 at 14:44
  • i was trying to achieve something like this <apex:page standardController="Account" extensions="AccountPDFController" > <apex:form > <apex:commandButton value="PrintPDF" action="{!buildFilename}"/> </apex:form> </apex:page> – neeraj Mar 15 '17 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.