Skip to content

Instantly share code, notes, and snippets.

@dafinoer
Created December 21, 2021 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dafinoer/fca4e32d9869931d878b8bb5dbc25dee to your computer and use it in GitHub Desktop.
Save dafinoer/fca4e32d9869931d878b8bb5dbc25dee to your computer and use it in GitHub Desktop.
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