Skip to content

Instantly share code, notes, and snippets.

@toanshulverma
Last active November 20, 2023 22:05
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • 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>
@angular1583
Copy link

Same as @AdrienRiou, sendDataProc in not a function
This page has an error. You might just need to refresh it.
Action failed: c:Expence$controller$downloadDocument [sendDataProc is not a function]
Failing descriptor: {c:Expence$controller$downloadDocument}

@tinyamasisurum0
Copy link

tinyamasisurum0 commented May 13, 2019

Thanks for the share.
How can I take control of var dataToSend and style it in PDFGenerator.vfp
Like:

` <apex:pageBlock title="My Content">

    <apex:pageBlockTable value="{!PDFData}" var="item">

        <apex:column value="{!item.aBCHoursRequired}"/>

    </apex:pageBlockTable>

</apex:pageBlock>  

`

Actually I want to use apex:pageBlockTable to create a table but I got an error because PDFData variable is a string.

Currently I am getting a JSON printed with no spaces or breaks which seems not read-friendly.

SHould I style the JSON in LightningPDFGeneratorDemoApp.app ? But how?
Thanks,

@lxicorruptxl
Copy link

Can you please demonstrate what it would look like to display Account Name and ID in a pdf visualforce page. Not sure what goes in the callback on the js controller or how to structure "datatosend". Thanks

@Abhilash090
Copy link

image

When i click on Click Download button getting error, please help me on this.

@rudra-shukla
Copy link

Hi,
Thanks for the explanation and code. i want to create a lightning component and show all accounts in a table and on a button click show that table in PDF. Any one can help me on this.

@toanshulverma
Copy link
Author

Thanks for the share.
How can I take control of var dataToSend and style it in PDFGenerator.vfp
Like:

` <apex:pageBlock title="My Content">

    <apex:pageBlockTable value="{!PDFData}" var="item">

        <apex:column value="{!item.aBCHoursRequired}"/>

    </apex:pageBlockTable>

</apex:pageBlock>  

`

Actually I want to use apex:pageBlockTable to create a table but I got an error because PDFData variable is a string.

Currently I am getting a JSON printed with no spaces or breaks which seems not read-friendly.

SHould I style the JSON in LightningPDFGeneratorDemoApp.app ? But how?
Thanks,

Two options i can think of right now:

  1. You can form the json; parse it within apex controller and set value to controller's list property; bind a datatable to this list property
  2. You can generate the html within your code and send serialized html code in PDFData attribute

@gbzitoo
Copy link

gbzitoo commented Jun 29, 2021

imagem

Quando eu clico no botão Click Download recebendo um erro, por favor me ajude nisso.

Conseguiu resolver ?

@karishmagodbole
Copy link

karishmagodbole commented Feb 24, 2022

I have this type of error how to I resolve. Please help me
Screenshot (15)

@toanshulverma
Copy link
Author

I have this type of error how to I resolve. Please help me Screenshot (15)

For running the demo, access LightningPDFGeneratorDemo visualforce page. The lightning app is not setup for demo.

@toanshulverma
Copy link
Author

imagem
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