fajfield_print_flutter
Print receipts and labels from Flutter on any thermal printer, at any paper width, in any language, offline, from one template.
This is the Flutter package of fajfield_print. It includes everything in the core package and adds:
- Real fonts:
FlutterTextRendererdraws text with the device's fonts, so Malayalam, Hindi, Tamil, Arabic and other scripts print correctly. FajPreview: shows exactly what will print.FajDebugPanel: preview, warnings, full log and bytes on one debug screen.
Samples, templates and docs: fajfield.com/developers/flutter
Why
- Any width: 58, 80, 104/110/112 mm, or any number of dots, not just 58 and 80 mm.
- Any printer language: ESC/POS (receipt printers), TSPL (TSC and most low-cost label printers), ZPL (Zebra).
- Any language of text: everything is sent to the printer as a picture, not with printer fonts.
- Templates, not code: design in the Fajfield designer, download the JSON, print it. Change the layout without releasing a new app.
- Receipts that grow: tables repeat per item, and everything below moves down.
- Never in the dark: logs at every step, errors with a code and a hint, a debug preview, a printer check and a test page.
Install
dependencies:
fajfield_print_flutter: ^0.1.0
One import gives you everything, including the core package:
import 'package:fajfield_print_flutter/fajfield_print_flutter.dart';
Android: network printing needs internet permission in android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
Print a receipt
final printer = FajPrinter.network(
'192.168.1.50',
paper: Paper.mm80,
text: FlutterTextRenderer(), // real fonts: any language
);
final template = PrintTemplate.fromJsonString(
await rootBundle.loadString('assets/receipt-80mm.fajfield.json'),
);
final job = await printer.print(template, data: {
'Shop': 'Fresh Mart',
'Total': '735.00',
'items': [
{'name': 'Tea powder 250 g', 'qty': '2', 'amount': '180.00'},
{'name': 'Coconut oil 1 L', 'qty': '1', 'amount': '245.00'},
],
});
Get a template: open the Fajfield designer, change it as you like, then go to Print / Export → For developers → Download template (JSON). Add the file to your app's assets/ folder, or load it from your server.
Your own font: bundle a font in pubspec.yaml and pass FlutterTextRenderer(fontFamily: 'NotoSansMalayalam'). Text too thin or too bold? Adjust threshold: (0–255).
Connecting
| Printer on | Code |
|---|---|
| Wi-Fi / LAN | FajPrinter.network('192.168.1.50') (port 9100; set port: if different) |
| Bluetooth / USB | FajPrinter.custom((bytes) => yourPlugin.write(bytes)): one line with any plugin |
| A file (.prn) | FajPrinter.file('label.prn') |
| Nothing (testing) | FajPrinter.dryRun() |
Find printers on the network: await discoverPrinters() returns every device answering on port 9100.
Bluetooth tip: many Bluetooth printers lose data sent in one big write. Send in chunks:
FajPrinter.custom(
(b) => characteristic.write(b, withoutResponse: true),
name: 'BT TVS RP3200',
chunkSize: 180,
chunkDelay: const Duration(milliseconds: 20),
text: FlutterTextRenderer(),
);
Pick the language: language: PrinterLanguage.escpos (default, receipt printers), .tspl or .zpl (label printers).
Paper width
Paper.mm58 // 384 dots
Paper.mm80 // 576 dots
Paper.mm110 // 832 dots (also mm104, mm112)
Paper.mm(100) // any width in mm
Paper.dots(800) // exact printable dots from the spec sheet
Not sure how wide your printer really prints? await printer.print(printer.testPage()) prints a mm ruler across the full width. Wherever the ruler stops is your real width.
Preview and debug screen
// Before printing: see exactly what will print
final r = await printer.preview(template, data: data);
FajPreview(result: r, debug: true); // red outline around every element
// After printing: everything about the job on one screen
FajDebugPanel(job: job);
Debugging: what happened and why
1. Logs. Every step (render, encode, connect, send, status) is logged with timings.
final log = PrintLogger.console(LogLevel.debug); // print everything
// or send to your own logger:
// PrintLogger(level: LogLevel.info, onLog: (e) => myLogger.log('$e'));
FajPrinter.network('192.168.1.50', logger: log, text: FlutterTextRenderer());
[fajfield 11:02:41.120 INFO connect] connected host=192.168.1.50 port=9100 ms=38
[fajfield 11:02:41.301 WARN template] No value for {{Total}}
→ Add "Total" to the data you pass to print(), or remove it from the template.
[fajfield 11:02:41.410 INFO send] sent bytes=18422 ms=109 to=network 192.168.1.50:9100
2. Errors you can act on. Every PrintException has a code, a message and a hint:
try {
await printer.print(template, data: data);
} on PrintException catch (e) {
showError('${e.message}\n${e.hint}');
}
| code | example hint |
|---|---|
connectTimeout |
Printer off, or not on the same Wi-Fi. Print the self-test page to see its IP. |
connectRefused |
Wrong port. Most use 9100; some use 9101, 6101 or 515. |
hostUnreachable |
Phone is on mobile data or a guest Wi-Fi. |
tooWide |
Template is 832 dots but paper is 576. Use the right Paper. |
writeFailed |
Connection dropped. For Bluetooth use chunkSize. |
barcodeInvalid |
EAN-13 needs 12 digits (the check digit is added). |
3. Warnings that don't stop printing: missing {{fields}}, things off the edge of the paper, text cut short, bad barcode data. They're in job.warnings and in the log.
4. The job report: debugPrint(job.report()) gives the printer, page size, bytes, render/encode/send times, warnings and log. Attach it to bug reports.
5. Printer check: debugPrint(await printer.diagnose(printTest: true))
Fajfield printer check: network 192.168.1.50:9100, escpos, 80mm
connection: OK
status: out of paper
→ Fix the printer (paper, cover, pause button) and try again.
6. See the bytes: job.hex() shows hex and ASCII, and FajPrinter.file() saves exactly what would be sent.
Templates in code
You can also build templates without the designer:
final t = PrintTemplate.label(widthMm: 50, heightMm: 25)
..text('{{name}}', x: 10, y: 8, size: 28, bold: true)
..text('MRP ₹{{mrp}}', x: 10, y: 44, size: 24)
..barcode('{{sku}}', x: 10, y: 80, height: 70);
Elements: text, box, ellipse, line, barcode (code128, ean13, ean8, upca, code39, i2of5, gs1-128), qr, datamatrix, image, invert, table. Positions are in printer dots (8 dots = 1 mm at 203 dpi).
Status
0.1.0, first version. Please report issues on GitHub with job.report() attached.
Made by Fajfield: research and development for ERP, warehouse, POS and business data.
Libraries
- fajfield_print_flutter
- Flutter add-on: real fonts for every script, and widgets to preview and debug prints.