Unable import existing data in jSignature

2.5k views Asked by At

This is the code I'm saving my jSignature value in my database:

var datapair = $('.sign-wrapper').jSignature();
datapair.bind('change', function(e){
        var data = datapair.jSignature("getData", "svgbase64");
        var i = new Image()
        i.src = "data:" + data[0] + "," + data[1];

        $(this).prev('input').val(i.src);
});

So I'm having following value in my DB:

data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iODkiIGhlaWdodD0iMTEyIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMTExIGMgMC4wNCAtMC4xNCAxLjAyIC01LjQ1IDIgLTggYyAyLjM0IC02LjA4IDUuOSAtMTEuODcgOCAtMTggYyAxLjg2IC01LjQ0IDMuMDkgLTExLjIyIDQgLTE3IGMgMS4xIC02Ljk4IDAuODggLTEzLjk4IDIgLTIxIGMgMS4yNSAtNy44MyAzLjA0IC0xNS4zOSA1IC0yMyBjIDAuNzEgLTIuNzYgMS43NiAtNS41MiAzIC04IGMgMS4wNSAtMi4wOSAyLjUyIC00LjA4IDQgLTYgYyAxLjg5IC0yLjQ2IDMuOTIgLTQuOTIgNiAtNyBjIDAuODEgLTAuODEgMi4yNiAtMi4yMiAzIC0yIGMgMS43NyAwLjUzIDUuMzcgMyA3IDUgYyAyLjM3IDIuOTEgNC40NiA3LjEzIDYgMTEgYyA1LjQ2IDEzLjc5IDkuMzIgMjguNTIgMTUgNDIgYyAyLjIzIDUuMjkgNi4zOCA5Ljc2IDkgMTUgYyAzLjQyIDYuODQgNS43NiAxNC43NiA5IDIxIGwgNCA0Ii8+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTSAyMCA2NCBjIDAuMTEgLTAuMTIgMy43MyAtNS40IDYgLTcgYyAyLjk4IC0yLjEgNy4yMSAtMy44NSAxMSAtNSBjIDcuMDcgLTIuMTQgMTQuNzggLTMuNzEgMjIgLTUgYyAxLjkyIC0wLjM0IDYgMCA2IDAiLz48L3N2Zz4=

So while reloading the page I'm having the above base64 image data url in a hidden variable.

I tried to load the data to the existing signature canvas using below code:

// If value exist load existing
      var src = $(this).prev('input').val();
      if(src != '') {
        datapair.jSignature("importData", src);
      }

But it is not working and I am getting following error in console:

Uncaught Error: jSignature is unable to find import plugin with for format 'image/svg+xml;base64'

2

There are 2 answers

6
karthick On BEST ANSWER

The problem occurs when your data is not recognized by the plugin.

The reason why it throws an error is, the format 'image/svg+xml;base64' is not supported by the importer, there is only an exporter support for it.

Reference: https://github.com/brinley/jSignature#data-import--export-and-plugins

I suggest you store the data using base30 because there is support for both export and import.

var data = datapair.jSignature("getData", "base30 ");
0
Manu Burrero Sánchez On

setData takes two arguments - data object, data format name. When data object is a string formatted in data-url pattern you don't need to specify the data dormat name. The data format name (mime) will be implied from the data-url prefix. See example below for that. Returns (in a traditional jQuery chainable way) jQuery object ref to the element onto which the plugin was applied.

You can store the value in base64 and load it. I load the value in base64 in an input and through Jquery I load it in the object.

// Creo el objeto de firma.
$("#jq_sig").jSignature
({
    // width/height of signature pad
    width: 497,
    height: 200,
    // Format bootstrap 4
    cssclass: "bg-light border",
    lineWidth: 2,
    color: "black",
});

// I take the value of the form field that I previously loaded with PHP.
var firma = $('#form input[name=firma]').val();

if (firma.length > 0) 
{
    $("#jq_sig").jSignature("setData", 'data:image/png;base64,' + firma);                       
    console.log('Signature loaded.');
}
else
{
    console.log('Signature not loaded.');
}

You can save to your database in any format. Then you tell 'setdata' what kind of format it is.