Last active
January 16, 2025 04:50
-
-
Save DavBfr/bbe8ba482b1a9101628d8b0d83888517 to your computer and use it in GitHub Desktop.
esc_pos_printer with pdf/printing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Hi, Here I am using Epson - TMT-T88V model.
import 'package:flutter/material.dart';
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.239';
const printerDpi = 50.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 {
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Test'),
),
body: PdfPreview(
build: () => _buildDocument(),
allowPrinting: false,
canChangePageFormat: false,
actions: [
PdfPreviewAction(
icon: const Icon(Icons.print),
onPressed: (context, build, pageFormat) => printTicket(),
)
],
),
),
);
}
Future _generateTicket() async {
final pdf1 = pdf.Document();
}
Future _buildDocument() async {
final doc = pdf.Document();
/* doc.addPage(
pdf.Page(
pageFormat: PdfPageFormat.roll80,
build: (context) => pdf.Center(
child: pdf.PdfLogo(),
),
),
);*/
}
Future printTicket() async {
final printer = PrinterNetworkManager('192.168.0.239');
final res = await printer.connect();
}
/*Future _print() async {
final printer = PrinterNetworkManager(printerIp);
final res = await printer.connect();
}*/
}