Publishing Salesforce Reports with PDF templates

In under 100 lines of code.

Arun V
Salesforce Analytics
2 min readMar 19, 2014

--

When was the last time you wanted to export your Salesforce Report to PDF? Look no further; in this blog we will explore how you can use the new Analytics API in APEX with Visualforce templates to produce high-quality documents with customized layouts.

Design the Template

In the example above , the goal is to take a simple Accounts report that is in summary format and turn into a pixel-perfect custom document with company logo and custom title. Once the design is done, make sure all the assets are uploaded as “Static Resources”. Now go ahead and prototype the sample pdf page using Visualforce. Look at the example here: https://github.com/arun-sfdc/Analytics-API/blob/master/ReportPDF/reportPDF.page

In addition to custom layout, we also include custom formatting, all opportunities over 100K are highlighted with a red background. This is something we need to implement using our model & controller.

Setup the backend and report

Once the Visualforce page is setup, all you need is a simple controller that passes in some sample data. With this step complete, you should be able to produce a PDF that looks very much like your design. In this example here: https://github.com/arun-sfdc/Analytics-API/blob/master/ReportPDF/ReportPDFController.cls , we have two controller methods — one to generate the table headers and the other to generate the data. The end result will be set of pages — one page for each account. Each of these pages will include a table containing the Key Metrics or KPIs on each of the open opportunities for that account.

Hook-up the Analytics API

The last step is to plug-in the Analytics API so you are feeding actual data into your backend. Here in this example we have 3 APEX class — the first two form the standard Visitor pattern and third class is the controller that generates the content. The main reason for using the Visitor pattern is to build different templating examples while reusing most of the report parsing code.

Go ahead, give it a spin and let me know how it goes!

Code on GitHub: https://github.com/arun-sfdc/Analytics-API/tree/master/ReportPDF

--

--