Last active
April 23, 2022 07:27
-
-
Save DavBfr/a25fe7001555818fc1eab04bbc0648a1 to your computer and use it in GitHub Desktop.
This file contains 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:pdf/pdf.dart'; | |
import 'package:pdf/widgets.dart' as pw; | |
import 'package:printing/printing.dart'; | |
Future<void> main() async { | |
runApp(const MyApp('Printing Demo')); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp(this.title, {Key? key}) : super(key: key); | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: Text(title)), | |
body: PdfPreview( | |
build: (format) => _generatePdf(format, title), | |
maxPageWidth: 400, | |
), | |
), | |
); | |
} | |
Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async { | |
final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true); | |
const col = [ | |
['S.No', 10, pw.Alignment.centerLeft], | |
['Description', 30, pw.Alignment.centerLeft], | |
['MRP', 10, pw.Alignment.centerRight], | |
['Rate', 10, pw.Alignment.centerRight], | |
['Qty', 8, pw.Alignment.center], | |
['Amount', 13, pw.Alignment.centerRight], | |
]; | |
const data = [ | |
['1', 'ANNAPORNA PEPPER CHICKEN 40G', '35', '23', '34', '782'], | |
['2', 'HAND GLOVES PLASTIC', '30', '20', '3', '60'], | |
['3', '0.7 Cello Arc', '15', '14', '2', '28'], | |
['4', '1.5L Maaza', '70', '70', '3', '210'], | |
['5', '10 Ml Vapo Rub', '32', '30', '4', '120'], | |
['6', '1000Ml Auto Car Wash Shampoo', '199', '190', '3', '570'], | |
['7', '1000Ml Maid Dishwash', '179', '170', '3', '510'], | |
['8', '100G Lime', '20', '18', '6', '108'], | |
['9', '100G AVT Leaf Tea', '45', '43', '3', '129'], | |
]; | |
pdf.addPage( | |
pw.Page( | |
pageTheme: pw.PageTheme( | |
pageFormat: const PdfPageFormat( | |
4 * PdfPageFormat.inch, | |
double.infinity, | |
marginBottom: 1 * PdfPageFormat.inch, | |
marginLeft: 5 * PdfPageFormat.mm, | |
marginRight: 5 * PdfPageFormat.mm, | |
marginTop: 2 * PdfPageFormat.mm, | |
), | |
theme: pw.ThemeData.withFont( | |
base: await PdfGoogleFonts.robotoLight(), | |
bold: await PdfGoogleFonts.robotoBold(), | |
).copyWith( | |
defaultTextStyle: const pw.TextStyle( | |
fontSize: 12, | |
lineSpacing: 1.2, | |
), | |
header0: pw.TextStyle( | |
fontSize: 16, | |
lineSpacing: 1.2, | |
fontWeight: pw.FontWeight.bold, | |
), | |
), | |
), | |
build: (context) { | |
return pw.Column( | |
children: [ | |
pw.Text( | |
'SUPER DEPARTMENTAL STORE', | |
textAlign: pw.TextAlign.center, | |
style: pw.Theme.of(context).header0, | |
), | |
pw.SizedBox(height: 6), | |
pw.Text( | |
'245 Varet St, Brooklyn\nPhone: 1-555-0964', | |
textAlign: pw.TextAlign.center, | |
), | |
pw.SizedBox(height: 6), | |
pw.Text( | |
'COUNTER SALE', | |
textAlign: pw.TextAlign.center, | |
style: pw.Theme.of(context).header0, | |
), | |
pw.SizedBox(height: 6), | |
pw.Row( | |
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, | |
children: [ | |
pw.Text( | |
'Date : 27-Nov-2021', | |
), | |
pw.Text( | |
'Time : 08:54 PM', | |
), | |
], | |
), | |
pw.SizedBox(height: 6), | |
pw.Align( | |
alignment: pw.Alignment.centerLeft, | |
child: pw.Text( | |
'Invoice No. : 5', | |
), | |
), | |
pw.Divider(), | |
pw.Row( | |
mainAxisSize: pw.MainAxisSize.max, | |
children: [ | |
for (final c in col) | |
pw.Flexible( | |
flex: c[1] as int, | |
child: pw.Align( | |
alignment: c[2] as pw.Alignment, | |
child: pw.Text( | |
c[0] as String, | |
), | |
), | |
), | |
], | |
), | |
pw.Divider(), | |
for (final d in data) ...[ | |
pw.Row( | |
mainAxisSize: pw.MainAxisSize.max, | |
children: [ | |
for (var i = 0; i < 2; i++) | |
pw.Flexible( | |
flex: i == 1 | |
? col | |
.sublist(1) | |
.map((e) => e[1] as int) | |
.reduce((v, r) => v + r) | |
: col[i][1] as int, | |
child: pw.Align( | |
alignment: col[i][2] as pw.Alignment, | |
child: pw.Text( | |
d[i], | |
), | |
), | |
), | |
], | |
), | |
pw.SizedBox(height: 4), | |
pw.Row( | |
mainAxisSize: pw.MainAxisSize.max, | |
children: [ | |
for (var i = 1; i < col.length; i++) | |
pw.Flexible( | |
flex: i == 1 | |
? col | |
.sublist(0, 2) | |
.map((e) => e[1] as int) | |
.reduce((v, r) => v + r) | |
: col[i][1] as int, | |
child: pw.Align( | |
alignment: col[i][2] as pw.Alignment, | |
child: pw.Text( | |
i == 1 ? '' : d[i], | |
), | |
), | |
), | |
], | |
), | |
pw.SizedBox(height: 4), | |
], | |
pw.Divider(), | |
pw.Align( | |
alignment: pw.Alignment.centerRight, | |
child: pw.Text( | |
'NET AMOUNT : 2517', | |
style: pw.TextStyle(fontWeight: pw.FontWeight.bold), | |
), | |
), | |
], | |
); | |
}, | |
), | |
); | |
return pdf.save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment