Showing
4 changed files
with
104 additions
and
14 deletions
@@ -69,6 +69,15 @@ class PdfPageFormat { | @@ -69,6 +69,15 @@ class PdfPageFormat { | ||
69 | PdfPageFormat get portrait => | 69 | PdfPageFormat get portrait => |
70 | height >= width ? this : copyWith(width: height, height: width); | 70 | height >= width ? this : copyWith(width: height, height: width); |
71 | 71 | ||
72 | + PdfPageFormat applyMargin( | ||
73 | + {double left, double top, double right, double bottom}) => | ||
74 | + copyWith( | ||
75 | + marginLeft: math.max(marginLeft, left), | ||
76 | + marginTop: math.max(marginTop, top), | ||
77 | + marginRight: math.max(marginRight, right), | ||
78 | + marginBottom: math.max(marginBottom, bottom), | ||
79 | + ); | ||
80 | + | ||
72 | @override | 81 | @override |
73 | String toString() { | 82 | String toString() { |
74 | return "${width}x$height"; | 83 | return "${width}x$height"; |
@@ -3,21 +3,90 @@ import 'dart:async'; | @@ -3,21 +3,90 @@ import 'dart:async'; | ||
3 | 3 | ||
4 | import 'package:pdf/pdf.dart'; | 4 | import 'package:pdf/pdf.dart'; |
5 | 5 | ||
6 | -Future<PdfDocument> generateDocument() async { | 6 | +Future<PdfDocument> generateDocument(PdfPageFormat format) async { |
7 | final pdf = PdfDocument(deflate: zlib.encode); | 7 | final pdf = PdfDocument(deflate: zlib.encode); |
8 | - final page = PdfPage(pdf, pageFormat: PdfPageFormat.a4); | 8 | + final page = PdfPage(pdf, |
9 | + pageFormat: format.applyMargin( | ||
10 | + left: 2.0 * PdfPageFormat.cm, | ||
11 | + top: 2.0 * PdfPageFormat.cm, | ||
12 | + right: 2.0 * PdfPageFormat.cm, | ||
13 | + bottom: 2.0 * PdfPageFormat.cm)); | ||
9 | final g = page.getGraphics(); | 14 | final g = page.getGraphics(); |
10 | final font = PdfFont.helvetica(pdf); | 15 | final font = PdfFont.helvetica(pdf); |
11 | - final top = page.pageFormat.height; | 16 | + final top = page.pageFormat.height - page.pageFormat.marginTop; |
17 | + | ||
18 | + g.setColor(PdfColor.orange); | ||
19 | + g.drawRect( | ||
20 | + page.pageFormat.marginLeft, | ||
21 | + page.pageFormat.marginBottom, | ||
22 | + page.pageFormat.width - | ||
23 | + page.pageFormat.marginRight - | ||
24 | + page.pageFormat.marginLeft, | ||
25 | + page.pageFormat.height - | ||
26 | + page.pageFormat.marginTop - | ||
27 | + page.pageFormat.marginBottom); | ||
28 | + g.strokePath(); | ||
12 | 29 | ||
13 | g.setColor(PdfColor(0.0, 1.0, 1.0)); | 30 | g.setColor(PdfColor(0.0, 1.0, 1.0)); |
14 | - g.drawRect(50.0 * PdfPageFormat.mm, top - 80.0 * PdfPageFormat.mm, | ||
15 | - 100.0 * PdfPageFormat.mm, 50.0 * PdfPageFormat.mm); | 31 | + g.drawRRect( |
32 | + 50.0 * PdfPageFormat.mm, | ||
33 | + top - 80.0 * PdfPageFormat.mm, | ||
34 | + 100.0 * PdfPageFormat.mm, | ||
35 | + 50.0 * PdfPageFormat.mm, | ||
36 | + 20.0, | ||
37 | + 20.0, | ||
38 | + ); | ||
16 | g.fillPath(); | 39 | g.fillPath(); |
17 | 40 | ||
18 | g.setColor(PdfColor(0.3, 0.3, 0.3)); | 41 | g.setColor(PdfColor(0.3, 0.3, 0.3)); |
19 | - g.drawString(font, 12.0, "Hello World!", 10.0 * PdfPageFormat.mm, | 42 | + g.drawString( |
43 | + font, | ||
44 | + 12.0, | ||
45 | + "Hello World!", | ||
46 | + page.pageFormat.marginLeft + 10.0 * PdfPageFormat.mm, | ||
20 | top - 10.0 * PdfPageFormat.mm); | 47 | top - 10.0 * PdfPageFormat.mm); |
21 | 48 | ||
49 | + { | ||
50 | + final page = PdfPage(pdf, | ||
51 | + pageFormat: format.applyMargin( | ||
52 | + left: 2.0 * PdfPageFormat.cm, | ||
53 | + top: 2.0 * PdfPageFormat.cm, | ||
54 | + right: 2.0 * PdfPageFormat.cm, | ||
55 | + bottom: 2.0 * PdfPageFormat.cm)); | ||
56 | + final g = page.getGraphics(); | ||
57 | + final font = PdfFont.helvetica(pdf); | ||
58 | + final top = page.pageFormat.height - page.pageFormat.marginTop; | ||
59 | + | ||
60 | + g.setColor(PdfColor.orange); | ||
61 | + g.drawRect( | ||
62 | + page.pageFormat.marginLeft, | ||
63 | + page.pageFormat.marginBottom, | ||
64 | + page.pageFormat.width - | ||
65 | + page.pageFormat.marginRight - | ||
66 | + page.pageFormat.marginLeft, | ||
67 | + page.pageFormat.height - | ||
68 | + page.pageFormat.marginTop - | ||
69 | + page.pageFormat.marginBottom); | ||
70 | + g.strokePath(); | ||
71 | + | ||
72 | + g.setColor(PdfColor(0.0, 1.0, 1.0)); | ||
73 | + g.drawRRect( | ||
74 | + 50.0 * PdfPageFormat.mm, | ||
75 | + top - 80.0 * PdfPageFormat.mm, | ||
76 | + 100.0 * PdfPageFormat.mm, | ||
77 | + 50.0 * PdfPageFormat.mm, | ||
78 | + 20.0, | ||
79 | + 20.0, | ||
80 | + ); | ||
81 | + g.fillPath(); | ||
82 | + | ||
83 | + g.setColor(PdfColor(0.3, 0.3, 0.3)); | ||
84 | + g.drawString( | ||
85 | + font, | ||
86 | + 12.0, | ||
87 | + "Hello World!", | ||
88 | + page.pageFormat.marginLeft + 10.0 * PdfPageFormat.mm, | ||
89 | + top - 10.0 * PdfPageFormat.mm); | ||
90 | + } | ||
22 | return pdf; | 91 | return pdf; |
23 | } | 92 | } |
@@ -25,13 +25,13 @@ class MyAppState extends State<MyApp> { | @@ -25,13 +25,13 @@ class MyAppState extends State<MyApp> { | ||
25 | 25 | ||
26 | void _printPdf() async { | 26 | void _printPdf() async { |
27 | print("Print ..."); | 27 | print("Print ..."); |
28 | - final pdf = await generateDocument(); | 28 | + final pdf = await generateDocument(PdfPageFormat.a4); |
29 | Printing.printPdf(document: pdf); | 29 | Printing.printPdf(document: pdf); |
30 | } | 30 | } |
31 | 31 | ||
32 | void _sharePdf() async { | 32 | void _sharePdf() async { |
33 | print("Share ..."); | 33 | print("Share ..."); |
34 | - final pdf = await generateDocument(); | 34 | + final pdf = await generateDocument(PdfPageFormat.a4); |
35 | 35 | ||
36 | // Calculate the widget center for iPad sharing popup position | 36 | // Calculate the widget center for iPad sharing popup position |
37 | final RenderBox referenceBox = | 37 | final RenderBox referenceBox = |
@@ -46,7 +46,6 @@ class MyAppState extends State<MyApp> { | @@ -46,7 +46,6 @@ class MyAppState extends State<MyApp> { | ||
46 | } | 46 | } |
47 | 47 | ||
48 | Future<void> _printScreen() async { | 48 | Future<void> _printScreen() async { |
49 | - const margin = 10.0 * PdfPageFormat.mm; | ||
50 | final pdf = PdfDocument(deflate: zlib.encode); | 49 | final pdf = PdfDocument(deflate: zlib.encode); |
51 | final page = PdfPage(pdf, pageFormat: PdfPageFormat.a4); | 50 | final page = PdfPage(pdf, pageFormat: PdfPageFormat.a4); |
52 | final g = page.getGraphics(); | 51 | final g = page.getGraphics(); |
@@ -58,8 +57,12 @@ class MyAppState extends State<MyApp> { | @@ -58,8 +57,12 @@ class MyAppState extends State<MyApp> { | ||
58 | print("Print Screen ${im.width}x${im.height} ..."); | 57 | print("Print Screen ${im.width}x${im.height} ..."); |
59 | 58 | ||
60 | // Center the image | 59 | // Center the image |
61 | - final w = page.pageFormat.width - margin * 2.0; | ||
62 | - final h = page.pageFormat.height - margin * 2.0; | 60 | + final w = page.pageFormat.width - |
61 | + page.pageFormat.marginLeft - | ||
62 | + page.pageFormat.marginRight; | ||
63 | + final h = page.pageFormat.height - | ||
64 | + page.pageFormat.marginTop - | ||
65 | + page.pageFormat.marginBottom; | ||
63 | double iw, ih; | 66 | double iw, ih; |
64 | if (im.width.toDouble() / im.height.toDouble() < 1.0) { | 67 | if (im.width.toDouble() / im.height.toDouble() < 1.0) { |
65 | ih = h; | 68 | ih = h; |
@@ -71,8 +74,15 @@ class MyAppState extends State<MyApp> { | @@ -71,8 +74,15 @@ class MyAppState extends State<MyApp> { | ||
71 | 74 | ||
72 | PdfImage image = PdfImage(pdf, | 75 | PdfImage image = PdfImage(pdf, |
73 | image: bytes.buffer.asUint8List(), width: im.width, height: im.height); | 76 | image: bytes.buffer.asUint8List(), width: im.width, height: im.height); |
74 | - g.drawImage(image, margin + (w - iw) / 2.0, | ||
75 | - page.pageFormat.height - margin - ih - (h - ih) / 2.0, iw, ih); | 77 | + g.drawImage( |
78 | + image, | ||
79 | + page.pageFormat.marginLeft + (w - iw) / 2.0, | ||
80 | + page.pageFormat.height - | ||
81 | + page.pageFormat.marginTop - | ||
82 | + ih - | ||
83 | + (h - ih) / 2.0, | ||
84 | + iw, | ||
85 | + ih); | ||
76 | 86 | ||
77 | Printing.printPdf(document: pdf); | 87 | Printing.printPdf(document: pdf); |
78 | } | 88 | } |
@@ -2,11 +2,13 @@ import 'dart:io'; | @@ -2,11 +2,13 @@ import 'dart:io'; | ||
2 | 2 | ||
3 | import 'package:flutter_test/flutter_test.dart'; | 3 | import 'package:flutter_test/flutter_test.dart'; |
4 | 4 | ||
5 | +import 'package:pdf/pdf.dart'; | ||
6 | + | ||
5 | import 'package:printing_example/document.dart'; | 7 | import 'package:printing_example/document.dart'; |
6 | 8 | ||
7 | void main() { | 9 | void main() { |
8 | testWidgets('Generate the Pdf document', (WidgetTester tester) async { | 10 | testWidgets('Generate the Pdf document', (WidgetTester tester) async { |
9 | - final pdf = await generateDocument(); | 11 | + final pdf = await generateDocument(PdfPageFormat.a4); |
10 | var file = File('document.pdf'); | 12 | var file = File('document.pdf'); |
11 | file.writeAsBytesSync(pdf.save()); | 13 | file.writeAsBytesSync(pdf.save()); |
12 | }); | 14 | }); |
-
Please register or login to post a comment