Search
Loading...
Skip to content

Insert QR Code

Add scannable QR codes to designs programmatically using image fills.

QR code demonstration showing image fill approach

5 mins
estimated time
Download
StackBlitz
GitHub

QR codes encode URLs that mobile devices can scan, making them useful for marketing materials, business cards, event posters, and product packaging. This guide shows how to generate QR codes as images and add them to CE.SDK designs.

Generating QR Code Images#

Use a QR code library like qrcode to generate QR codes as data URLs with custom colors.

// Generate QR code as data URL image with custom colors
const qrImageUrl = await generateQRCodeDataURL('https://img.ly', {
width: 256,
color: { dark: '#1a5fb4', light: '#ffffff' },
});

The toDataURL method creates a base64-encoded image that works directly with CE.SDK’s image fill. You can customize the colors at generation time.

Creating a QR Code Block#

Create a graphic block with a rectangle shape and apply the QR code as an image fill.

// Create graphic block with rectangle shape and image fill
const imageQrBlock = engine.block.create('graphic');
const rectShape = engine.block.createShape('rect');
engine.block.setShape(imageQrBlock, rectShape);
// Create image fill with QR code data URL
const imageFill = engine.block.createFill('image');
engine.block.setString(imageFill, 'fill/image/imageFileURI', qrImageUrl);
engine.block.setFill(imageQrBlock, imageFill);

Image fills use a rectangle shape with the QR code as the fill content. This approach is straightforward and supports color customization at generation time.

Positioning and Sizing#

Set the QR code dimensions and position on the page.

// Set dimensions and position for image-based QR code
engine.block.setWidth(imageQrBlock, qrSize);
engine.block.setHeight(imageQrBlock, qrSize);
engine.block.setPositionX(imageQrBlock, 300);
engine.block.setPositionY(imageQrBlock, 200);
// Add to page
engine.block.appendChild(page, imageQrBlock);

Maintain a square aspect ratio by setting equal width and height. For reliable scanning, keep QR codes at least 100x100 pixels.

API Reference#

MethodCategoryPurpose
engine.block.create('graphic')CreationCreate graphic block for QR code
engine.block.createShape('rect')ShapesCreate rectangle shape
engine.block.setShape(id, shape)ShapesApply shape to graphic block
engine.block.createFill('image')FillsCreate image fill
engine.block.setString(id, 'fill/image/imageFileURI', uri)FillsSet image data URL
engine.block.setFill(id, fill)FillsApply fill to block
engine.block.setWidth(id, width)TransformSet QR code width
engine.block.setHeight(id, height)TransformSet QR code height
engine.block.setPositionX(id, x)TransformSet horizontal position
engine.block.setPositionY(id, y)TransformSet vertical position
engine.block.appendChild(parent, child)HierarchyAdd QR code to page