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.
Prerequisites
-
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:
-
Customize Application permission — This permission is required to create Visualforce pages and Apex classes.
-
View All Data or Modify All Data permission — This permission is required to access or modify contact records in Salesforce. If you don’t have this permission, you may need to work with your Salesforce administrator to grant you the necessary access.
-
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.
-
Once you’ve set up your Salesforce account, log in to your organization.
-
Open the Developer Console by going to Setup > Developer Console.
-
In the Developer Console, click File > New > Apex Class.
-
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
.
-
Save the Apex class by clicking File > Save.
-
Now, create a new Visualforce page by clicking File > New > Visualforce Page.
-
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:form> <apex:pageBlock> <apex:pageBlockTable value="{!contacts}" var="contact"> <apex:column value="{!contact.Name}" /> <apex:column value="{!contact.Account.Name}" /> <apex:column value="{!contact.Phone}" /> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
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.
-
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.
-
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. -
Click the Preview button.
-
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.
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.
-
In the Developer Console, create a new file by clicking File > New > Static Resource.
-
Enter a name for your file, such as
PDFStyles
, and select thetext/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%; } th, 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.
-
Save the CSS file by clicking File > Save.
-
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}" />
This code links the Visualforce page to the CSS file you just created using a static resource reference.
-
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.
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.
Conclusion
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.
FAQ
How can I generate a PDF in Salesforce?
You can generate a PDF in Salesforce by creating a Visualforce page and using the renderAs="pdf"
attribute. This allows you to render the Visualforce page content as a PDF document.
What are the steps to create a PDF from a Salesforce record?
Create a Visualforce page that retrieves the data from the Salesforce record, format the content as desired, and set the renderAs="pdf"
attribute to generate the PDF when the page is accessed.
Can I customize the PDF layout and content in Salesforce?
Yes, you can customize the PDF layout and content using Visualforce components, HTML, and CSS. This allows you to create professional-looking PDFs with custom branding and formatting.
What are some common use cases for generating PDFs in Salesforce?
Common use cases include generating invoices, quotes, contracts, reports, and any other documents that need to be created from Salesforce data and shared with customers or internal stakeholders.
Are there any third-party tools to enhance PDF generation in Salesforce?
Yes, there are third-party tools like PSPDFKit that provide advanced PDF generation and customization features, making it easier to create complex and dynamic PDFs.