function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ian QuahIan Quah 

Generating XLS, PDF and CSV WITHOUT using apex:pages

1) Is it possible to do it without generating or "rendering" the apex pages?

Currently, I have code that does it in apex pages, where it takes in the apex page as an argument then collects the data before processing the blob. However, to do so I have to be ON the apex page before clicking the button to generate it.

Is it possible to "generate" the apex page (without actually making it show up on screen / being redirected to it) on a lightning page so that I can pass it as an argument to the pre-existing code?

I ask because I have a lightning page that shows a list of all users elligible for profile generation. It doesn't make sense to click on the user to be redirected to their apex:page before being able to generate the CSV or XLS or PDF. Instead, I want buttons available on the lightning page which I can click on to handle all of that.

2) I found some links about generating PDF and CSV within lightning but I'm wondering what the downsides are (so that I can make an educated decision on if I can live with it)

https://medium.com/@ToAnshulVerma/salesforce-lightning-generate-pdf-from-lightning-components-with-in-memory-data-cd5dffe0fe25

http://www.sfdcmonkey.com/2017/04/19/download-csv-file-data-salesforce-lightning-component/
NagendraNagendra (Salesforce Developers) 
Hi Ian,

Please find the below solution posted on stack exchange community.

Yes, it is possible to do so, but the Security Review team currently "doesn't allow" it as far as I can tell. If you're doing it for your own org, though, there doesn't appear to be any restriction. Keep in mind that you still need to be able to generate the content somehow, so you may very well end up using VF pages to generate the content for a PDF, although, for CSV and other non-binary types, it's your choice. Mind you, it's not the only way do so, but this is probably the easiest.

There are two potential downsides: (a) some browsers may arbitrary block drive-by downloads (Chrome seems okay with what I did in the sample code, but be aware that this may happen, so test carefully), and (b) your app may be delayed or blocked by the Security Review, since their policies seem to forbid doing this (why? I don't know, you'd have to ask them).
Basically, all you need to do is to is (a) get the content into PDF form, (b) encode it as a base64 string, (c) give this data back to the component/application, and (d) prompt to download using an anchor tag. You can do this for CSV and other types, but if you want to generate images and such, you'll want to explore other alternatives. For example, images can be made via canvas toDataURL, CSV is just plain text, etc.

Demo Page for PDF Content:
<apex:page >
    Hello World
</apex:page>
Sample Apex Code:
public class q193671 {
    @AuraEnabled public static String getData() {
        return EncodingUtil.base64Encode(Page.q193671.getContentAsPdf());
    }
}
Sample Application:
<aura:application controller="q193671">
    <ui:button press="{!c.pressed}" label="Download" />
</aura:application>
Sample Application Controller:
({
    pressed: function(component, event, helper) {
        var action = component.get("c.getData");
        action.setCallback(this, function(result) {
            var content = document.createElement("a");
            content.href = "data:application/pdf;base64,"+result.getReturnValue();
            content.download = 'content.pdf';
            content.click();
        });
        $A.enqueueAction(action);
    }
})
Kindly mark this as solved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra
Pavan K JPavan K J
Hi nagendra,
I'm new to lightning and i'm working on generating a pdf , so regarding aboove code can you please tell me what  line:06 in sample Application Controller doing?