Add the adapter with cargo add laravel-rust-barcode@0.1.
Register the barcode provider to use linear and matrix encoders from the Laravel container or MiniJinja templates.
use laravel::Application;
use laravel_rust_barcode::BarcodeServiceProvider;
let app = Application::configure()
.with_providers(|providers| { providers.add(BarcodeServiceProvider); })
.create();
The provider binds Dns1d and Dns2d as immutable singletons. Render options belong to each call.
| Variable | Default | Effect |
|---|---|---|
BARCODE_STORE_PATH | Application public directory | Existing directory for image files |
BARCODE_PUBLIC_PATH | Site root | Public path prefix for saved files |
Public URLs combine APP_URL, the configured prefix, and the encoded filename. Set
BarcodeConfig in the application's configuration callback to override these settings in code.
Linear and matrix services
use laravel_rust_barcode::{Dns1d, Dns2d, core::{GridStyle, RasterOptions}};
let dns1d = app.resolve::<Dns1d>().await;
let png = dns1d.png("HELLO", "C39+", true, RasterOptions::default())?;
let file = dns1d.store(&png, "HELLO", None)?;
let url = dns1d.uri(&file);
let dns2d = app.resolve::<Dns2d>().await;
let svg = dns2d.svg("https://example.com", "QRCODE,M", GridStyle::default())?;
Both services accept PHP type names without regard to ASCII case. Use Dns1d for SVG, HTML,
PNG or JPEG. Use Dns2d for SVG, HTML or PNG. A 2D type string may include QR or PDF417 options.
The matrix PNG method accepts an optional Logo decoded from image bytes.
The default filename uses a lowercase slug. For 2D symbols it also includes the full type string. The slug transliterates Latin-1 and Latin Extended-A letters and drops other scripts. A custom file name keeps the last path component and takes its extension from the renderer. The file is replaced atomically.
Templates
The default minijinja feature registers these template functions.
| Function | Output |
|---|---|
barcode_svg(code, kind) | Inline linear SVG with a label |
barcode_html(code, kind) | Linear HTML without a label |
barcode_2d_svg(code, kind) | Matrix SVG |
{{ barcode_svg(order.reference, "C128") }}
{{ barcode_2d_svg(order.url, "QRCODE,M") }}
The renderer escapes the label text before returning safe markup. Invalid inputs return a template error. Use the services for custom dimensions and colours. Disable default features to omit the template functions.
Binary payloads and static serving
For raw Code 128 function bytes, call Dns1d::encode with a byte slice and render the returned
symbol directly. The rendering shortcuts and templates take UTF-8 strings. For PDF417 options
containing the legacy raw 0xFF comma escape, call core::Pdf417Options::from_type_bytes with a byte slice. The escape is allowed in optional text fields, not in numeric file IDs.
On Unix, the atomic store creates files readable only by the application user. Serve them as that user, or manage file permissions separately if a static server runs under another account.
Use a dedicated barcode store for request-driven writes. Slugs can collide and each write replaces
the existing image, including its permissions. Avoid accepting caller-chosen names in shared stores.
Use uri() for an encoded URL. The core's explicit Store::temporary() is intended for local use,
not shared production storage. The Laravel provider instead defaults to the application's public
directory when no path is configured.
Rendering is synchronous. In services handling concurrent requests, set smaller application-level dimension limits and bound the number of blocking render tasks.