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
Hari Krishnan 105Hari Krishnan 105 

I have a requirement that On click of a button from Account record page need to generate a pdf with all related contacts of that account.

SwethaSwetha (Salesforce Developers) 
HI Hari,

To generate a PDF with all related contacts of an Account record, you can create a Visualforce page with a controller that queries the related contacts and renders the output as a PDF. Then, you can add a custom button to the Account record page that navigates to the Visualforce page and generates the PDF.

Try below code

VF Page:
<apex:page controller="AccountController" renderAs="pdf">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!contacts}" var="c">
            <apex:column value="{!c.Name}"/>
            <apex:column value="{!c.Phone}"/>
            <apex:column value="{!c.Email}"/>

Controller class
public with sharing class AccountController {
    public List<Contact> contacts { get; set; }
    public Account account { get; set; }

    public AccountController() {
        // get the current Account record
        account = [SELECT Id, Name FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

        // query the related contacts of the Account record
        contacts = [SELECT Id, Name, Phone, Email FROM Contact WHERE AccountId = :account.Id];
Now Go to the Account object in Salesforce Setup> click on "Buttons, Links, and Actions"> Create a new custom button, and select "Visualforce Page" as the content source.

Choose the above created VF page you created and set the behavior to "Display in existing window without sidebar or header".

If this information helps, please mark the answer as best. Thank you