Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download Image #50

Open
cianiandreadev opened this issue Dec 27, 2021 · 7 comments
Open

Download Image #50

cianiandreadev opened this issue Dec 27, 2021 · 7 comments
Assignees

Comments

@cianiandreadev
Copy link

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!!

@scopewu
Copy link
Owner

scopewu commented Dec 27, 2021

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

same issue: #14

@cianiandreadev
Copy link
Author

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

@geisi
Copy link

geisi commented Mar 1, 2022

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>

@Sanan4li
Copy link

Sanan4li commented Apr 4, 2023

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()
    },

@vishakha99-gif
Copy link

vishakha99-gif commented Apr 21, 2023

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

@gorkemtumkaya
Copy link

gorkemtumkaya commented Feb 5, 2024

@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>

@scopewu scopewu self-assigned this Sep 26, 2024
@maprangsoft
Copy link

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>

thank you very much. 🥰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants