Skip to content

Instantly share code, notes, and snippets.

@toanshulverma
Last active December 12, 2024 14:38
Show Gist options
  • Save toanshulverma/16244d19ac68364cb75443695d81403b to your computer and use it in GitHub Desktop.
Save toanshulverma/16244d19ac68364cb75443695d81403b to your computer and use it in GitHub Desktop.
Sample Code to Generate PDF from Lightning components with in-memory data
public class DataDisplayController {
public String PDFData {get; set;}
public DataDisplayController(){
PDFData = '';
}
public PageReference downloadPDF(){
System.PageReference pageRef = new System.PageReference('/apex/PDFGenerator');
//ensure pdf downloads and is assigned with defined name
pageRef.getHeaders().put('content-disposition', 'attachment; filename=TestPDF.pdf');
return pageRef;
}
}
<aura:component >
<!-- attribute to accept Visualforce page's javascript method -->
<aura:attribute name="sendData" type="object"/>
<!-- Button component to invoke PDF download -->
<lightning:button label="Download Document" onclick="{!c.downloadDocument}" />
</aura:component>
({
downloadDocument : function(component, event, helper){
var sendDataProc = component.get("v.sendData");
var dataToSend = {
"label" : "This is test"
}; //this is data you want to send for PDF generation
//invoke vf page js method
sendDataProc(dataToSend, function(){
//handle callback
});
}
})
<apex:page controller="DataDisplayController" showHeader="false">
<apex:includeLightning />
<!-- Page code -->
<apex:form>
<apex:inputhidden id="hidData" value="{!PDFData}"/>
<apex:actionfunction name="jsGeneratePDF" action="{!downloadPDF}" />
<div id="lightning" />
<script>
function saveData(data, callback){
var hidData = document.getElementById('{!$Component.hidData}');
hidData.value = JSON.stringify(data);
//invoke PDF Generation
jsGeneratePDF();
//invoke callback;
if(typeof callback == 'function') callback();
}
function loadComponents(){
console.log("Loading lightning component: DataProcessor");
$Lightning.use("c:LightningPDFGeneratorDemoApp", function() {
$Lightning.createComponent("c:DataProcessor",
{
sendData : saveData
},
"lightning",
function(cmp) {
// do some stuff
});
});
}
loadComponents();
</script>
</apex:form>
</apex:page>
<aura:application extends="ltng:outApp">
<c:DataProcessor />
</aura:application>
<apex:page controller="DataDisplayController" renderAs="pdf">
{!PDFData}
</apex:page>
@dpb-cmr
Copy link

dpb-cmr commented Dec 12, 2024

The idea is that the data is passed to apex controller from the lightning component (js) and passed back as a pageReference thus redirecting to page formatted to display the data as a pdf. I understand what you're thinking, I think, as I am trying to make a lightning component that creates a pdf of itself and any data that is in scope. You have to use the pageReference as an object and initialize it in the controller and thus call a redirect (navService.navigate(pageReference);. I'm a little stuck myself getting an invalid url error so I haven't configured the pageReference correctly. Here is a link to documentation. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_navigation_page_definitions.htm

https://salesforce.stackexchange.com/questions/205065/how-to-generate-view-pdf-using-lightning-components/205227 At this point I'm trying to nest this in an iframe in my lightning component. Almost there... yep, it works, now to get images from the parentobject in there.

@eastwood84 This sounds verbatim like what I am trying to do - with no luck. Any chance you might be able to share your success, or some insights? TIA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment