Blog Post

How to Generate a PDF in Salesforce

Illustration: How to Generate a PDF in Salesforce

In this tutorial, you’ll learn how to generate a PDF in Salesforce from a contact record. You’ll start by creating a Visualforce page that generates a PDF from a contact record in Salesforce. You’ll then use an Apex controller to retrieve the data, and a PDF rendering engine to generate the output.

Please note that contact records are the data source for this tutorial because they provide an easy-to-understand example of how you can generate PDFs using Salesforce. However, the techniques used in this article can also be applied to other types of records and data sources in Salesforce. You can use the same principles to generate and send reports, invoices, contracts, or other common document types.

About PSPDFKit for Salesforce

If you work with PDF documents in Salesforce, PSPDFKit provides a client-side JavaScript library that can be integrated into a Lightning web component to enable opening, editing, and saving PDFs from a web browser. Some of our most popular features include the ability to:

  • View and edit PDF content directly in Salesforce

  • Collaborate on documents by highlighting text and adding notes or stamps

  • Organize documents by adding, deleting, rotating, or reordering pages

  • Programmatically fill PDF forms with information from Salesforce

  • Add an eSignature by drawing, typing, or uploading an image of your signature

  • Permanently redact sensitive information like email addresses, SSNs, phone numbers, and more

What Is a Visualforce Page?

Visualforce pages are HTML pages that can be customized to display specific Salesforce data. You can create a Visualforce page that will display your data in a table or a chart and then convert that page into a PDF.

To create a Visualforce page, you’ll need to have a basic understanding of HTML, CSS, and Apex code. You’ll also need to create a custom controller that will allow you to access the data you want to display on the page.

What Is Apex?

Apex is a programming language used to write custom logic in Salesforce. You can use Apex code to create a PDF by writing code that will extract the data you need and format it into a PDF file.


  • A Salesforce developer account or a sandbox with the necessary permissions to create and modify Visualforce pages and Apex classes.

  • Specifically, you’ll need the following permissions:

If you’re using a Salesforce developer account, you’ll have these permissions by default. However, if you’re using a sandbox or another type of organization, you may need to work with your Salesforce administrator to ensure you have the necessary permissions.

Step 1: Setting Up the Project

The first step is to create a new Apex class and Visualforce page in your Salesforce organization.

  1. Once you’ve set up your Salesforce account, log in to your organization.

  2. Open the Developer Console by going to Setup > Developer Console.

Screenshot showing the Developer Console

  1. In the Developer Console, click File > New > Apex Class.

  2. In the new Apex class window, enter a name for your class, such as PDFController.

Copy and paste the following code into the class editor:

public class PDFController {

    public List<Contact> contacts { get; set; }

