qrcode.vue icon indicating copy to clipboard operation
qrcode.vue copied to clipboard

Download Image

Open cianiandreadev opened this issue 3 years ago • 10 comments

Hi 👋, I am using the version 1.7 for Vue 2.

I would like to provide the page with a button that allows to download the generated QR.

Ideally (since the page contains multiple QR code) would be awesome if the same button allows to download more then one at the same time.

How would that be possible? (If it is possible?)

Thanks in advance for your help and thanks for your great work!!

cianiandreadev avatar Dec 27 '21 10:12 cianiandreadev

@cianiandreadev Hi, you can try the .toDataURL of canvas. Looking more info on stackoverflow.com

same issue: #14

scopewu avatar Dec 27 '21 12:12 scopewu

Thank you for your support and quick answer!! I will try as you suggested 🙂

cianiandreadev avatar Dec 27 '21 12:12 cianiandreadev

If anyone else wants to implement a QR Code download function I have it working smoothly with following solution:

<template>
    <div ref="qrcode">
           <qrcode-vue :value="foo"/>
     </div>
    <button @click="downloadQrCode">Download</button>
</template>
<script setup>
...
const qrcode = ref(null)
const downloadQrCode = () => {
    let canvasImage = qrcode.value.getElementsByTagName('canvas')[0].toDataURL('image/png');
    let xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function () {
        let a = document.createElement('a');
        a.href = window.URL.createObjectURL(xhr.response);
        a.download = 'qrcode.png';
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        a.remove();
    };
    xhr.open('GET', canvasImage);
    xhr.send();
...
}
</script>

geisi avatar Mar 01 '22 15:03 geisi

This is the code I used to download the QR.

 <qrcode-vue
                    id="qr-code"
                    value="https://github.com"
                    :size="200"
                    level="H"
                  />
                  
                   <button

                class="px-3 py-2 bg-white rounded-4p border-gray-100 mt-5"
                @click="downloadQr"
              >
                Download
              </button>
                   
 
 
 
 downloadQr () {
      const link = document.createElement('a')
      link.download = `qr-code.png`
      link.href = document.getElementById('qr-code').childNodes[0].toDataURL()
      link.click()
    },

Sanan4li avatar Apr 04 '23 09:04 Sanan4li

hello, I found one issue in this . if we download qrcode using this logic then that downloaded qrcode's background is black so that it is overlapping and i'm not able to scan the code .. so can anyone please help me with how can i change the bgcolor of generated canvas ??

Thanks in Advance

vishakha99-gif avatar Apr 21 '23 04:04 vishakha99-gif

@vishakha99-gif Hey! Just try giving some margin to qrcode. This works for me.

<template>
  <div
    ref="qrcodeContainer">
    <qrcode-vue
      ref="qrcode"
      :value="value"
      :level="level"
      :margin="4"
      :size="300" />
  </div>

  <button @click="downloadQrCode">Download</button>
</template>

<script setup lang="ts">
import { ref } from "vue";
import QrcodeVue, { Level } from "qrcode.vue";

defineProps<{
  value: string;
}>();

const level = ref<Level>("M");

const qrcodeContainer = ref();

function downloadQrCode() {
  const canvas = qrcodeContainer.value.querySelector("canvas");
  const link = document.createElement("a");
  link.download = "qr-code.png";
  link.href = canvas.toDataURL("image/png");
  link.click();
}
</script>

gorkemtumkaya avatar Feb 05 '24 15:02 gorkemtumkaya