vue-signature-pad icon indicating copy to clipboard operation
vue-signature-pad copied to clipboard

Not working with jsPDF if v-show is hidden at first

Open hey0wing opened this issue 4 years ago • 6 comments

I had some pages with Navbar using v-show, and with SignaturePad in every page.

DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0

        <div id="Form0" ref="Form0" v-show="showing[0]">
                    <VueSignaturePad
                      :options="{onBegin: () => {$refs.Form0Signature.resizeCanvas()}}"
                      id="Form0Signature"
                      width="500px"
                      height="300px"
                      class="signaturePad"
                      ref="Form0Signature"
                      inline
                    />
        </div>
        <div id="Form1" ref="Form1" v-show="showing[1]">
                    <VueSignaturePad
                      :options="{onBegin: () => {$refs.Form1Signature.resizeCanvas()}}"
                      id="Form1Signature"
                      width="500px"
                      height="300px"
                      class="signaturePad"
                      ref="Form1Signature"
                      inline
                    />
        </div>
...
export default {
  name: "App",
  data: () => ({
    showing: {0:true, 1:false},
  }),
  methods: {
    downloadWithCSS() {
        html2canvas(forms[j], {canvas: canvasElement}).then(function(canvas) {
            var img = canvas.toDataURL("image/jpeg");
            var imgProps = doc.getImageProperties(img);
            if (j!==0){doc.addPage();}
            var pdfWidth = doc.internal.pageSize.getWidth();
            var pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
            doc.addImage(img, "PNG", 0, 0, pdfWidth, pdfHeight);
            if (j===1){doc.save("sample.pdf");}
       });
    }
}

hey0wing avatar Jul 01 '20 13:07 hey0wing

Try this code: <VueSignaturePad :options="signatureOptions" id="signature" ref="signature" v-if="parameter" />

DaxtonChen avatar Sep 15 '20 04:09 DaxtonChen

I can confirm that v-show is blocking signature pad from being editable or usable for that matter. I didn't get any console errors, it was just not working.

I had this piece of code:

<div v-show="signatureControlsActive == true" class="signatureWrapper">
<div v-show="signaturePadVisible == true">
   <div>
      <VueSignaturePad
         id="signature"
         ref="signaturePad"
         width="100%"
         height="180px"
         :options="options" />
   </div>
</div>
<div v-show="signatureTextVisible == true"></div>
              <h3>Text</h3>
            </div>
          </div>

And replacing v-show with v-if like so:

<div v-if="signatureControlsActive == true" class="signatureWrapper">
<div v-if="signaturePadVisible == true">
   <div>
      <VueSignaturePad
         id="signature"
         ref="signaturePad"
         width="100%"
         height="180px"
         :options="options" />
   </div>
</div>
<div v-show="signatureTextVisible == true"></div>
              <h3>Text</h3>
            </div>
          </div>

Fixed it for me.

Uraharadono avatar Jun 19 '21 13:06 Uraharadono

Changing the :key also works

jdexyz avatar Sep 21 '21 22:09 jdexyz

when you open the page with the new/different signature pad try running this, it must happen after the canvas is visible and has a fixed size

setTimeout(()=>{ this.$refs..resizeCanvas(); }, 10)

PaoloDiGallo avatar Nov 10 '21 12:11 PaoloDiGallo