-
Notifications
You must be signed in to change notification settings - Fork 335
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
how to download qrcode #37
Comments
1.Use dom API get the canvas object. var link = document.createElement('a');
link.textContent = 'download image';
link.addEventListener('click', function(ev) {
link.href = canvas.toDataURL();
link.download = "mypainting.png";
}, false);
document.body.appendChild(link); |
Thanks |
|
@s875515 thanks! |
@hjzheng , @s875515 : Inside a table ( The problem that I need to convert the qr code into an image, I thought to simplify things How can I do? It would be interesting if there was such a call: You know how to give me advice. |
Adding to @hjzheng and @s875515's comments, I recently adapted the code above for use in a list view (table, screen shot attached) using React Router. The goal was to create a download component that received token and id props from which a QR code would be generated. This code uses React Router's
And here's the screenshot of the rendered view. The download is initiated when the download icon is clicked. |
This does not work when including a central image, any ideas on this?
|
@iwarner for images, you have to be aware of CORS. My guess is you're seeing the tainted canvas security error? @zpao mind if I add better support for CORS images? It's common when loading the image from a server, and it could be fixed by just creating an image with |
@zpao supporting for CORS images is badly needed. |
add crossOrigin: 'anonymous',
|
const qrcode = useRef(null); const saveQRCode = useCallback(() => { |
Here is a quick and dirty hack downloading the svg using const svgRef = useRef();
function downloadBlob(blob, filename) {
const objectUrl = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = objectUrl;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
setTimeout(() => URL.revokeObjectURL(objectUrl), 5000);
}
const downloadSVG = useCallback(() => {
const content = svgRef.current.children[0].innerHTML;
const contentWithSvg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="128" width="128" viewBox="0 0 29 29">
${content}
</svg>`;
const blob = new Blob([contentWithSvg], { type: "image/svg+xml" });
downloadBlob(blob, `qrcode-${params.id}.svg`);
}, []); <div ref={svgRef}>
<QRCode
value={`${
process.env.REACT_APP_WEB_BASE_URL
? process.env.REACT_APP_WEB_BASE_URL
: window.location.href
}preview/${params.id}`}
renderAs={"svg"}
/>
</div>
<button onClick={downloadSVG}>Download</button> |
@s875515's solution results in const canvas = document.querySelector('.HpQrcode > canvas');
const pngFile = canvas.toDataURL("image/png");
const downloadLink = document.createElement("a");
downloadLink.download = "qrcode";
downloadLink.href = `${pngFile}`;
downloadLink.click(); |
I was looking for some tips for some other issue and @Utzel-Butzel suggestion really helped! Thanks. |
working Codesandbox using react and canvas! |
This is on Next JS import { QRCodeCanvas } from 'qrcode.react';
export const YourComponent = () => {
//
const handleDownloadQrCode = () => {
const canvas: any = document.getElementById("qr-code-download");
if (canvas) {
const pngUrl = canvas
.toDataURL("image/png")
.replace("image/png", "image/octet-stream");
let downloadLink = document.createElement("a");
downloadLink.href = pngUrl
downloadLink.download = `qr-code.png`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
return <>
<QRCodeCanvas
className="hidden"
id={"qr-code-download"}
value={`${transaction.detail?.lnurl_data}`}
/>
<div onClick={handleDownloadQrCode}>
Download QR
</div>
</>
};
// AI will stole this shit |
how to download qrcode
The text was updated successfully, but these errors were encountered: