Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5611

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T10:53:07+00:00 2024-11-27T10:53:07+00:00

How to download fancy QR Codes with React

  • 60k

Generating QR codes on the web is easy. There are lots of libraries out there who help us do it. One of them is qrcode.react which we will be using for the purpose of this tutorial.

Code to download a plain QR Code

We're rendering a <canvas> element in the DOM using QRCodeCanvas supplied by qrcode.react. We can convert it to a png using JavaScript and then download it as a file.

// QRCodeDownload.tsx import React from "react" import { QRCodeCanvas } from "qrcode.react"  import Button from "components/Button"  const QRCodeDownload = () => {   const url = "https://anshsaini.com"    const downloadQRCode = () => {     const canvas = document.querySelector("#qrcode-canvas") as HTMLCanvasElement     if (!canvas) throw new Error("<canvas> not found in the DOM")      const pngUrl = canvas       .toDataURL("image/png")       .replace("image/png", "image/octet-stream")     const downloadLink = document.createElement("a")     downloadLink.href = pngUrl     downloadLink.download = "QR code.png"     document.body.appendChild(downloadLink)     downloadLink.click()     document.body.removeChild(downloadLink)   }    return (     <div className="p-3">       <QRCodeCanvas id="qrcode-canvas" level="H" size={300} value={url} />       <div className="my-5">         <Button onClick={downloadQRCode}>Download QR Code</Button>       </div>     </div>   ) }  export default QRCodeDownload 
Enter fullscreen mode Exit fullscreen mode

QR Code downloaded

Voila✨ The QR Code is now saved in the file system as an image.

Taking it a step further

No one wants to share a QR code on its own. Its ugly. We want a nice UI surrounding the QR code, giving it some context. Maybe something like this:
Fancy QR Code

Let's a create a wrapper component which will handle the styling. I'm using react-jss for styling but of course you can use whatever you want and style your QR Code however you like.

// QRCodeTemplate.tsx import React, { FC, PropsWithChildren } from "react"  import DEV_RAINBOW_LOGO from "assets/qr_code/dev-rainbow.png" import DEV_RAINBOW_BG from "assets/qr_code/dev-rainbow-bg.png" import { createUseStyles } from "react-jss"  import Typography from "components/Typography"  const ICON_SIZE = 100  const useStyles = createUseStyles(theme => ({   root: {     backgroundImage: `url('${DEV_RAINBOW_BG}')`,     maxWidth: 1060,     maxHeight: 1521,     borderRadius: "32px",     border: "5px solid #EFEFF7",     padding: theme.spacing(11),   },   logo: {     boxShadow: "9px 3px 20px 1px rgb(0 0 0 / 10%)",     height: 150,     width: 150,     borderRadius: 8,   },   icon: {     borderRadius: 8,     width: ICON_SIZE,     height: ICON_SIZE,     position: "absolute",     // Need to offset the values due to `excavate: true` in qrcode.react     top: `calc(50% - ${ICON_SIZE / 2 + 1}px)`,     // Need to offset the values due to `excavate: true` in qrcode.react     left: `calc(50% - ${ICON_SIZE / 2 - 5}px)`,   },   qrContainer: {     position: "relative",     backgroundColor: "#EFEFF7",     borderRadius: "56px",     margin: theme.spacing(8, 0),     padding: theme.spacing(4),   },   qrInner: {     backgroundColor: "white",     borderRadius: "32px",     padding: 90,   },   referredBy: {     fontStyle: "normal",     fontWeight: "600",     fontSize: "24px",     lineHeight: "29px",     letterSpacing: "0.1em",   }, }))  const QRCodeTemplate: FC<PropsWithChildren> = ({ children }) => {   const classes = useStyles()   return (     <div className={classes.root} id="fancy-qr-code">       <img alt="logo" className={classes.logo} src={DEV_RAINBOW_LOGO} />       <Typography className="mt-7" variant="title1">         To register, scan the QR Code.       </Typography>        <div className={classes.qrContainer}>         <img           alt="icon"           className={classes.icon}           height={ICON_SIZE}           src={DEV_RAINBOW_LOGO}           width={ICON_SIZE}         />         <div className={classes.qrInner}>{children}</div>       </div>       <Typography className={classes.referredBy} variant="emphasised">         REFERRED BY       </Typography>       <Typography variant="largeTitle">Ansh Saini</Typography>     </div>   ) }  export default QRCodeTemplate 
Enter fullscreen mode Exit fullscreen mode

We can now pass our QR Code as children to the QRCodeTemplate

<QRCodeTemplate>   <QRCodeCanvas     // Passing image here doesn't render when we convert html to canvas. So, we handle it manually.     // We are only using this prop for `excavate: true` use case.     imageSettings={{       src: "",       x: undefined,       y: undefined,       height: 80,       width: 80,       excavate: true,     }}     level="H"     size={640}     value={url}   /> </QRCodeTemplate> 
Enter fullscreen mode Exit fullscreen mode

Earlier we only had a canvas element in the DOM so we could directly convert it to a png. But now, our DOM includes HTML elements like <div>, <img> etc. for styling. So we need to parse our DOM as canvas and then convert it into a png. There's a libary which helps us do just that. html2canvas. We modify our downloadQRCode function such that first we get snapshot of our DOM as a canvas then we convert that into a png.

Final code

// QRCodeTemplate.tsx import React from "react"  import html2canvas from "html2canvas" import { QRCodeCanvas } from "qrcode.react"  import Button from "components/Button" import QRCodeTemplate from "components/dashboard/QRCodeTemplate"  const QRCodeDownload = () => {   const url = "https://anshsaini.com"    const getCanvas = () => {     const qr = document.getElementById("fancy-qr-code")     if (!qr) return      return html2canvas(qr, {       onclone: snapshot => {         const qrElement = snapshot.getElementById("fancy-qr-code")         if (!qrElement) return         // Make element visible for cloning         qrElement.style.display = "block"       },     })   }    const downloadQRCode = async () => {     const canvas = await getCanvas()     if (!canvas) throw new Error("<canvas> not found in DOM")      const pngUrl = canvas       .toDataURL("image/png")       .replace("image/png", "image/octet-stream")     const downloadLink = document.createElement("a")     downloadLink.href = pngUrl     downloadLink.download = "QR code.png"     document.body.appendChild(downloadLink)     downloadLink.click()     document.body.removeChild(downloadLink)   }    return (     <div className="p-3">       <QRCodeTemplate>         <QRCodeCanvas           // Passing image here doesn't render when we convert html to canvas. So, we handle it manually.           // We are only using this prop for `excavate: true` use case.           imageSettings={{             src: "",             x: undefined,             y: undefined,             height: 80,             width: 80,             excavate: true,           }}           level="H"           size={640}           value={url}         />       </QRCodeTemplate>       <div className="my-5">         <Button onClick={downloadQRCode}>Download QR Code</Button>       </div>     </div>   ) }  export default QRCodeDownload 
Enter fullscreen mode Exit fullscreen mode

The Result

We have a beautiful QR code saved as a png. And with the power of React, we can create as many styles as we want.
Fancy QR Code downloaded

javascriptreacttutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.