3.1 KiB
3.1 KiB
QR Code Integration for Invoice System
Overview
This implementation adds QR code generation functionality to the invoice system, specifically for Trust Fund Request documents (type 2) using the main2_2.html template.
Features
- Automatic QR Code Generation: When a Trust Fund Request (type 2) is generated, the system automatically creates a QR code from the
lhdn_qrcodefield - Base64 Encoding: QR codes are converted to base64 format and embedded directly in the HTML
- Responsive Design: QR codes are displayed with appropriate sizing and styling
- Error Handling: Graceful fallback if QR code generation fails
Technical Implementation
Dependencies Added
qrcode==7.4.2- For generating QR codesPillow- Already available, used for image processing
Code Changes
1. billing/models.py
- Added imports for
qrcode,base64, andBytesIO - Modified
BillHtml()method to generate QR codes for type 2 documents - QR codes are generated with the following specifications:
- Version: 1
- Error correction: L (Low)
- Box size: 10
- Border: 4
- Colors: Black on white
2. assets/invoices/main2_2.html
- Added QR code display section at the bottom of the template
- Includes styling and title for the QR code section
- Uses
{bill_qr_code}placeholder for dynamic content
How It Works
- Detection: When
BillHtml(temType)is called with type 2 (Trust Fund Request) - QR Generation: If
lhdn_qrcodefield contains data, a QR code is generated - Base64 Conversion: The QR code image is converted to base64 format
- HTML Embedding: The base64 data is embedded as an HTML img tag
- Template Rendering: The HTML template is rendered with the QR code included
QR Code Specifications
- Format: PNG
- Size: 150px width, auto height, max-width 200px
- Border: 1px solid #ddd
- Error Correction: Low (7% recovery)
- Data Capacity: Suitable for URLs, text, and small data
Usage
For Developers
- Ensure the
lhdn_qrcodefield contains the data you want to encode - Call
BillHtml(temType)on a type 2 Bill object - The QR code will be automatically generated and included in the HTML
For Users
- Create a Trust Fund Request (type 2) with data in the
lhdn_qrcodefield - Generate the HTML invoice using the existing workflow
- The QR code will appear at the bottom of the generated document
Error Handling
- If QR code generation fails, an empty string is returned
- Errors are logged to the console for debugging
- The invoice generation continues normally even if QR code fails
Testing
A test script test_qr.py is provided to verify QR code functionality:
python test_qr.py
Future Enhancements
- Customizable QR code colors and sizes
- Support for different QR code formats (SVG, etc.)
- QR code positioning options in templates
- Batch QR code generation for multiple documents
Notes
- QR codes are only generated for Trust Fund Request documents (type 2)
- The system uses the existing
lhdn_qrcodefield as the data source - Base64 encoding ensures the QR code is self-contained in the HTML
- No external image files are required