Skip to content

Instantly share code, notes, and snippets.

@DavBfr
Last active January 16, 2025 04:50
Show Gist options
  • Save DavBfr/bbe8ba482b1a9101628d8b0d83888517 to your computer and use it in GitHub Desktop.
Save DavBfr/bbe8ba482b1a9101628d8b0d83888517 to your computer and use it in GitHub Desktop.
esc_pos_printer with pdf/printing
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_esc_pos_network/flutter_esc_pos_network.dart';
import 'package:flutter_esc_pos_utils/flutter_esc_pos_utils.dart';
import 'package:image/image.dart' as im;
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pdf;
import 'package:printing/printing.dart';
extension PdfRasterExt on PdfRaster {
im.Image asImage() {
return im.Image.fromBytes(width, height, pixels);
}
}
const printerIp = '192.168.0.123';
const printerDpi = 203.0;
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Test'),
),
body: PdfPreview(
build: (_) => _buildDocument(),
allowPrinting: false,
canChangeOrientation: false,
canChangePageFormat: false,
actions: [
PdfPreviewAction(
icon: const Icon(Icons.print),
onPressed: (context, build, pageFormat) => _print(),
)
],
),
),
);
}
Future<Uint8List> _buildDocument() async {
final doc = pdf.Document();
doc.addPage(
pdf.Page(
orientation: pdf.PageOrientation.landscape,
pageFormat: PdfPageFormat.roll80,
build: (context) => pdf.Center(
child: pdf.Text(
'Hello World',
style: pdf.TextStyle(
fontWeight: pdf.FontWeight.bold,
fontSize: 20,
),
),
),
),
);
doc.addPage(
pdf.Page(
pageFormat: PdfPageFormat.roll80,
build: (context) => pdf.Center(
child: pdf.PdfLogo(),
),
),
);
return await doc.save();
}
Future<void> _print() async {
final printer = PrinterNetworkManager(printerIp);
final res = await printer.connect();
if (res != PosPrintResult.success) {
throw Exception('Unable to connect to the printer');
}
final profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm80, profile);
var ticket = <int>[];
await for (var page
in Printing.raster(await _buildDocument(), dpi: printerDpi)) {
final image = page.asImage();
ticket += generator.image(image);
ticket += generator.feed(2);
ticket += generator.cut();
}
printer.printTicket(ticket);
printer.disconnect();
}
}
name: print_pos
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.12.0-0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_esc_pos_utils:
flutter_esc_pos_network:
printing:
pdf:
flutter:
uses-material-design: true
@khairulTonu
Copy link

khairulTonu commented Dec 19, 2022

I have tried it. Image is printing perfectly. But when I am printing any text, it's just printing a blank page. I have tried with multiple fonts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment