React library
Export and download
Save a QR code as PNG, JPG, WebP, GIF, BMP, ICO, TIFF, SVG, PDF or EPS with downloadQRCode, get a data URL, Blob or SVG markup, copy it, or make a PNG in Node.
downloadQRCode(props, options) saves the code described by props as a file in the browser, in any of six formats. Its siblings return the same file as a data URL, a Blob or markup instead, and qrdx/node makes a PNG on a server. Every function takes the same props as QRCode, so the file matches the code on screen.
Downloading from the playground or the dashboard instead? See Download and print a QR code in Help.
Download a file
import { downloadQRCode } from "qrdx";
await downloadQRCode(
{
value: "https://example.com",
fgColor: "#1d4ed8",
bodyPattern: "rounded",
frame: { style: "banner-bottom", text: "Scan me" },
},
{
format: "png",
size: { width: 1024, height: 1024 },
filename: "menu.png",
}
);
The browser saves menu.png. size is the pixel width and height of the code itself, from 50 to 5000 and square; a frame makes the file bigger than that. filename is optional: without it the file is named qr-code.<ext>, with the exact pixel size added for raster formats (qr-code-1024x1024.png, or qr-code-1270x1454.png with a bottom banner). quality (0 to 1) applies to WebP only.
Props are the same as on QRCode, except that a logo goes in imageSettings. To export the code a QRCode draws, including its logo and logoOptions, pass getQRData(props) or resolveQRCodeProps(props) instead of the raw props.
Formats
WebP
WebP is lossless by default, so module edges stay exact and a transparent background stays transparent. Where the browser's canvas writes lossless WebP (Chrome and Edge do), the browser makes the file. Where it cannot, the library checks the bytes and encodes the pixels itself with a small built-in lossless encoder: Safari, asked for WebP, returns a PNG. Either way a .webp file is always WebP.
quality (0 to 1) asks for a smaller lossy file instead, where the browser supports it. Lossy WebP softens module edges and colors, so keep the default for codes that need to scan from far away. getQRAsWebP returns the file as a Blob:
import { getQRAsWebP } from "qrdx";
const lossless = await getQRAsWebP({ value: "https://example.com", size: 1024 });
const lossy = await getQRAsWebP({ value: "https://example.com", size: 1024 }, { quality: 0.9 });canvasToWebP(canvas, options) does the same for any canvas, and encodeWebPLossless(rgba, width, height) encodes RGBA pixels with no DOM.
GIF
GIF is for the older tools and email builders that only take it. A GIF holds at most 256 colors and one transparent color. A code in flat colors (a palette of up to 8 included) fits as it is, so the GIF has exactly the pixels of the PNG. A gradient, an image fill or a photo logo has more colors, so it is reduced to a 256-color palette, with no dithering, because dither noise is what stops a code scanning. A pixel under half opaque becomes the transparent color and any other becomes opaque, so the soft edge of a code on a transparent background comes out hard-edged.
After a reduction the library decodes the original and the GIF: if the original reads and the GIF would not, downloadQRCode and getQRAsGIF throw a GIFScanError instead of saving a code that no longer scans, and the download dialog turns the button off and says to use a PNG. A design that doesn't decode to begin with isn't blamed on GIF. The check loads jsqr (a dependency of qrdx); a page without a bundler maps it in its import map, and without it a reduced GIF is refused, not passed on unchecked.
No browser can write GIF from a canvas (toBlob("image/gif") returns a PNG), so the library writes the file itself, in every browser, and checks its header before saving: a .gif file is always a GIF. getQRAsGIF returns it as a Blob:
import { getQRAsGIF, GIFScanError } from "qrdx";
try {
const gif = await getQRAsGIF({ value: "https://example.com", size: 1024 });
} catch (error) {
if (error instanceof GIFScanError) {
// The design needs more than 256 colors and would stop scanning: use PNG
}
}canvasToGIF(canvas, expected?) does the same for any canvas, pixelsToGIF(pixels, expected?) for RGBA pixels with no DOM, and encodeGIF(image) writes an image you have already reduced to a palette (quantizeRGBA does that step).
BMP
BMP is for the legacy systems and embedded tools that only take it. The file is an uncompressed 24-bit bitmap, so it is much larger than the same code as PNG. 24-bit BMP has no alpha channel, so a transparent background comes out white.
No browser can write BMP from a canvas (toBlob("image/bmp") returns a PNG), so the library writes the file itself, in every browser, and checks its header before saving: a .bmp file is always a BMP. getQRAsBMP returns it as a Blob:
import { getQRAsBMP } from "qrdx";
const bmp = await getQRAsBMP({ value: "https://example.com", size: 1024 });canvasToBMP(canvas) does the same for any canvas, and encodeBMP(rgba, width, height) writes RGBA pixels with no DOM.
ICO
ICO is the favicon format: one file holding the code at 16, 32, 48 and 256 px, each stored as a PNG inside the ICO, which every browser and Windows since Vista read. A transparent background stays transparent. Because the sizes are fixed, size doesn't apply and the file name states none (qr-code.ico), and a frame is left out, since every image is square and at most 256 px. The small sizes are for icons, not for scanning.
No browser can write ICO from a canvas (toBlob("image/x-icon") returns a PNG), so the library draws the code at each size, takes a PNG of each, writes the ICO itself, and checks its header before saving: a .ico file is always an ICO. getQRAsICO returns it as a Blob:
import { getQRAsICO } from "qrdx";
const favicon = await getQRAsICO({ value: "https://example.com" });canvasesToICO(canvases) writes any canvases of 1 to 256 px, and encodeICO(images) wraps PNG files you already have, with no DOM.
TIFF
TIFF is for print shops that ask for it. "tiff" is the code's pixels with its transparency, recorded at 300 dpi; "tiff-cmyk" is the same file in CMYK for print. The CMYK one uses the same conversion as the print PDF and EPS: pure black prints as black ink only (no rich black under fine modules), white is no ink, and a transparent background comes out white. No ICC profile is embedded, so ask your printer for a color-managed conversion when exact brand colors matter. Both are saved as .tif, the CMYK one with -cmyk in its name.
No browser can write TIFF from a canvas (toBlob("image/tiff") returns a PNG), so the library writes it itself (Deflate-compressed where the browser has Compression Streams, uncompressed otherwise) and checks its header before saving. getQRAsTIFF returns it as a Blob:
import { getQRAsTIFF } from "qrdx";
const tiff = await getQRAsTIFF(
{ value: "https://example.com", size: 1200 },
{ colorMode: "cmyk", dpi: 300 },
);canvasToTIFF(canvas, options) does the same for any canvas, and encodeTIFF(rgba, width, height, options) writes RGBA pixels with no DOM.
The PDF download holds the code as a PNG on a page of the same size. It needs the optional jspdf package (npm install jspdf); without it, PDF downloads throw PDF export needs the optional "jspdf" package. For a vector PDF at a physical size, with bleed, crop marks and CMYK, use downloadPrintPDF; see Print-ready export.
EPS
EPS needs no extra package. downloadQRCode reads size.width as points, so a 512 px request writes a 512 pt (18 cm) file. For an exact physical size, use getQRAsEPS, which returns the EPS as a string: the code is sizeMm wide (default 50 mm), and a frame adds to that.
import { downloadPrintEPS, getQRAsEPS } from "qrdx";
const eps = await getQRAsEPS(
{ value: "https://example.com" },
{ sizeMm: 40, bleedMm: 3, colorMode: "cmyk" }
);
await downloadPrintEPS({ value: "https://example.com" }, { sizeMm: 40, filename: "menu.eps" });EPS has no transparency and no crop marks. Linear and radial gradients print as smooth PostScript Level 3 shadings; angular gradients print as a fan of thin wedges. See-through colors are mixed with the background, and the background with white paper. PNG and JPEG logos are embedded. EPS cannot carry an image fill: getQRAsEPS throws EPS_IMAGE_FILL_REASON, and the dashboard's download dialog offers PDF, SVG and PNG instead.
Data URLs, Blobs and markup
import { copyQRCodeToClipboard, getQRCodeDataUrl, getSVGString } from "qrdx";
const props = { value: "https://example.com", size: 512 };
// The same file as a data URL, for an <img> or a form field
const png = await getQRCodeDataUrl(props, "png", { width: 512, height: 512 });
// A PNG on the clipboard, for pasting into a document
await copyQRCodeToClipboard(props, { width: 512, height: 512 });
// The SVG markup, for inlining in a page or an email template
const svg = await getSVGString(props);getQRCodeDataUrl(props, format, size, { quality })takes every formatdownloadQRCodetakes.copyQRCodeToClipboardwrites a PNG. Call it from a click handler: browsers refuse clipboard writes outside a user gesture.getSVGStringembeds the logo as a data URL, so the markup stands on its own.toSVGStringfromqrdx/coreis the synchronous version without that step.
Preset sizes
import { PRESET_SIZES, validateSize } from "qrdx";
PRESET_SIZES.small; // { width: 200, height: 200 }
PRESET_SIZES.medium; // 400
PRESET_SIZES.large; // 800
PRESET_SIZES.xlarge; // 1200
PRESET_SIZES["2xl"]; // 1600
PRESET_SIZES["3xl"]; // 2000
validateSize(4000, 4000); // { isValid: true }
validateSize(6000, 6000); // { isValid: false, error: "Size must not exceed 5000x5000 pixels" }Pick the size by where the file goes: 256 to 512 px for a web page, 1024 px or more for print at 300 pixels per inch (1024 px prints sharply up to about 8.7 cm wide). For exact print sizes use the print functions, which work in millimeters and inches.
PNG in Node
getQRAsPNG from qrdx/node returns PNG bytes without a browser. It needs one optional rasterizer installed next to qrdx: @resvg/resvg-js draws the SVG, logos included, the way browsers do; sharp also works.
import { writeFile } from "node:fs/promises";
import { getQRAsPNG } from "qrdx/node";
const png = await getQRAsPNG(
{ value: "https://example.com", bodyPattern: "rounded", margin: 4 },
{ size: 1024 }
);
await writeFile("qr-code.png", png);Import it from qrdx/node, never from qrdx, so browser bundles never see the native modules. A logo that is a data URL is embedded as it is; an http(s) URL or a local path is loaded only with loadExternalLogos: true, so a server passing a caller's settings through cannot be made to fetch internal URLs or read its own disk. SVG markup needs no rasterizer: toSVGString from qrdx/core runs anywhere.
If something goes wrong
Size must be at least 50x50 pixels,Size must not exceed 5000x5000 pixelsorWidth and height must be equal for QR codes.validateSizerejected the size. Pass a square size in range.PDF export needs the optional "jspdf" package. Installjspdfin the same package asqrdx.- The logo is missing from the file. The image comes from another origin without CORS headers, so the browser could not copy it. Serve it from your own origin or pass a data URL.
copyQRCodeToClipboardrejects. It ran outside a click or key handler, or the page is not served over HTTPS. Call it from the handler itself.getQRAsPNGthrows about a missing rasterizer. Install@resvg/resvg-jsorsharpnext toqrdx.
Related
Was this page helpful?
Last updated on
Blend a picture into the modules
Draw Logo QR and Image QR codes with the art prop, sample a logo or photo with loadQRArtImage or createQRArtImage, keep it scannable and export vector files.
shadcn/ui registry
Eight QR code components for shadcn/ui projects, built on the qrdx library. Install one with the shadcn CLI and own the code.
