Skip to content

WebP Buffer renderer

Use QRCodeWebPRenderer when Node.js-compatible code needs small raster bytes for a file, HTTP response, attachment, or object store, and the destination accepts WebP. The output is lossless WebP, so it keeps every pixel of the PNG Buffer renderer output while usually being much smaller: a default QR code for https://qrcodesdk.dev is about 310 bytes as WebP and 1.1 KB as PNG.

import {qrcode} from '@qrcodesdk/core';
import {QRCodeWebPRenderer} from '@qrcodesdk/node';
const webp = qrcode('https://qrcodesdk.dev').render(QRCodeWebPRenderer());
import {qrcode} from '@qrcodesdk/core';
import {diagonal, finder} from '@qrcodesdk/core/style';
import {QRCodeWebPRenderer} from '@qrcodesdk/node';
const webp = qrcode('https://qrcodesdk.dev').render(
QRCodeWebPRenderer({
style: {
moduleSize: 8,
modules: diagonal(),
finder: finder({ring: diagonal(), color: '#7c3aed'}),
},
}),
);

See Customize appearance for every color, shape, and finder option.

The renderer synchronously returns a Node.js Buffer containing a complete lossless WebP (VP8L) file. Pixels are identical to the PNG renderer’s output for the same options: the width and height are style.moduleSize × (matrix width + 2 × style.quietZone), shaped edges are anti-aliased, and RGBA colors keep their alpha. Output above 8192 × 8192 pixels (67,108,864 pixels) throws QRCodeError with code INVALID_OPTIONS before any pixels are allocated.

QRCodeWebPRendererOptions accepts style and centerImage. TypeScript rejects every other key; the renderer does not repeat that check at runtime.

Option Type Default Effect
style QRCodeVisualStyle shared defaults Applies graphical dimensions, colors, and shapes
centerImage QRCodeRasterCenterImage undefined Draws a center image from rasterCenterImage()

The encoder has no quality or effort setting. Output is always lossless.

Create the center image with rasterCenterImage() from PNG bytes, as for the PNG renderer. One center image can be reused across the PNG, WebP, GIF, and JPEG renderers:

import {readFile} from 'node:fs/promises';
import {qrcode} from '@qrcodesdk/core';
import {QRCodeWebPRenderer, rasterCenterImage} from '@qrcodesdk/node';
const centerImage = rasterCenterImage(await readFile('./logo.png'), {size: 0.3});
const webp = qrcode('https://qrcodesdk.dev')
.errorCorrection('H')
.render(QRCodeWebPRenderer({centerImage}));
  • The center-image source must contain PNG bytes, even for WebP output. See the PNG renderer constraints for its limits.
  • Most current browsers, image tools, and HTTP clients display WebP. Choose PNG when a destination such as an older email client or document format does not accept it.
  • Option values are copied when QRCodeWebPRenderer() is called.

Support QRCodeSDK

If you find QRCodeSDK useful, please consider sponsoring the project!