main.dart
4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pdf;
import 'package:printing/printing.dart';
import 'document.dart';
import 'viewer.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
final GlobalKey<State<StatefulWidget>> shareWidget = GlobalKey();
final GlobalKey<State<StatefulWidget>> previewContainer = GlobalKey();
Future<void> _printPdf() async {
print('Print ...');
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async =>
(await generateDocument(format)).save());
}
Future<void> _saveAsFile() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
File file = File(appDocPath + '/' + 'document.pdf');
print('Save as file ${file.path} ...');
await file.writeAsBytes((await generateDocument(PdfPageFormat.a4)).save());
Navigator.push<dynamic>(
context,
MaterialPageRoute<dynamic>(
builder: (BuildContext context) => PdfViewer(file: file)),
);
}
Future<void> _sharePdf() async {
print('Share ...');
final pdf.Document document = await generateDocument(PdfPageFormat.a4);
// Calculate the widget center for iPad sharing popup position
final RenderBox referenceBox =
shareWidget.currentContext.findRenderObject();
final Offset topLeft =
referenceBox.localToGlobal(referenceBox.paintBounds.topLeft);
final Offset bottomRight =
referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight);
final Rect bounds = Rect.fromPoints(topLeft, bottomRight);
await Printing.sharePdf(
bytes: document.save(), filename: 'my-résumé.pdf', bounds: bounds);
}
Future<void> _printScreen() async {
final RenderRepaintBoundary boundary =
previewContainer.currentContext.findRenderObject();
final ui.Image im = await boundary.toImage();
final ByteData bytes =
await im.toByteData(format: ui.ImageByteFormat.rawRgba);
print('Print Screen ${im.width}x${im.height} ...');
Printing.layoutPdf(onLayout: (PdfPageFormat format) {
final PdfDocument pdf = PdfDocument(deflate: zlib.encode);
final PdfPage page = PdfPage(pdf, pageFormat: format);
final PdfGraphics g = page.getGraphics();
// Center the image
final double w = page.pageFormat.width -
page.pageFormat.marginLeft -
page.pageFormat.marginRight;
final double h = page.pageFormat.height -
page.pageFormat.marginTop -
page.pageFormat.marginBottom;
double iw, ih;
if (im.width.toDouble() / im.height.toDouble() < 1.0) {
ih = h;
iw = im.width.toDouble() * ih / im.height.toDouble();
} else {
iw = w;
ih = im.height.toDouble() * iw / im.width.toDouble();
}
final PdfImage image = PdfImage(pdf,
image: bytes.buffer.asUint8List(),
width: im.width,
height: im.height);
g.drawImage(
image,
page.pageFormat.marginLeft + (w - iw) / 2.0,
page.pageFormat.height -
page.pageFormat.marginTop -
ih -
(h - ih) / 2.0,
iw,
ih);
return pdf.save();
});
}
@override
Widget build(BuildContext context) {
return RepaintBoundary(
key: previewContainer,
child: Scaffold(
appBar: AppBar(
title: const Text('Pdf Printing Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
child: const Text('Print Document'), onPressed: _printPdf),
RaisedButton(
key: shareWidget,
child: const Text('Share Document'),
onPressed: _sharePdf),
RaisedButton(
child: const Text('Print Screenshot'),
onPressed: _printScreen),
RaisedButton(
child: const Text('Save to file'), onPressed: _saveAsFile),
],
),
),
));
}
}