1

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

Problem statement

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?

Problem statement in context of my needs

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)

PDF Generation

CSV Generation

1 Answer 1

1

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);
    }
})

4
  • You're amazing thank you so much! May I ask if you reviewed anything to find the information? and if so could you link me to it? Also, some questions: "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," From the code you've posted you seem to be doing that, correct? (Just making sure so I know I can reference it) "your app may be delayed or blocked by the Security Review" Can you elaborate?
    – IanQ
    Commented Sep 27, 2017 at 3:17
  • @user49458 (a) A bit of MDN to remember how to specify a Data URI (always get the comma and semi-colon mixed up). (b) Yes, this demo code does, but you could write a REST service or something; you just need to be able to call PageReference getContentAsPDF. (c) I've heard that if you do this technique, SR will flag your app and not pass it, but I haven't been involved with ISV for a while now, so it's worth checking out yourself if you are doing ISV work. If you're just developing for clients, SR doesn't apply to you.
    – sfdcfox
    Commented Sep 27, 2017 at 3:29
  • Awesome I'm going to be testing the code now to see if it works as expected (sanity check for myself - nothing against you) if there's anything I don't understand may I PM you or email you?
    – IanQ
    Commented Sep 27, 2017 at 3:39
  • @user49458 Sure, feel free.
    – sfdcfox
    Commented Sep 27, 2017 at 4:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .