PNG Buffer renderer
When to use
Section titled “When to use”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.
Minimal example
Section titled “Minimal example”import {qrcode} from '@qrcodesdk/core';import {QRCodePNGRenderer} from '@qrcodesdk/node';
const png = qrcode('https://qrcodesdk.dev').render(QRCodePNGRenderer());Styled example
Section titled “Styled example”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.
Return value
Section titled “Return value”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.
Renderer-specific options
Section titled “Renderer-specific options”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.
Center image
Section titled “Center image”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 |
Renderer-specific constraints
Section titled “Renderer-specific constraints”compression.levelmust be a safe integer from0through9.- The center-image source must be a
Uint8Array, such as a Node.jsBuffer, containing valid PNG bytes, at most 16 MiB and 16,777,216 pixels. Other formats, paths, and URLs are not decoded.rasterCenterImage()throwsINVALID_IMAGE_SOURCEfor invalid sources andINVALID_OPTIONSfor an invalid layout. - Pass a
centerImagecreated withrasterCenterImage(); 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 whenrasterCenterImage()is called, so later changes to the source bytes have no effect.
Related guides
Section titled “Related guides”- Customize appearance for shared styling and scan reliability.
- Add a center image for reading and sizing a PNG center image.
- Download or save for writing the
Bufferto disk. - Serve a QR code for
image/pngHTTP responses.
Related pages
Section titled “Related pages”Support QRCodeSDK
If you find QRCodeSDK useful, please consider sponsoring the project!