Last active
June 20, 2024 23:13
-
-
Save toanshulverma/16244d19ac68364cb75443695d81403b to your computer and use it in GitHub Desktop.
Sample Code to Generate PDF from Lightning components with in-memory data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
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 | |
}); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:application extends="ltng:outApp"> | |
<c:DataProcessor /> | |
</aura:application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="DataDisplayController" renderAs="pdf"> | |
{!PDFData} | |
</apex:page> |
Quando eu clico no botão Click Download recebendo um erro, por favor me ajude nisso.Conseguiu resolver ?
Will need more details. I just recreated whole solution afresh in a separate environment and it is working as expected. So, I assume, there must be something missing/wrong at your end. Do try to re-create this in a fresh developer org for clarity.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For running the demo, access LightningPDFGeneratorDemo visualforce page. The lightning app is not setup for demo.