Generate PDF in Salesforce without any app (2022)
Source of post: Generate PDF in Salesforce without any app
In every business generating PDF files from invoices, bills, articles, reports, etc. is necessary. As a PDF (portable document file), we can keep it offline for later consumption instead of viewing the same detail using the network.
In this post, we will explain how to Generate PDF in Salesforce on button click. In this implementation, we will show Generate PDF button in Salesforce on the Account details page. After clicking a button, a PDF will be generated.
Steps to generate pdf in Salesforce
- Create a Static Resource
- Create a VF page
- Create a button to Generate PDF
What you need before start
- Your Company Logo
- Working Salesforce Org
Let’s Start to generating pdf in Salesforce
Create a Static Resource for your company logo:
To create a static resource:
- From Setup, enter Static Resources in the Quick Find box, then select Static Resources.
- Click New.
- In the Name text box, enter the text “companyLogo”.
- Next to the File text box, click Browse to navigate to a local copy of your Company Logo.
- Set the Cache Control to public.
- Click Save.
Create a Detail Page Button on Account to generate PDF:
To Create a Button on Account
- From Setup, click Object Manager, then click Account.
- Click Buttons, Links, and Actions, then click New Button or Link.
- Name the button
"Generate PDF"
. - Select Detail Page Button.
- Set Content Source “Visualforce Page”
- Select content “MyFirstPDF”
- Click Save, then click OK. Now add it to the Account page layout.
- Click Page Layouts, then click Account Layout.
- From the Buttons category in the palette, drag “
**Generate PDF**
" into the Custom Buttons area on the page layout. - Click Save. OK! Now let’s test it.
- From the App Launcher, find and select Sales, then click the Accounts tab.
- Open an account record.
- In the highlights panel, not only do you see the fields from the object’s compact layout, but you also see an actions menu. The actions menu is a combination of the standard buttons, custom buttons, and actions from the page layout.
- Expand the actions menu, and select Generate PDF. The browser opens a new window or tab that shows you the PDF.
Conclusion
Generating a PDF within Salesforce can be done with VisualForce and Apex. It is very easy (even for those with very little VisualForce and Apex experience). You are able to generate any Salesforce page as a PDF by adding the renderAs attribute to the component, and specifying “pdf” as the rendering service.
We first need to create a Visualforce page that will have the PDF Content. Below is the simple Visualforce page that will display Account details with the help of the Account Standard Controller.