# Getting started (laravel-rust-qrcode)

> Install the Rust QR code package, render a PNG, and register its Laravel route and template helpers.

- Package: laravel-rust-qrcode (Laravel for Rust, alpha), crates: `laravel-rust-qrcode`, `laravel-rust-qrcode-core`
- Maintenance: active; Latest release: 0.1.0 (2026-12-04); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-qrcode), [crates.io](https://crates.io/crates/laravel-rust-qrcode/0.1.0), [docs.rs](https://docs.rs/laravel-rust-qrcode/0.1.0), [Changelog](https://oss.bowerbird.si/qrcode/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-qrcode/blob/0d4061426c0e70f1d1e0fac79c65fe7f046a3dab/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-qrcode/blob/0d4061426c0e70f1d1e0fac79c65fe7f046a3dab/SECURITY.md)
- Based on: [Simple QrCode](https://github.com/SimpleSoftwareIO/simple-qrcode): The QR code builder this package ports: formats, styling, logos, payload helpers and Laravel integration, following version 4.2.0.
- PHP parity: core (13 covered, 19 diverged, 18 planned of 50 abilities; 83 fixtures; follows simplesoftwareio/simple-qrcode 4.2.0; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/qrcode/docs/getting-started/
- Source: https://github.com/bowerbird-si/laravel-rust-qrcode/blob/0d4061426c0e70f1d1e0fac79c65fe7f046a3dab/docs/getting-started.md

Install the Rust QR code package, render a PNG, and register its Laravel route and template helpers.

```toml
[dependencies]
laravel-rust-qrcode = "0.1.0"
```

The `minijinja` feature is on by default and adds template functions. Set `default-features = false` for
a route or application that does not use views. For use without Laravel, depend on the
`laravel-rust-qrcode-core` crate, which exposes the encoder, renderer, and payload helpers.

## Generate a code

```rust
use laravel_rust_qrcode::{Format, Generator};

let code = Generator::new()
    .format(Format::Png)
    .size(174)
    .margin(4)
    .generate("https://example.test/")?;
code.write_to("example.png")?;
```

Use `as_bytes()` to borrow the output, `into_bytes()` to take ownership, or `as_svg()` for SVG markup.
A generator can render several payloads with the same settings. Its defaults match PHP's SVG,
100-pixel canvas, zero margin, black foreground, white background, error correction L, and ISO-8859-1.
Allow a four-module quiet zone for scanning.

## Use it in Laravel

Register the service provider to inject a factory or use the template functions.

```rust
use laravel::Application;
use laravel_rust_qrcode::QrCodeServiceProvider;

let app = Application::configure()
    .with_providers(|providers| {
        providers.add(QrCodeServiceProvider);
    })
    .create();
```

A route can inject the factory. Each call to `builder()` starts with fresh options.

```rust
use laravel::{container::Inject, http::Response};
use laravel_rust_qrcode::{Format, QrCodeFactory};

async fn ticket_code(qr: Inject<QrCodeFactory>) -> laravel::Result<Response> {
    Ok(qr.builder()
        .format(Format::Png)
        .size(174)
        .margin(4)
        .response("https://example.test/tickets/42")?)
}
```

The response sets the content type for its format. In a MiniJinja view, use either helper with an
optional size and margin. Both encode text as UTF-8.

```html
<div>{{ qrcode(ticket_url, 174, 4) }}</div>
<img src="{{ qrcode_png(ticket_url, 174, 4) }}" alt="Ticket QR code">
```

The SVG helper marks generated markup safe. Payload text is encoded into modules. The PNG helper
returns a data URI. For email attachments, render PNG in Rust and pass `into_bytes()` and `image/png`
to your mailer.

## Encoding and sizing

Use `.encoding(Encoding::Utf8)` when the text cannot be represented in ISO-8859-1.
`generate_bytes(&[u8])` encodes opaque bytes. For a fixed QR version or mask, give the generator an
explicit encoder, for example `Generator::new().with_encoder(Encoder::new().version(5).mask(2))`.

Supported text encodings are UTF-8, UTF-16BE, ASCII, ISO-8859-1 through ISO-8859-11, ISO-8859-13
through ISO-8859-16, Shift-JIS, Windows-1250, Windows-1251, Windows-1252, Windows-1256, GBK, and EUC-KR.
The parser accepts `SHIFT-JIS`, `SHIFT_JIS`, `ASCII`, and `US-ASCII` case insensitively.

| Setting | Range |
| --- | --- |
| Canvas edge | 1 through 4096 pixels or PostScript points |
| Quiet zone | 0 through 64 modules on each side |
| QR version | 1 through 40 |
| Mask | 0 through 7 |
| RGBA alpha | 0 through 255 |

PNG needs enough pixels for at least one per module including the quiet zone. Its minimum edge is
`17 + 4 × version + 2 × margin`. Choose a canvas that is a multiple of that number for even module
widths. EPS composites partial transparency onto white. SVG and PNG support alpha.

See [Migrating from PHP](https://oss.bowerbird.si/qrcode/docs/migrating-from-php/) for all six payload helpers and their differences.
