Search
Loading...
Skip to content

Insert QR Code

Add scannable QR codes to designs programmatically using image fills.

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 in a headless Node.js environment.

Setting Up the Engine#

Initialize CE.SDK in headless mode for server-side processing.

// Initialize CE.SDK engine in headless mode
const engine = await CreativeEngine.init({
// license: process.env.CESDK_LICENSE, // Optional (trial mode available)
});

The headless engine runs without a UI, suitable for automated QR code generation.

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
const qrSize = 300;
engine.block.setWidth(imageQrBlock, qrSize);
engine.block.setHeight(imageQrBlock, qrSize);
engine.block.setPositionX(imageQrBlock, 50);
engine.block.setPositionY(imageQrBlock, 50);
// 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.

Exporting the Result#

Export the design with the QR code to a file.

// Export the QR code result
const blob = await engine.block.export(page, { mimeType: 'image/png' });
const buffer = Buffer.from(await blob.arrayBuffer());
writeFileSync(`${outputDir}/qr-code.png`, buffer);
console.log('✓ Exported QR code to output/qr-code.png');

Export to PNG for raster output. The QR code will be embedded in the exported image.

Cleaning Up#

Always dispose the engine when done to free resources.

// Always dispose the engine to free resources
engine.dispose();

The dispose method releases all engine resources. Use a try/finally pattern to ensure cleanup happens even if errors occur.

API Reference#

MethodCategoryPurpose
CreativeEngine.init(config)SetupInitialize headless engine
engine.scene.create(mode, options)SceneCreate design scene with page
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
engine.block.export(id, options)ExportExport design to image
engine.dispose()CleanupRelease engine resources