Creating PDF with Charts in Salesforce

Salesforce, as a technology platform, has come a long way in the past decade or so, incorporating the latest technology trends eventually but surely. Be it moving to Lightning Experience or adopting the ES6 compliant Lightning Web Components, Salesforce has tried to keep the developer experience in mind as well while reshaping the customer experience.

But one problem that has remained constant over the years is the ability to generate PDFs, especially if there is a need to incorporate charts in them. Imagine a requirement where utilization graphs have to be embedded in an invoice PDF before emailing it to a client or a bar chart showing comparison between the subscribed quantities from last year and this year have to be included in a PDF.

As developers, all of us would have faced this one time or the other and would have scoured the ends of the internet to find native Salesforce solutions to achieve this seemingly innocuous requirement, just to discover this immense white space – there is no native solution to do this!

So, in case you are dealing with the same conundrum, here are the top approaches that might help you this festive season:

Approach 1: Generate and Save Visualforce Charts as an image

This approach utilizes native Visualforce Charts to generate charts. However, since these cannot directly be embedded into a Visualforce page that needs to be rendered as a PDF document, the Visualforce Charts first need to be saved as an image using a third-party JavaScript library like canvg. These chart images can then be included in the Visualforce page targeted to be rendered as a PDF document.

Sounds simple, right? The downsides do not make it so simple. The PDF generation becomes a two-step process since all the charts have to be rendered on the UI before they can be saved and embedded in the PDF. It also utilizes File storage to save the chart images which then need to be cleaned up later. And, since the charts are converted into a base64 encoded string value before being saved, there is a chance of hitting the string character limit if the chart is complicated enough.

Approach 2: Using Chart-to-Image libraries / tools

Online tools like Google Image Charts, Image-Charts or QuickChart provide GET / POST based solutions which provide chart images in response to chart parameters and data provided as part of the request. These images can then be inserted into the Visualforce page rendered as a PDF.

If you are worried about your data leaving the secure space of Salesforce, this approach will definitely give your security team a nightmare. Also, there are compatibility issues, licensing requirements etc. that need to be considered based on the solution of choice.

Approach 3: Multiple JavaScript libraries

JavaScript has grown by leaps and bounds in recent times and almost any programmatic task can be achieved by a mixture of libraries and frameworks. With a combination of chart.js, dom-to-image and jsPDF libraries included in a Visualforce page, the requirement can be achieved with minimal hiccups. Though I have not done it myself, but I am pretty sure this will work with LWCs and Lightning Web Charting Component (LWCC) as well.

Again, for companies that are particular about security, utilizing three or more JavaScript libraries is definitely something horrendous, but this approach is what makes most sense looking at the current technology landscape, governor limits and other constraints.

Approach 4: AppExchange Solutions

Though there are multiple AppExchange solutions available for document generation, it is not readily evident if all of them support inclusion of charts in the generated documents. One product that does support charting is Conga Composer with the Conga Grid add-on. I mean this was expected – Conga is one of the leaders in the space; it would have been a surprise if they didn’t support this.

However, buying and setting up something like Conga is a costly and time-consuming affair. So, a clear roadmap and utilization scope for such a solution needs to be considered before diving head-first into the procurement process.

 

This list is certainly not exhaustive. There will be more brilliant solutions developed by the ever-effervescent members of the Salesforce Ohana. Do feel free to share about your experience with PDF generation and how you were able to achieve it. Hopefully, this will be addressed by a more native capability within the platform in the times to come.

 

References

https://salesforce.stackexchange.com/questions/56633/image-charts-in-visualforce-pdf

https://developers.google.com/chart/image

https://www.image-charts.com/

https://quickchart.io/documentation/salesforce-visualforce-apex-charts/

https://csrviswa.medium.com/salesforce-lightning-how-to-generate-pdf-with-charts-and-images-f10f5dfef45e

https://conga.com/products/business-process-automation/conga-grid

 

Author Details

Pratyush Anand

Pratyush is a Salesforce Techno-Solution Architect with the Enterprise Cloud Application Services (Salesforce) Unit at Infosys. He has more than 13 years of experience in designing and developing Salesforce applications, with a keen eye for innovation, optimization and efficiency within the platform as well as in the business processes. He has worked in multiple domains, right from banking, insurance to manufacturing and telecommunications. He also has a knack for ideating, designing, developing and documenting reusable assets and bots to aid in the development and implementation of projects. Pratyush has an overwhelming love for the literary arts, and is an author, blogger and poet when not designing disruptive tech solutions.

Leave a Comment

Your email address will not be published. Required fields are marked *