Showing
1 changed file
with
70 additions
and
24 deletions
@@ -2,29 +2,38 @@ import 'dart:io'; | @@ -2,29 +2,38 @@ import 'dart:io'; | ||
2 | import 'dart:ui'; | 2 | import 'dart:ui'; |
3 | 3 | ||
4 | import 'package:flutter/material.dart'; | 4 | import 'package:flutter/material.dart'; |
5 | +import 'package:flutter/rendering.dart'; | ||
5 | import 'package:pdf/pdf.dart'; | 6 | import 'package:pdf/pdf.dart'; |
6 | import 'package:printing/printing.dart'; | 7 | import 'package:printing/printing.dart'; |
7 | 8 | ||
8 | void main() => runApp(new MaterialApp(home: new MyApp())); | 9 | void main() => runApp(new MaterialApp(home: new MyApp())); |
9 | 10 | ||
10 | -class MyApp extends StatelessWidget { | 11 | +class MyApp extends StatefulWidget { |
12 | + @override | ||
13 | + MyAppState createState() { | ||
14 | + return new MyAppState(); | ||
15 | + } | ||
16 | +} | ||
17 | + | ||
18 | +class MyAppState extends State<MyApp> { | ||
11 | final shareWidget = new GlobalKey(); | 19 | final shareWidget = new GlobalKey(); |
20 | + final previewContainer = new GlobalKey(); | ||
12 | 21 | ||
13 | PDFDocument _generateDocument() { | 22 | PDFDocument _generateDocument() { |
14 | final pdf = new PDFDocument(deflate: zlib.encode); | 23 | final pdf = new PDFDocument(deflate: zlib.encode); |
15 | - final page = new PDFPage(pdf, pageFormat: PDFPageFormat.A4); | 24 | + final page = new PDFPage(pdf, pageFormat: PDFPageFormat.a4); |
16 | final g = page.getGraphics(); | 25 | final g = page.getGraphics(); |
17 | final font = new PDFFont(pdf); | 26 | final font = new PDFFont(pdf); |
18 | final top = page.pageFormat.height; | 27 | final top = page.pageFormat.height; |
19 | 28 | ||
20 | g.setColor(new PDFColor(0.0, 1.0, 1.0)); | 29 | g.setColor(new PDFColor(0.0, 1.0, 1.0)); |
21 | - g.drawRect(50.0 * PDFPageFormat.MM, top - 80.0 * PDFPageFormat.MM, | ||
22 | - 100.0 * PDFPageFormat.MM, 50.0 * PDFPageFormat.MM); | 30 | + g.drawRect(50.0 * PDFPageFormat.mm, top - 80.0 * PDFPageFormat.mm, |
31 | + 100.0 * PDFPageFormat.mm, 50.0 * PDFPageFormat.mm); | ||
23 | g.fillPath(); | 32 | g.fillPath(); |
24 | 33 | ||
25 | g.setColor(new PDFColor(0.3, 0.3, 0.3)); | 34 | g.setColor(new PDFColor(0.3, 0.3, 0.3)); |
26 | - g.drawString(font, 12.0, "Hello World!", 10.0 * PDFPageFormat.MM, | ||
27 | - top - 10.0 * PDFPageFormat.MM); | 35 | + g.drawString(font, 12.0, "Hello World!", 10.0 * PDFPageFormat.mm, |
36 | + top - 10.0 * PDFPageFormat.mm); | ||
28 | 37 | ||
29 | return pdf; | 38 | return pdf; |
30 | } | 39 | } |
@@ -51,25 +60,62 @@ class MyApp extends StatelessWidget { | @@ -51,25 +60,62 @@ class MyApp extends StatelessWidget { | ||
51 | Printing.sharePdf(document: pdf, bounds: bounds); | 60 | Printing.sharePdf(document: pdf, bounds: bounds); |
52 | } | 61 | } |
53 | 62 | ||
63 | + Future<void> _printScreen() async { | ||
64 | + const margin = 10.0 * PDFPageFormat.mm; | ||
65 | + final pdf = new PDFDocument(deflate: zlib.encode); | ||
66 | + final page = new PDFPage(pdf, pageFormat: PDFPageFormat.a4); | ||
67 | + final g = page.getGraphics(); | ||
68 | + | ||
69 | + RenderRepaintBoundary boundary = | ||
70 | + previewContainer.currentContext.findRenderObject(); | ||
71 | + final im = await boundary.toImage(); | ||
72 | + final bytes = await im.toByteData(format: ImageByteFormat.rawRgba); | ||
73 | + print("Print Screen ${im.width}x${im.height} ..."); | ||
74 | + | ||
75 | + // Center the image | ||
76 | + final w = page.pageFormat.width - margin * 2.0; | ||
77 | + final h = page.pageFormat.height - margin * 2.0; | ||
78 | + double iw, ih; | ||
79 | + if (im.width.toDouble() / im.height.toDouble() < 1.0) { | ||
80 | + ih = h; | ||
81 | + iw = im.width.toDouble() * ih / im.height.toDouble(); | ||
82 | + } else { | ||
83 | + iw = w; | ||
84 | + ih = im.height.toDouble() * iw / im.width.toDouble(); | ||
85 | + } | ||
86 | + | ||
87 | + PDFImage image = PDFImage(pdf, | ||
88 | + image: bytes.buffer.asUint8List(), width: im.width, height: im.height); | ||
89 | + g.drawImage(image, margin + (w - iw) / 2.0, | ||
90 | + page.pageFormat.height - margin - ih - (h - ih) / 2.0, iw, ih); | ||
91 | + | ||
92 | + Printing.printPdf(document: pdf); | ||
93 | + } | ||
94 | + | ||
54 | @override | 95 | @override |
55 | Widget build(BuildContext context) { | 96 | Widget build(BuildContext context) { |
56 | - return new Scaffold( | ||
57 | - appBar: new AppBar( | ||
58 | - title: const Text('Pdf Printing Example'), | ||
59 | - ), | ||
60 | - body: new Center( | ||
61 | - child: new Column( | ||
62 | - mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
63 | - children: <Widget>[ | ||
64 | - new RaisedButton( | ||
65 | - child: new Text('Print Document'), onPressed: _printPdf), | ||
66 | - new RaisedButton( | ||
67 | - key: shareWidget, | ||
68 | - child: new Text('Share Document'), | ||
69 | - onPressed: _sharePdf), | ||
70 | - ], | ||
71 | - ), | ||
72 | - ), | ||
73 | - ); | 97 | + return new RepaintBoundary( |
98 | + key: previewContainer, | ||
99 | + child: new Scaffold( | ||
100 | + appBar: new AppBar( | ||
101 | + title: const Text('Pdf Printing Example'), | ||
102 | + ), | ||
103 | + body: new Center( | ||
104 | + child: new Column( | ||
105 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
106 | + children: <Widget>[ | ||
107 | + new RaisedButton( | ||
108 | + child: new Text('Print Document'), onPressed: _printPdf), | ||
109 | + new RaisedButton( | ||
110 | + key: shareWidget, | ||
111 | + child: new Text('Share Document'), | ||
112 | + onPressed: _sharePdf), | ||
113 | + new RaisedButton( | ||
114 | + child: new Text('Print Screenshot'), | ||
115 | + onPressed: _printScreen), | ||
116 | + ], | ||
117 | + ), | ||
118 | + ), | ||
119 | + )); | ||
74 | } | 120 | } |
75 | } | 121 | } |
-
Please register or login to post a comment