Created
December 21, 2021 10:52
-
-
Save dafinoer/fca4e32d9869931d878b8bb5dbc25dee 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
class PdfUtil { | |
final doc = pdf.Document(); | |
final PdfPageFormat? formatPage; | |
PdfUtil(this.formatPage); | |
Future<Uint8List> buildPdf(pdf.Widget widget) { | |
doc.addPage(pdf.Page( | |
build: (pdf.Context context) => widget, | |
pageFormat: formatPage, | |
)); | |
return doc.save(); | |
} | |
Future<void> printLayoutPdf(pdf.Widget widget) async { | |
await Printing.layoutPdf(onLayout: (format) => buildPdf(widget)); | |
} | |
} | |
//example | |
void main() { | |
final pdf = PdfUtil(PdfPageFormat.a3); | |
final widget = pw.Center( | |
child: pw.Text('Hello World', style: pw.TextStyle(font: ttf, fontSize: 40)), | |
); | |
await pdf.buildPdf(widget); | |
//if u want printing | |
await pdf.printLayoutPdf(widget); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment