Hello Everyone, In this tutorial, I am providing a step-by-step procedure to create a signature pad Lightning component that use to draw signature and save as image format in any object of Salesforce. In this Lightning component, we are using two js file. To implement this example you have to upload these js files as a static resource and then include both of this static resource in your Lightning Component. I also provide both JS file in this tutorial to download.
After Completing this tutorial, you'll able to:
Step1: Go to File>New>Lightning Component and create a Lightning Component called Signature. Replace the following markup in the Lightning Component.
Signature.cmp
SignatureHandler.apxc
Preview.app
Output:
See also:
After Completing this tutorial, you'll able to:
- Create a Signature Pad Lightning Component.
So let's begin,
Resource: Download JS (after downloading please change file extension to ".js", otherwise it's not working)
Signature Pad
Login to your Salesforce Org. and open developer console.
Step1: Go to File>New>Lightning Component and create a Lightning Component called Signature. Replace the following markup in the Lightning Component.
Signature.cmp
<aura:component controller="SignatureHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" > <aura:handler event="aura:waiting" action="{!c.showSpinner}"/> <aura:handler event="aura:doneWaiting" action="{!c.hideSpinner}"/> <aura:attribute name="Spinner" type="boolean" default="false"/> <center><span style="font-weight:bold;font-size:20px;margin-top:10px;">Signature Pad</span></center> <div id="signature-pad"> <canvas style="border:1px solid black;width:100%;height:100%;margin-top:10px;" id="divsign"></canvas> <div style="float:right;margin-right:16px;"><a href="#" style="text-decoration:none;" id="btnClear">Clear Signature</a></div> </div> <ltng:require scripts="/resource/SignaturePad,/resource/SignApp" /> <br/> <center> <lightning:button variant="brand" label="Save" title="Save" onclick="{!c.SaveSignature}"/> </center> <aura:if isTrue="{!v.Spinner}"> <div aura:id="spinnerId" class="slds-spinner_container"> <div class="slds-spinner--brand slds-spinner slds-spinner--large slds-is-relative" role="alert"> <span class="slds-assistive-text">Loading</span> <div class="slds-spinner__dot-a"></div> <div class="slds-spinner__dot-b"></div> </div> </div> </aura:if> </aura:component>SignatureController.js
({ SaveSignature:function(component,event,helper){ var isblank=helper.isCanvasBlank(document.getElementById('divsign')); if(!isblank){ helper.saveSignature(component); } else{ alert('Please Sign in the box'); } }, showSpinner: function(component, event, helper) { component.set("v.Spinner", true); }, hideSpinner : function(component,event,helper){ component.set("v.Spinner", false); }, })SignatureHelper.js
({ isCanvasBlank:function (canvas) { var blank = document.createElement('canvas'); blank.width = canvas.width; blank.height = canvas.height; return canvas.toDataURL() == blank.toDataURL(); }, saveSignature:function(component){ var action = component.get("c.saveSign"); var vSplit=document.getElementById("divsign").toDataURL().split(',')[1]; //var vSplit=document.getElementById("divsign").toDataURL().replace(/^data:image\/(png|jpg);base64,/, ""); action.setParams({ base64Data:encodeURIComponent(vSplit), contentType:"image/png" }); action.setCallback(this, function(e) { if(e.getState()=='SUCCESS'){ alert('Signature Saved Successfully!') } else{ alert(JSON.stringify(e.getError())); } }); $A.enqueueAction(action); }, })Step2: Go to File>New>Apex Class and create an apex controller called SignatureHandler. Replace the following code.
SignatureHandler.apxc
public class SignatureHandler { @AuraEnabled public static void saveSign(String base64Data, String contentType) { List<Account> lstAcc=[SELECT Id,Name FROM Account limit 1]; base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8'); Attachment a = new Attachment(); a.parentId = lstAcc[0].Id; a.Body = EncodingUtil.base64Decode(base64Data); a.Name = 'Signature.png'; a.ContentType = contentType; insert a; } }
Step3: Go to File>New>Lightning Application and create a Lightning Application called Preview. Replace the following markup in the Lightning Application.
Preview.app
<aura:application extends="force:slds"> <c:Signature/> </aura:application>After that click on Preview button from Right Side Pallete.
Output:
See also:
- Multiselect Picklist in Lightning Component
- Global Search Lightning Component
- Lightning Design System in Salesforce
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.
22 Comments
the canvas does not allow drawing when the component is included in another cmp. Why ? When loaded on a lightning page it works.
ReplyDeleteI think, when you include component in another component , the scope of JS resource is lost.
DeleteI added the component in a community portal and I am also unable to draw... What should be done?
DeleteHi Manish,
ReplyDeleteI have implemented your example. When I Click Clear signature, the signature pad is getting the double than original one and in the url, "?Fragment=" is adding up.
Hello Hareesh, I think you made some mistake, I have implemented this example again in my developer org. It's working properly. Please check JS resource.
DeleteHi Manish,
ReplyDeleteIt worked good. thanks for your post. Now I want to implement multiple signature panels. can you help me out please.
yes, sure...!
DeleteHi Manish, great post.
ReplyDeleteThanks man..!
DeleteHi, It's a great post.
ReplyDeleteThere is an issue I am facing when I use this component as part of Quick action. The signature pad shows double in size and does not let me sign. But, if I reload the page and then click on the quick action button, the signature pad size comes correctly and let's me sign.
Any thoughts why would this be happening?
Hi, Salon
DeleteI am trying the same scenario in my org, but I couldn't find any issue in this component. I think you missed something. Please check javascript resource.
Are you passing recordId of an object from quickaction parent component to this Signature child component? That;s when the issue comes up.
DeleteHi Manish,
DeleteI am facing the same problem too, the js file you have provided is a text plain file.
After downloaded zip file extract js file and changes it extension from .txt to .js. It will convert text file in js file, after that you can use in your lightning component.
DeleteHi Manish, Thanks for sharing this.
ReplyDeleteI am trying to use this signature pad in Lightning Modal. Pad comes up correctly but it is not letting me sign. I am missing any thing here?
Hi, Karthik, Please check JS resource upload correctly and properly include in your lightning component.
ReplyDeleteHello,
ReplyDeleteThis is a great post and it works when I preview. However, I tried to add this component on a lightning community and it doesn't work. It still lets me attempt to save a signature and clears the pad if I click the button but doesn't let me draw the signature.
Thoughts?
Thanks!
Hi , this component is not working in lightning flows
ReplyDeleteThe image is saved in attachments but as we view the image it displays the signature but the background is very very dark making it tougher to view the image of signature.
ReplyDeleteMay I know how to fix the background issue?
ReplyDeleteAt Digital Marketing Thanks for this amazing content.
ReplyDeleteHey @Manish Singh, many people are unable to draw the signature. Same happened with me as well, I saw the handler class and there you have used a filter query within the List like this-->List lstAcc=[SELECT Id,Name FROM Account WHERE Name='SalesforceScool' limit 1]; The account with name "SalesforceScool" doesn't exists in our system and thus when I removed this filter, it worked for me. People who are facing similar problem can make this change in handler class---> List lstAcc=[SELECT Id,Name FROM Account limit 1];
ReplyDeletePost a Comment