-
-
Save DavBfr/bbe8ba482b1a9101628d8b0d83888517 to your computer and use it in GitHub Desktop.
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 |
To print a PDF, you need a printer that can print graphics. Most can, but you also need to know how to send pictures. This flutter_esc_pos* package can communicate to ESC/POS printers, but not all of them are compatible.
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();
pdf1.addPage(
pdf.Page(
orientation: pdf.PageOrientation.landscape,
pageFormat: PdfPageFormat.roll80,
build: (context) => pdf.Center(
child: pdf.Text(
'KARTHI',
style: pdf.TextStyle(
fontWeight: pdf.FontWeight.bold,
fontSize: 20,
),
),
),
),
);
return pdf1.save();
}
Future _buildDocument() async {
final doc = pdf.Document();
doc.addPage(
pdf.Page(
orientation: pdf.PageOrientation.landscape,
pageFormat: PdfPageFormat.roll80,
build: (context) => pdf.Center(
child: pdf.Text(
'KARTHI',
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 printTicket() async {
final printer = PrinterNetworkManager('192.168.0.239');
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 _generateTicket(), dpi: printerDpi)) {
final image = page.asImage();
ticket += generator.image(image);
ticket += generator.feed(1);
ticket += generator.cut();
}
printer.printTicket(ticket);
printer.disconnect();
}
/*Future _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(1);
ticket += generator.cut();
}
printer.printTicket(ticket);
printer.disconnect();
}*/
}
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.
Hello ,
I write as same this example but I have a problem the printer only text but pdf not print.