Skip to content

QR Codes

Short URL generates QR code images for every short URL using the endroid/qr-code PHP library.

Endpoints

QR codes are served at dedicated API routes. All endpoints require the Access content permission.

Endpoint Format Description
/api/shorturl/qr/{slug} Config Default format (PNG/SVG)
/api/shorturl/qr/{slug}/png PNG Raster image
/api/shorturl/qr/{slug}/svg SVG Vector image

The default format is configured in the module settings.

Responses include a 24-hour browser cache header (Cache-Control: max-age=86400). The PNG and SVG endpoints are served inline, so the image opens in the browser, where it can be copied or saved; saving proposes {slug}.png or {slug}.svg.

Path shortcut

Appending .qr to any short URL slug serves the QR code in the default format. For example, if your short URL is https://example.com/promo, then https://example.com/promo.qr returns the QR code image.

This is handled by an inbound path processor that rewrites /{slug}.qr to /api/shorturl/qr/{slug}.

Node view display

The Short URL field (the slug) of the Short URL content type is displayed with the Short URL and QR code formatter. It renders:

  • The full short URL as a clickable input field with a copy-to-clipboard button
  • The destination URL
  • An optional label
  • An inline QR code (SVG data URI)
  • Links that open the SVG and PNG images

The field can be positioned, hidden or given another formatter via Manage display on the Short URL content type. The destination and label fields are hidden there by default, since the formatter already shows them.

QR code settings

Setting Values Description
Error correction Medium Balance between size and recovery
Size 200px / 300px 200px for inline, 300px for the SVG and PNG endpoints
Margin 10px Quiet zone around the QR code
Encoding UTF-8 Character encoding

These values are hardcoded in the QrCodeGenerator service. To customize them, override the service in your site's services.yml:

services:
  Drupal\shorturl\QrCodeGeneratorInterface:
    class: Drupal\my_module\MyQrCodeGenerator