I’m using Thomas Bradley’s Signature Pad plugin for a project, which I’ve used successfully before. The twist this time is that I want to save the signature as an image rather than just as JSON.
There’s a method called getSignatureImage() that works just fine to grab the signature as a base64 string, like so (this is the result for an empty canvas):
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAArwAAACWCAYAAAA8Els6AAAIjElEQ…QIECBg8LoBAgQIECBAgACBtIDBm65XOAIECBAgQIAAgQtmc1xdE+aNPAAAAABJRU5ErkJggg==
(Note the “…”; it’s a much longer string.)
I was having a problem saving the signature to a library successfully. Uploading the file was easy, but the image file was always ending up corrupted. It didn’t matter if I uploaded to a Document library or Picture Library; no joy.
I knew I was missing something obvious. I tried removing the leading “data:image/png;base64,”. I tried different values for Content-Type, etc. It had to be something about the way I was creating the file.
In the end, I got some great advice from a colleague. The base64 content has to be *decoded* so that we can save it. This is what worked:
2 | var img = signatureArea.getSignatureImage(); |
3 | var outfile = fakefilename(); |
6 | url: _spPageContextInfo.webAbsoluteUrl + |
7 | "/_api/web/getfolderbyserverrelativeurl('/path/to/my/picture/library/')/files/add(overwrite=true, url='" + outfile + "')" , |
9 | data: convertDataURIToBinary(img), |
12 | "accept" : "application/json;odata=verbose" , |
13 | "X-RequestDigest" : $( "#__REQUESTDIGEST" ).val() |
16 | $( "#sig-file" ).attr( "src" , outfile); |
22 | var convertDataURIToBinary = function (dataURI) { |
23 | var base64Marker = ";base64," ; |
24 | var base64Index = dataURI.indexOf(base64Marker) + base64Marker.length; |
25 | var base64 = dataURI.substring(base64Index); |
26 | var raw = window.atob(base64); |
27 | var rawLength = raw.length; |
28 | var array = new window.Uint8Array( new window.ArrayBuffer(rawLength)); |
30 | for (i = 0; i < rawLength; i++) { |
31 | array[i] = raw.charCodeAt(i); |
Hi Marc,
I was able to implement Signature in Infopath form according to your previous article. But since I want to print out the form with signature I think I will need to save the signature as an image.
Should I add the above script to code already written for JSON signature ?
And should I create an image field in form and place it in section with screenTip “==Signature==” ?
Do I need to create a picture library and how can I reference it to the field ?
Thanks in advance for all your help.
@MMomin:
In another implementation of this, I stored the image files in a library and put the URL to the image(s) in the InfoPath form. So it’s definitely possible; you just need to figure out how you want it to work.
M.
Thanks Marc
That worked very well in Chrome. I really appreciate all your help.
But in IE 11 it throws error at
var array = new window.Uint8Array(new window.ArrayBuffer(rawLength));
Any advice ?
This article https://paultavares.wordpress.com/2015/01/20/how-to-render-a-sharepoint-page-in-ie-using-edge-mode-without-changing-the-master-page/ solved the above issue with IE.
Hello Marc. The signature on the form shows as coordinates when I use the PeoplePicker or save the form. Any ideas on getting it to show as an actual signature on the form when saved or the PeoplePicker is populated?
@Michael:
The signature is saved as a set of data, not as an image unless you set things up to create an image. This earlier post might be useful.
M.