Saturday, December 1, 2018

Lightning: Generate PDF within Lightning Experience with Salesforce Data

Some time back I posted a solution to generate PDF from Lightning components using in-memory data.
Post url: http://www.vermanshul.com/2017/07/lightning-generate-pdf-from-lightning.html

It was developed for a specific scenario, wherein we need to generate PDF where:
  1. User interface is Salesforce classic
  2. Initiated via Lightning Component
  3. Data doesn't exist within Salesforce and is completely in-memory
As complex and tricky this situation was, we did end up finding a stable and equally tricky solution.

However, I realize that there are still lack of solutions (or maybe my search skills are downgrading) to generate and automatically download PDF document from Lightning Experience, without using any lightning components, wherein data exists within Salesforce. You can use the earlier solution in that case, but it will be an overkill.

There are various solutions available to generate PDF from javascript. But, I still think the plain old method of converting HTML to PDF (via visualforce PDF generation utility provided by Salesforce) is a better solution, due to sheer control over the end result.

So, I ended up creating another quick solution to generate PDF documents from Salesforce.

Generate PDF within Lightning Experience with Salesforce Data


This simple solution entails adding a custom action to an object and embedding the button within object view.

 
Steps:
  1. Create a visualforce page with page attribute renderas = "pdf" (sample code below)
  2. Add a Quick Action "Generate PDF" for required object:
    • Action Type: Visualforce
    • Visualforce Page: GeneratePDF.page
  3. Go to Lightning App builder to add the action to detail view of Object

Code:

Automatically download PDF from Lightning Experience

Now, the above solution does allow you to generate PDF. However, it's not a good user experience. Wouldn't it be better, if the PDF is automatically downloaded with a defined name.


Steps:
  1. Create new page to initiate PDF page download (code below)
  2. Modify quick action "Generate PDF" created in previous step and set Visualforce page to DownloadPDF.page
That's IT!!

Code:





Popular Posts