How to capture HTML5 Canvas signature and display it in output label?

I need to capture the customer signature from Html5 Canvas and display it in a Output label. Can anyone please help me out in this?

 <div data-role="page" id="signatureCaptureHome" style="over-flow:auto; width:10px; height:10px"  > 
      <div data-role="content" id="content">
       <canvas id="signatureCanvas"  height="50px" width="550px" style= "border:1px solid black" />
        <apex:outputLabel style="font-weight:Bold" value="{! signatureCanvas}"/>

        global static String signatureCanvas(String signatureBody, String parentId) {
              system.debug('Record Id == ' + parentId);
              Attachment a = new Attachment();
              a.ParentId = parentId;
              a.Body = EncodingUtil.base64Decode(signatureBody);
              a.ContentType = 'image/png';
             a.Name = 'Signature Capture.png';
             insert a;
           return '{success:true, attachId:' + a.Id + '}';
         }catch(Exception e){
             return JSON.serialize(e);
     return null;


OutputLabel wont help you here.

Use an Image tag basically if you have data in base64encoded in the signatureCanvas variable of your code

<img id="image" alt="data url loaded image" />

If above is the image tag ,then use the following java-script onload to show on vf

  var dataURL="data:image/jpeg;base64,"+{!signatureCanvas};
    document.getElementById("image").src = dataURL;

or try the below directly

<img id="image" alt="data url loaded image" "data:image/jpeg;base64,"+{!signatureCanvas}/>

Note carefully that the {!signatureCanvas} should have base64 encoded data when returned .

Source : Link , Question Author : Lavanya Sanathkumar , Answer Author : Mohith Shrivastava

Leave a Comment