Skip to content

PNG Buffer renderer

Use QRCodePNGRenderer when Node.js-compatible code needs PNG bytes for a file, HTTP response, attachment, object store, or another binary API. PNG is the most widely supported raster format. For smaller files, use the WebP Buffer renderer; for destinations that only accept GIF or JPEG, use the GIF or JPEG Buffer renderer.

import {qrcode} from '@qrcodesdk/core';
import {QRCodePNGRenderer} from '@qrcodesdk/node';
const png = qrcode('https://qrcodesdk.dev').render(QRCodePNGRenderer());
import {qrcode} from '@qrcodesdk/core';
import {diagonal, finder} from '@qrcodesdk/core/style';
import {QRCodePNGRenderer} from '@qrcodesdk/node';
const png = qrcode('https://qrcodesdk.dev').render(
QRCodePNGRenderer({
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 PNG file. Its pixel 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.

QRCodePNGRendererOptions accepts style, centerImage, and compression. 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
compression.level integer 0–9 9 Sets the PNG DEFLATE compression level
centerImage QRCodeRasterCenterImage undefined Draws a center image from rasterCenterImage()

style and centerImage form QRCodeRasterRendererOptions, which the PNG, WebP, GIF, and JPEG renderers share.

Compression changes encoding effort and file size, not the rendered QR modules. A level of 0 disables DEFLATE compression; 9 requests the highest level.

rasterCenterImage(source, layout?) from @qrcodesdk/node creates the centerImage value for the PNG, WebP, GIF, and JPEG renderers. It validates and decodes the PNG bytes once, so one center image can be reused for many renders and formats:

import {readFile} from 'node:fs/promises';
import {qrcode} from '@qrcodesdk/core';
import {QRCodePNGRenderer, rasterCenterImage} from '@qrcodesdk/node';
const centerImage = rasterCenterImage(await readFile('./logo.png'), {size: 0.3});
const png = qrcode('https://qrcodesdk.dev')
.errorCorrection('H')
.render(QRCodePNGRenderer({centerImage}));
Parameter Type Default Effect
source Uint8Array required PNG bytes, such as a Node.js Buffer
layout.size number 0.3 Image box as a fraction of matrix width
layout.padding number 1 Cleared border around the image, in modules
layout.clearBackground boolean true Removes every module the image or its padding touches
  • compression.level must be a safe integer from 0 through 9.
  • The center-image source must be a Uint8Array, such as a Node.js Buffer, containing valid PNG bytes, at most 16 MiB and 16,777,216 pixels. Other formats, paths, and URLs are not decoded. rasterCenterImage() throws INVALID_IMAGE_SOURCE for invalid sources and INVALID_OPTIONS for an invalid layout.
  • Pass a centerImage created with rasterCenterImage(); TypeScript checks the factory result type.
  • Center images preserve aspect ratio, are composited with alpha, and are contained within the configured square without cropping.
  • Option values are copied when QRCodePNGRenderer() is called. The center image is decoded when rasterCenterImage() is called, so later changes to the source bytes have no effect.

Support QRCodeSDK

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