QR Label Generator (crate)
Rust library to generate QR code images, with simple APIs for single and batch generation from JSON files. Publishable on crates.io.
Features
- ✅ Single QR code generation
- ✅ Batch generation from JSON and CSV files
- ✅ Size customization
- ✅ Robust error handling with
anyhow - ✅ PNG output format (via
image) - ✅ Warehouse PDF label generation (A4 format with multi-layout)
- ✅ Support for warehouse items with shelf position
- ✅ Customizable layouts (3x4, 2x5, 4x10, etc.)
- ✅ Three QR density modes: CodeOnly, CodeAndLocation, FullJson
- ✅ Configurable error correction levels (L, M, Q, H)
- ✅ Automatic optimization for ease of scanning
Installation
Prerequisites
- Rust 1.70+ installed on the system
- Cargo (included with Rust)
Adding as a dependency
In your Cargo.toml:
[dependencies]
qr-label-generator = "0.1"
Usage
Single QR code generation
use ;
Batch generation
Create a JSON file with QR code data:
Then invoke in code:
use QrGenerator;
Configuration
The Config structure allows setting default size, error correction level (L/M/Q/H) and foreground/background colors.
Project structure
QRLabel_Generator/
├── Cargo.toml # Project configuration and dependencies
├── README.md # This file
├── src/
│ ├── lib.rs # Library API
│ ├── qr.rs # QR generation logic
│ └── config.rs # Application configuration
└── examples/ # Example files
├── sample_data.json
└── simple.rs
Dependencies
qrcode: For QR code generationimage: For image manipulationanyhow: For error handlingserde: For JSON serialization/deserialization
Examples
Example: Simple QR code
See also examples/sample_data.json to use with QrGenerator::generate_batch.
QR code batch
Create the file examples.json:
Then use QrGenerator::generate_batch in your code.
Development
Running tests
Warehouse items
Generates QR codes for items, including code, description, warehouse and shelf position in the format S001-P001.
use ;
Format code
Batch from JSON
You can generate labels in batch by reading a JSON file with a list of WarehouseItem.
use QrGenerator;
Example file: examples/warehouse_items.json.
CSV
Generic CSV generation
CSV with headers text,filename,size:
text,filename,size
https://www.rust-lang.org,rust_website.png,220
tel:+1234567890,phone_number.png,200
Code:
use QrGenerator;
Warehouse from CSV
CSV with headers code,description,warehouse_location,shelf,shelf_relative_position,size.
use QrGenerator;
📄 Warehouse PDF Label Generation (NEW!)
Generates warehouse item labels in A4 format ready for printing. Each label includes:
- QR Code with configurable density (CodeOnly, CodeAndLocation, or FullJson)
- Item code (in bold)
- Item description
- Warehouse location (format: MAG-A - S001-P001)
Default Layout (3x4 = 12 labels per page)
use PdfLabelGenerator;
Custom Layout
use ;
🎯 QR Code Density Modes (NEW!)
Choose the optimal density level for your QR codes:
1. CodeOnly (Recommended) ⭐⭐⭐⭐⭐
let layout = LabelLayout ;
- Content: Code only (e.g.
ART-0001) - Advantages: Fastest scanning, works with any scanner
- Use when: You always have database access
2. CodeAndLocation ⭐⭐⭐⭐
let layout = LabelLayout ;
- Content:
ART-0001|MAG-A|S001-P001 - Advantages: Includes location info, good compromise
- Use when: Need to verify position quickly
3. FullJson ⭐⭐⭐
let layout = LabelLayout ;
- Content: Complete JSON with all details
- Advantages: Works offline, self-contained
- Use when: System completely offline
📖 Complete guide: See QR_DENSITY_GUIDE.md for details and comparisons.
🧪 Demo: Try all modes with:
Converting HTML to PDF
Generated files are print-ready HTML. To convert them to PDF:
Method 1: Browser (Simplest)
- Open the HTML file in Chrome, Firefox or Safari
- File > Print (or Ctrl+P / Cmd+P)
- Select "Save as PDF" as destination
- Save the PDF
Method 2: Command line (Automatic)
With wkhtmltopdf:
With WeasyPrint:
With Chrome headless:
CSV Format for Labels
code,description,warehouse_location,shelf,shelf_relative_position,size
ART-0001,Phillips screwdriver,MAG-A,1,1,220
ART-0002,Hammer 500g,MAG-A,2,8,200
ART-0003,Adjustable wrench,MAG-A,3,5,210
Complete Example
See examples/warehouse_pdf.rs for a complete example with multiple layouts.
Check code
License
Dual license: MIT or Apache-2.0, at your choice.
- See
LICENSEfor MIT terms - See
LICENSE-APACHEfor Apache-2.0 terms
Contributing
Contributions are welcome! Please:
- Fork the project
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request