Generate PDFs in Salesforce with Visualforce Pages

In Salesforce, generating PDFs allows users to create professional documents from their organization’s data, making it easier to share the data with others.

The feature of rendering a document in PDF is useful for reports, invoices, contracts, and other documents that need to be shared with customers.

In this Salesforce tutorial, we will learn how to generate PDFs in Salesforce using the Visualforce pages.

In this example, I will explain the entire process of generating the PDFs, which involves creating a Visualforce page and a Visualforce controller, and ultimately, viewing the generated PDF.

Use Salesforce Visualforce Pages to Generate PDF

In this example, we will generate a PDF for the lead records. Now, navigate to the Salesforce developer console and follow the steps below.

  1. Create a controller class, LeadPDFcontroller, that will handle the logic of the visualforce page. This controller will fetch the required lead records to populate the visualforce page.
public class LeadPDFController {
    public List<Lead> leads { get; private set; }

    public LeadPDFController() {
        leads = [SELECT Name, Company, Email, Phone FROM Lead LIMIT 100];
    }
}
  1. Now, create the Visualforce page following the code below. Visualforce allows us to create a page with a PDF rendering. The key is to use the renderAs=”pdf” attribute.
<apex:page controller="LeadPDFController" renderAs="pdf" showHeader="false">
    
    <h1>Lead Report</h1>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Company</th>
                <th>Email</th>
                <th>Phone</th>
            </tr>
        </thead>
        <tbody>
            <apex:repeat value="{!leads}" var="lead">
                <tr>
                    <td>{!lead.Name}</td>
                    <td>{!lead.Company}</td>
                    <td>{!lead.Email}</td>
                    <td>{!lead.Phone}</td>
                </tr>
            </apex:repeat>
        </tbody>
    </table>
</apex:page>

In the PDF, we will display the record details in the form of a table, so inside the table component, we have inserted the values Name, Company, Email, and Phone.

  1. After creating the Visualforce page, we will see the preview. To do this, navigate to Setup > Visualforce Pages, open the PDF Visualforce page, and click the Preview button.
Render a Visualforce page as pdf in Salesforce

In the preview, you will see that the visualforce page is rendered in a PDF format.

Create a PDF using Visualforce page in Salesforce

As we can see, the page is rendered in a PDF format, and from here, we can also download or save the PDF.

Customize the PDF Page Layout in Salesforce Visualforce

When creating PDFs using Visualforce pages, the default output style is a simple black-and-white design. But we can customize the style to make it more organized.

We have two options to add CSS to the Visualforce page: either we can add CSS directly to the page or link it through the static resource.

In this example, I will create a static resource to use the styles for other pages.

  1. To create a static resource, navigate to the developer console and create a new file by clicking File > New > Static Resource.
    • Enter the name for the static file, select the MIME type as text/CSS, and click on the Submit button.
    • After that, enter the CSS style format mentioned below.
body {
	background-color: #f5f5f5;
	font-family: Arial, sans-serif;
	font-size: 12px;
}
h1 {
	color: #006699;
	font-size: 18px;
	font-weight: bold;
	margin-bottom: 10px;
}
table {
	border-collapse: collapse;
	margin-bottom: 20px;
	width: 100%;
}
th,
td {
	border: 1px solid #ddd;
	padding: 8px;
	text-align: left;
}
th {
	background-color: #f2f2f2;
}
  1. To add the CSS style from the static resource to the PDF, link the CSS file to the Visualforce page by adding the following code after the opening <apex: page>.
<apex:stylesheet value="{!$Resource.<staticfilename>}" />

After making the changes to the Visualforce page, save it and reopen the Visualforce page in the preview.

Use Salesforce Visualforce pages to create PDF

This way, we can generate PDFs in Salesforce using the Visualforce pages and apply style to it using a static resource.

Limitations of PDF generation in Salesforce

There are some limitations to generating the PDFs using the Salesforce Visualforce pages.

  1. Non-compatible with Complex PDFs: Visualforce pages can easily render the PDF in a basic format, such as displaying records under the value column. It is incompatible with PDFs and complex structures, such as tables, charts, and images.
  2. Limited styling features: Visualforce pages use an older HTML rendering, which doesn’t support modern CSS or JavaScript. JavaScript execution is generally disabled when generating a PDF, and as a result, the applied designs, such as a grid, may not appear or function as expected.
  3. Limited Support for Features and Fonts: Interactive elements, such as dropdowns, buttons, or links, are not supported in all PDFs generated through Visualforce pages.
  4. Large data sets: Visualforce pages may become challenging to navigate when working with large data sets. To avoid this, consider using alternative options, such as batching or paginating the data, when creating PDFs from large datasets.
  5. Use of Components: Don’t use standard components that aren’t easily formatted for print, or form elements such as inputs or buttons, or any component that requires JavaScript to be formatted.
  6. Size of PDF: The maximum response size when creating a PDF file must be less than 15 MB before being rendered as a PDF file. This limit is the standard limit for all Visualforce requests. The maximum file size for a generated PDF file is 60 MB, and the maximum total size of all images included in a generated PDF is 30 MB.

Conclusion

In this Salesforce tutorial, we learned how to generate PDFs using the Salesforce Visualforce pages. Following the above method, you can easily create a PDF for any object in your Salesforce org.

We have also learned to add CSS style to the rendered PDF, and with this, you can create shareable PDF documents such as reports, invoices, and contracts.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.