Skip to main content

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:





Comments

  1. I can't see the source code. Can you share it with me please?

    ReplyDelete
    Replies
    1. You can find code on github (link below)
      https://gist.github.com/toanshulverma/efda6b9fab1ae900b11da97218a80961

      Delete
  2. Hi Anshul, great post! Do you have any idea why the default "Save" button is disabled/how to enable it? I see it is in your screenshots as well. We'd prefer for our users to save rather than automatically download. Thanks very much!

    ReplyDelete
    Replies
    1. Hi Ro, that will need customization to code. The code sample shared eas designed to automatically open or download the pdf.

      Delete

Post a Comment

Popular posts from this blog

Quick Tips: Salesforce default Images

Well, I'm sure a lot of you still rely on using out of the box salesforce images for displaying quick icons within formula fields or even using them within your Visualforce pages. Lately, I realized that a lot of earlier resources are no longer accessible, so I tried to quickly extract all images from Salesforce CSS files and provide a quick reference here. Please note, I've referenced all images from SF servers directly, so if anything changes, the image should stop rendering here. As these images are completely controlled by Salesforce, and in case they change anything, it might lead to image not being accessible. Image path Image /img/samples/flag_green.gif /img/samples/flag_green.gif /img/samples/flag_red.gif /img/samples/color_red.gif /img/samples/color_yellow.gif /img/samples/color_green.gif /img/samples/light_green.gif /img/samples/light_yellow.gif /img/samples/light_red.gif /img/samples/stars_100.gif /img/samples/stars_200.gif /img/samples/stars_300.

Lightning: Generate PDF from Lightning components with in-memory data

I'm sure as everyone is diving into lightning components development, they are getting acquainted with the nuances of the Lightning components framework. As well as, its current limitations. Being a new framework, this is bound to happen. Although we have our users still using salesforce classic, we have started using lightning components framework our primary development platform and Visualforce is considered primarily for rendering lightning components within Classic Service console. Recently, while re-architecting a critical module, we encountered a problem wherein we needed to generate PDF from lightning components. Now, being Javascript intensive framework, it has limited room for such features (may be included in future roadmap). As of now, there is no native feature within the lightning framework to do so (at least I didn't find anything). Common Scenario - Create Visualforce page to retrieve data and generate PDF For scenarios where the data exist within Sa

Quick Tips: Setup SFDX Manually without Admin access

We all have faced challenges while working in different enterprise environments, where there may be lot of controls/ checks/ red-tape to get by. In such situations, getting access to simple tools (even git) can take lot of time. Note: This tutorial is to be followed at your own risk, as it may not be complaint to your organization's IT policies. What is SFDX? SFDX is a command line utility for managing salesforce builds/ deployments. Being command line, it can be easily embedded to automation chains, to help build fully automated build and deployment processes. To get started, refer  https://trailhead.salesforce.com/en/content/learn/trails/sfdx_get_started Setup SFDX on Windows machine without admin access As you may have already realized, SFDX installation needs admin access to one's machine. Which may be a luxury a lot of developers may not have. So, i tried to provide a step-by-step guide to setup SFDX on your computer without any admin access Steps: Note: