Skip to content

QR Code Generator ​

A basic QR Code Generator with optional logo overlay using the external QRCode.js library.

Type: Component

Available in: App Page · Home Page · Record Page · Flow Screen

Read on Medium ​

How to Generate QR Codes with Lightning Web Components

How to Generate QR Codes with Lightning Web Components

Attributes ​

NameTypeDefaultDescription
color-darkstring'#000000'The initial color of the dark modules of the code, as any CSS color. Users can change it.
color-lightstring'#ffffff'The initial color of the light modules of the code, as any CSS color. Users can change it.
logo-urlstringThe logo to show in the code. An image from another domain needs to allow this with CORS.
record-idstringThe record to save the code to. Set automatically on record pages.
save-to-recordbooleanfalseIf present, a button saves the code as a file on the record given by record-id.
sizenumber256The displayed size of the code in pixels. The image has at least a resolution of 1024 pixels.
qr-code-data-urlstringRead-only. The code as a PNG data URL, empty without a code. Also available as a Flow output.
content-document-idstringRead-only. The id of the saved file. Empties with a new code. Also available as a Flow output.

Events ​

NameDetailDescription
qrcodegenerate{ dataUrl, value }Fired when a code was generated.
qrcodesave{ contentDocumentId }Fired after the code was saved as a file on the record.

Usage ​

html
<c-qr-code-generator size="300" onqrcodegenerate={handleGenerate}></c-qr-code-generator>

Users enter a target URL, optionally upload a logo and generate the code. The URL has to be complete, including https://.

Users can also change both colors. A hint appears if the contrast is low or the code is inverted, because such a code may be hard to scan. It doesn't block generating.

The image includes the empty border that scanners need, in the light color, so a saved code can be printed or placed anywhere.

Saving creates the file directly through Lightning Data Service, so no Apex is involved and the permissions of the user apply.

Custom Labels ​

The texts users see are Custom Labels, so they can be translated in Setup with the Translation Workbench. Copy them together with the component.

NameValue
ErrorAlert_ErrorError
ErrorUtils_UnknownErrorUnknown error
QrCodeGenerator_DarkColorDark Color
QrCodeGenerator_EnterUrlPlease enter a URL.
QrCodeGenerator_ErrorSavingError saving the QR code
QrCodeGenerator_FileNotReadFailed to read the selected file.
QrCodeGenerator_FileTitleQR Code
QrCodeGenerator_GenerateGenerate QR Code
QrCodeGenerator_GeneratedCodeGenerated QR Code
QrCodeGenerator_GeneratingGenerating...
QrCodeGenerator_GenerationFailedFailed to generate QR code:
QrCodeGenerator_HardToScanThese colors may be hard to scan.
QrCodeGenerator_LibraryMissingQR code library not loaded. Please try again.
QrCodeGenerator_LibraryNotLoadedFailed to load QR code library:
QrCodeGenerator_LightColorLight Color
QrCodeGenerator_LogoLogo (optional)
QrCodeGenerator_LogoPreviewLogo preview
QrCodeGenerator_RemoveLogoRemove logo
QrCodeGenerator_SaveSave
QrCodeGenerator_SavedThe QR code has been saved.
QrCodeGenerator_SuccessSuccess
QrCodeGenerator_TargetUrlTarget URL
QrCodeGenerator_TitleQR Code Generator
QrCodeGenerator_UrlPlaceholderhttps://example.com

Component Dependencies ​

NameTypeDescription
labelUtilsLWCShared function to fill the placeholders of Custom Labels.
qrcodejsStatic ResourceQRCode.js - JavaScript Library for generating QRCodes with Logo Support.
errorAlertLWCInline alert that shows error messages to the user.
errorUtilsLWCShared functions to read error messages and show error toasts.

Released under the MIT License. · llms.txt for AI agents