Showing
3 changed files
with
121 additions
and
38 deletions
@@ -14,48 +14,15 @@ | @@ -14,48 +14,15 @@ | ||
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | +library printing; | ||
18 | + | ||
17 | import 'dart:async'; | 19 | import 'dart:async'; |
18 | import 'dart:typed_data'; | 20 | import 'dart:typed_data'; |
19 | -import 'dart:ui'; | 21 | +import 'dart:ui' as ui; |
20 | 22 | ||
21 | import 'package:flutter/services.dart'; | 23 | import 'package:flutter/services.dart'; |
22 | import 'package:flutter/widgets.dart'; | 24 | import 'package:flutter/widgets.dart'; |
23 | import 'package:pdf/pdf.dart'; | 25 | import 'package:pdf/pdf.dart'; |
24 | 26 | ||
25 | -class Printing { | ||
26 | - static const MethodChannel _channel = MethodChannel('printing'); | ||
27 | - | ||
28 | - static Future<Null> printPdf({PdfDocument document, List<int> bytes}) async { | ||
29 | - assert(document != null || bytes != null); | ||
30 | - assert(!(document == null && bytes == null)); | ||
31 | - | ||
32 | - if (document != null) bytes = document.save(); | ||
33 | - | ||
34 | - final Map<String, dynamic> params = <String, dynamic>{ | ||
35 | - 'doc': Uint8List.fromList(bytes), | ||
36 | - }; | ||
37 | - | ||
38 | - await _channel.invokeMethod('printPdf', params); | ||
39 | - } | ||
40 | - | ||
41 | - static Future<Null> sharePdf( | ||
42 | - {PdfDocument document, List<int> bytes, Rect bounds}) async { | ||
43 | - assert(document != null || bytes != null); | ||
44 | - assert(!(document == null && bytes == null)); | ||
45 | - | ||
46 | - if (document != null) bytes = document.save(); | ||
47 | - | ||
48 | - if (bounds == null) { | ||
49 | - bounds = Rect.fromCircle(center: Offset.zero, radius: 10.0); | ||
50 | - } | ||
51 | - | ||
52 | - final Map<String, dynamic> params = <String, dynamic>{ | ||
53 | - 'doc': Uint8List.fromList(bytes), | ||
54 | - 'x': bounds.left, | ||
55 | - 'y': bounds.top, | ||
56 | - 'w': bounds.width, | ||
57 | - 'h': bounds.height, | ||
58 | - }; | ||
59 | - await _channel.invokeMethod('sharePdf', params); | ||
60 | - } | ||
61 | -} | 27 | +part 'src/asset_utils.dart'; |
28 | +part 'src/printing.dart'; |
printing/lib/src/asset_utils.dart
0 → 100644
1 | +/* | ||
2 | + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com> | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +part of printing; | ||
18 | + | ||
19 | +Future<PdfImage> pdfImageFromImage( | ||
20 | + {@required PdfDocument pdf, @required ui.Image image}) async { | ||
21 | + var bytes = await image.toByteData(format: ui.ImageByteFormat.rawRgba); | ||
22 | + | ||
23 | + return PdfImage(pdf, | ||
24 | + image: bytes.buffer.asUint8List(), | ||
25 | + width: image.width, | ||
26 | + height: image.height); | ||
27 | +} | ||
28 | + | ||
29 | +Future<PdfImage> pdfImageFromImageProvider( | ||
30 | + {@required PdfDocument pdf, | ||
31 | + @required ImageProvider image, | ||
32 | + ImageConfiguration configuration, | ||
33 | + ImageErrorListener onError}) async { | ||
34 | + final Completer<PdfImage> completer = Completer<PdfImage>(); | ||
35 | + final ImageStream stream = | ||
36 | + image.resolve(configuration ?? ImageConfiguration.empty); | ||
37 | + | ||
38 | + void listener(ImageInfo image, bool sync) async { | ||
39 | + final result = await pdfImageFromImage(pdf: pdf, image: image.image); | ||
40 | + if (!completer.isCompleted) completer.complete(result); | ||
41 | + stream.removeListener(listener); | ||
42 | + } | ||
43 | + | ||
44 | + void errorListener(dynamic exception, StackTrace stackTrace) { | ||
45 | + if (!completer.isCompleted) completer.complete(null); | ||
46 | + if (onError != null) { | ||
47 | + onError(exception, stackTrace); | ||
48 | + } else { | ||
49 | + FlutterError.reportError(FlutterErrorDetails( | ||
50 | + context: 'image failed to load', | ||
51 | + library: 'printing', | ||
52 | + exception: exception, | ||
53 | + stack: stackTrace, | ||
54 | + silent: true, | ||
55 | + )); | ||
56 | + } | ||
57 | + } | ||
58 | + | ||
59 | + stream.addListener(listener, onError: errorListener); | ||
60 | + return completer.future; | ||
61 | +} |
printing/lib/src/printing.dart
0 → 100644
1 | +/* | ||
2 | + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com> | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +part of printing; | ||
18 | + | ||
19 | +class Printing { | ||
20 | + static const MethodChannel _channel = MethodChannel('printing'); | ||
21 | + | ||
22 | + static Future<Null> printPdf({PdfDocument document, List<int> bytes}) async { | ||
23 | + assert(document != null || bytes != null); | ||
24 | + assert(!(document == null && bytes == null)); | ||
25 | + | ||
26 | + if (document != null) bytes = document.save(); | ||
27 | + | ||
28 | + final Map<String, dynamic> params = <String, dynamic>{ | ||
29 | + 'doc': Uint8List.fromList(bytes), | ||
30 | + }; | ||
31 | + | ||
32 | + await _channel.invokeMethod('printPdf', params); | ||
33 | + } | ||
34 | + | ||
35 | + static Future<Null> sharePdf( | ||
36 | + {PdfDocument document, List<int> bytes, Rect bounds}) async { | ||
37 | + assert(document != null || bytes != null); | ||
38 | + assert(!(document == null && bytes == null)); | ||
39 | + | ||
40 | + if (document != null) bytes = document.save(); | ||
41 | + | ||
42 | + if (bounds == null) { | ||
43 | + bounds = Rect.fromCircle(center: Offset.zero, radius: 10.0); | ||
44 | + } | ||
45 | + | ||
46 | + final Map<String, dynamic> params = <String, dynamic>{ | ||
47 | + 'doc': Uint8List.fromList(bytes), | ||
48 | + 'x': bounds.left, | ||
49 | + 'y': bounds.top, | ||
50 | + 'w': bounds.width, | ||
51 | + 'h': bounds.height, | ||
52 | + }; | ||
53 | + await _channel.invokeMethod('sharePdf', params); | ||
54 | + } | ||
55 | +} |
-
Please register or login to post a comment