    public PDFController() {
        contacts = [SELECT Name, Account.Name, Phone FROM Contact];

This code defines an Apex controller class that retrieves a list of contact records from the Salesforce database and stores them in a property called contacts.

  1. Save the Apex class by clicking File > Save.

  2. Now, create a new Visualforce page by clicking File > New > Visualforce Page.

  3. In the new Visualforce page window, enter a name for your page, such as PDFPage.

Copy and paste the following code into the page editor:

<apex:page controller="PDFController" renderAs="pdf">
			<apex:pageBlockTable value="{!contacts}" var="contact">
				<apex:column value="{!contact.Name}" />
				<apex:column value="{!contact.Account.Name}" />
				<apex:column value="{!contact.Phone}" />

This code defines a Visualforce page that uses the Apex controller you just created to retrieve contact records and display them in a table. The renderAs attribute specifies that the page should be rendered as a PDF.

  1. Save the Visualforce page by clicking File > Save.

Step 2: Testing the PDF Generation

Now that you’ve created your Apex controller and Visualforce page, you can test the PDF generation functionality to make sure it works as expected.

  1. Open a new browser window or tab and navigate to your Setup Home. Here you’ll see the recently created Visualforce page. Click the page name, PDFPage, to open it.

  2. Click the Preview button.

Screenshot showing a preview of the PDF

  1. This will open a new browser tab and display a preview of the PDF. You can also use Salesforce APIs to retrieve the PDF programmatically.

The Visualforce page will load and display a table of contact records.

Screenshot showing preview of PDF

If you encounter any errors or issues, review the code and make sure you followed all the steps correctly. You can also check the debug logs in the Developer Console for any error messages or stack traces.

Step 3: Customizing the PDF Output

Now that you have working PDF generation functionality, you can customize the output to meet your specific requirements. The next section provides some examples of how to do this.

Customizing the PDF Styles

By default, the PDF output generated by Visualforce uses a simple black-and-white style. However, you can customize the styles by creating a new CSS file and linking it to the Visualforce page.

  1. In the Developer Console, create a new file by clicking File > New > Static Resource.

  2. Enter a name for your file, such as PDFStyles, and select the text/css MIME type from the dropdown menu.

Copy and paste the following CSS code into the file editor:

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%;

td {
	border: 1px solid #ddd;
	padding: 8px;
	text-align: left;

th {
	background-color: #f2f2f2;

.footer {
	margin-top: 50px;
	text-align: center;

This code defines a custom set of styles for the PDF output, including a custom background color, font family, font size, and table styles.

  1. Save the CSS file by clicking File > Save.

  2. Now, link the CSS file to the Visualforce page by adding the following code after the opening <apex:page> tag:

<apex:stylesheet value="{!$Resource.PDFStyles}" />

Linking the CSS file to the Visualforce page

This code links the Visualforce page to the CSS file you just created using a static resource reference.

  1. Save the Visualforce page by clicking File > Save.

Now, when you preview the Visualforce page, you’ll see the PDF output with the custom styles applied.

PDF with custom styles

Customizing the PDF Layout

In addition to customizing the styles, you can also customize the layout of the PDF output by modifying the Visualforce page. Here are some examples of how to do this:

  • Add headers and footers to the PDF by using the <apex:pageHeader> and <apex:pageFooter> tags.

  • Customize the page orientation and size by using the orientation and size attributes of the <apex:page> tag.

  • Add images, logos, or other branding elements to the PDF by using the <apex:image> tag.

By using Visualforce to generate PDFs in Salesforce, you have the flexibility to control the content and formatting of the PDFs. This can be useful for creating professional-looking documents and reports and for making information from Salesforce easily accessible to your users.

Limitations of PDF Generation in Salesforce

There are, however, several limitations of PDF generation in Salesforce that are important to consider:

  • Customizability — There aren’t a lot of customization options available when creating PDFs using Salesforce’s built-in PDF generation features. Although Visualforce offers a method for producing customized PDFs, the styling options are more limited than those offered by other tools, and the finished PDF might not be as polished or expert-looking.

  • Complex layouts can be challenging — Visualforce works best with straightforward, simple PDF layouts. If you need to create a complicated, multipage PDF with lots of tables, charts, and images, it might be challenging to do so using only Visualforce.

  • Large data sets — When working with large data sets, Visualforce pages may become sluggish and difficult to navigate. Consider alternative options, such as batching or paginating the data, if you need to create PDFs from very large data sets.

  • Limited API accessibility — Since the PDF rendering engine cannot be directly accessed through Salesforce’s APIs, creating PDFs from data stored there may be more challenging than in other settings.

  • Lack of support for specific fonts — If your Visualforce page or data uses one of these fonts, problems may arise because Salesforce’s PDF generation engine doesn’t support it.

  • Maintenance issues — As they get bigger and more complex, Visualforce pages can be complicated and challenging to maintain. It may also become more difficult to maintain Visualforce-based PDFs in the future as Salesforce continues to develop and deprecates older technologies in favor of newer ones.


Generating PDFs in Salesforce is a powerful feature that can help you create custom reports, invoices, and other documents. In this tutorial, you learned how to create a Visualforce page that generates a PDF from contact records in Salesforce, using an Apex controller to retrieve the data and a PDF rendering engine to generate the output.

You also learned how to customize the PDF styles and layout to meet your specific requirements. With this knowledge, you can apply the same principles to generate PDFs from other types of data in Salesforce and build powerful, custom reports for your organization.

We offer a Salesforce PDF library that can be used to add PDF viewing, annotation, and editing to your native Salesforce application. To get started with our Salesforce PDF library, you can contact our Sales team.

Hulya Karakaya Technical Writer

Hulya is a frontend web developer and technical writer at PSPDFKit who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
DESIGN  |  Baseline UI • Web

Part V — Mastering the Baseline UI Theme: An In-Depth Exploration

DESIGN  |  Baseline UI • Web

Part IV — Building Consistency: A Guide to Design Tokens in Baseline UI

DESIGN  |  Baseline UI • Web

Part III — Accessible UI Design: Building Inclusive Digital Experiences