Generate PDF from Apex Salesforce

by Rijwan Mohmmed

Hello friends, today we are going to discuss Generate PDF from Apex class in Salesforce. In this article, we will first generate a pdf in the apex class with the help of the Visualforce page and then send this pdf as an attachment in the email notification.

Many times we face this type of issue that we need to create a pdf order bill and send this to email, we do confuse that how can we get pdf in apex. Because we all know we can generate CSV files easily in apex.


Check out this for generating CSV: create or generate a csv file in Apex Salesforce

Highlights Points :

  1. Create a pdf by using Visalforce Page (renderas pdf)
  2. Call this Visalforce page from apex class to get pdf
  3. Insert this pdf to salesforce object record related list
  4. Send email with attachment

Step 1: In this step, we create a LWC Component with a button name “Generate PDF And Send Email”. So on clicking this button, we can send an email with an attachment. For records, I create a Picklist of contacts so we can select and send emails.

GeneratePdfAndSendEmail.Html :

    <div class="slds-tabs_card">
		<div class="slds-page-header">
			<div class="slds-page-header__row">
				<div class="slds-page-header__col-title">
					<div class="slds-media">
						<div class="slds-media__figure">
							<span class="slds-icon_container slds-icon-standard-opportunity">
								 <lightning-icon icon-name="standard:recipe" alternative-text="recipe" title="recipe"></lightning-icon>
						<div class="slds-media__body">
							<div class="slds-page-header__name">
								<div class="slds-page-header__name-title">
										<span>Generate PDF from Apex Salesforce</span>
										<span class="slds-page-header__title slds-truncate" title="Recently Viewed">TechDicer</span>
	</div> <br/>
    <!-- Start Card for Combobox -->
    <lightning-card  variant="Narrow"  title="Generate PDF from Apex Salesforce" icon-name="standard:account">
        <div class="slds-p-horizontal_medium">
            <lightning-combobox name="types" 
			<lightning-button variant="brand" label="Generate PDF And Send Email" 
                              title="Primary action" 

GeneratePdfAndSendEmail.JS :

import {LightningElement, track, wire} from 'lwc';
import fetchRecords from "@salesforce/apex/PDFGenerateCtrl.fetchRecords";
import sendPdf from "@salesforce/apex/PDFGenerateCtrl.sendPdf";
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class GeneratePdfAndSendEmail extends LightningElement {
    @track l_All_Types;
    @track typeOptions;
    @track selectedOption;

    @wire(fetchRecords, {})
    wireData({error, data}) {
        if (data) {
            try {
                this.l_All_Types = data; 
                let options = [];
                for (var key in data) {
                    // Here key will have index of list of records starting from 0,1,2,....
                    options.push({label: data[key].Name, value: data[key].Id });
                this.typeOptions = options; 
            } catch (error) {
                console.error('check error here', error);
        } else if (error) {
            console.error('check error here', error);
        this.selectedOption =; 

        sendPdf({contactId : this.selectedOption})
            this.ShowToast('Success', res, 'success', 'dismissable');
            this.ShowToast('Error', 'Error in send email!!', 'error', 'dismissable');

    ShowToast(title, message, variant, mode){
        const evt = new ShowToastEvent({
            title: title,
            variant: variant,
            mode: mode

Step 2: In this step, we create an apex class where we write the logic (pdf.getContent()) for generate a pdf and send emails.

PDFGenerateCtrl.cls :

public class PDFGenerateCtrl {
    public Contact con{get;set;}
    public String currentRecordId{get;set;}

    public PDFGenerateCtrl(){
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        if(currentRecordId != null && String.isNotBlank(currentRecordId)){
            con = [SELECT Id, Name, Email FROM Contact WHERE Id =:currentRecordId];

    public static List<Contact> fetchRecords(){
        try {
            List<Contact> conList = new List<Contact>();

            conList = [Select id, Name, Email from Contact];              
            return conList;
        } catch (Exception e) {
            System.debug('Exception: '+e.getMessage());
            return null;

    public static String sendPdf(String contactId) {
        PageReference pdf = new pagereference('/apex/ContactDataPDF?id='+contactId);
        pdf.getParameters().put('id', contactId);
        Blob body;
        try {
            // returns page as a PDF
            body = pdf.getContent();
        } catch (Exception e) {
            body = Blob.valueOf('data');

        //insert content version
        ContentVersion CV = new ContentVersion();
        CV.Title = 'TechdicerContact.pdf';
        CV.PathOnClient = 'TechdicerContact.pdf';
        CV.VersionData = body;
        CV.FirstPublishLocationId = contactId;
        insert CV;
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.Body = body;

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {''});
        mail.setSubject('Generate PDF from Apex Salesforce');
        mail.setHtmlBody('Generate PDF from Apex Salesforce');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attach}); 
        // Send the email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        return 'Email sent Successfully with PDF.';

Step 3: In this step, we create a Visualforce page that is reneder-as pdf and put some info in this.


<apex:page Controller="PDFGenerateCtrl" renderAs="pdf">
  <h1>Contact Data</h1>
  <p><b>Name :</b> {!con.Name}</p>  
  <p><b>Email :</b> {!con.Email}</p> 

Output :

What’s your Reaction?

You may also like


steve May 4, 2023 - 12:19 pm

Everyone shows how to do this, no one shows a passing test class lol.

Rijwan Mohmmed May 4, 2023 - 1:16 pm

it’s really simple.

esachait October 12, 2023 - 10:21 am

Is this code really works??

Rijwan Mohmmed October 13, 2023 - 4:34 pm



Leave a Comment