Getting started

laravel-rust-barcode Alpha Laravel for Rust

Install the framework-free crate and render a Code 39 symbol as SVG or HTML.

[dependencies]
laravel-rust-barcode-core = "0.1"
use laravel_rust_barcode_core::{Linear, LinearRenderOptions};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let kind: Linear = "C39+".parse()?;
    let symbol = kind.encode("HELLO")?;
    let options = LinearRenderOptions::new().height(60.0).color("#333");

    let svg = symbol.svg(Some("HELLO"), true, options)?;
    let html = symbol.html(Some(("HELLO", 12)), options)?;

    println!("{svg}\n{html}");
    Ok(())
}

The SVG call requests inline markup. The HTML call sets a 12 px human-readable label. Omit the label with None. With default rendering options, each narrow module is 2 px wide, the symbol is 30 px high, and the foreground is black.

Encoding returns EncodeError for an unknown type name, an invalid byte or a payload above 4096 bytes. Rendering returns RenderError for out-of-range dimensions or an invalid colour. Markup colours accept alphabetic names and hexadecimal RGB.

QR Code

Use QrLevel to encode QR Code Model 2 symbols and GridStyle to render them.

use laravel_rust_barcode_core::{GridStyle, QrLevel};

let code = QrLevel::M.encode("https://example.com")?;
let svg = code.svg(GridStyle::new().module(4, 4).padding(16))?;
# Ok::<(), Box<dyn std::error::Error>>(())

The example leaves a four-module quiet zone around the QR Code. The encoder selects the mask deterministically. A scanner recovers the same payload at the requested error-correction level, while the selected mask, grid, and version can differ from PHP. SVG and HTML output match PHP byte for byte when rendered from the same grid and options.

Data Matrix

Use encode_data_matrix(payload) for a square ECC 200 symbol, then render the grid with GridStyle. Leave a quiet zone at least one module wide. PHP and Rust can choose different compaction modes, so the grid can differ for the same payload.

PNG and JPEG

Call png on a linear or 2D symbol or jpeg on a linear symbol. The resulting image exposes the encoded bytes and a base64 string. Raster images are limited to 4096 pixels per side. The PNG background defaults to transparent. JPEG uses white. Linear labels use a 7 × 13 bitmap cell.

For a 2D logo, decode the logo bytes with Logo::decode and pass it to the grid renderer. Logos must be PNG, JPEG or GIF, at most 2 MiB and 1024 pixels per side.

Write images to disk

Create a Store with an existing directory and call write with the rendered image, the original payload, the full PHP type string for a 2D barcode, and an optional filename. The store writes the same bytes as the in-memory image and replaces an existing file atomically. Use the resulting StoredBarcode to get its disk path, public path or public URL.

Default slugs transliterate Latin-1 and Latin Extended-A letters. For a custom name, the store keeps the final path component and chooses the extension from the rendered format.