Showing
5 changed files
with
92 additions
and
16 deletions
| 1 | import 'dart:async'; | 1 | import 'dart:async'; |
| 2 | import 'dart:io'; | 2 | import 'dart:io'; |
| 3 | -import 'dart:typed_data'; | ||
| 4 | -import 'dart:ui' as ui; | ||
| 5 | 3 | ||
| 6 | import 'package:flutter/foundation.dart'; | 4 | import 'package:flutter/foundation.dart'; |
| 7 | import 'package:flutter/material.dart'; | 5 | import 'package:flutter/material.dart'; |
| @@ -9,7 +7,6 @@ import 'package:flutter/rendering.dart'; | @@ -9,7 +7,6 @@ import 'package:flutter/rendering.dart'; | ||
| 9 | import 'package:flutter/services.dart'; | 7 | import 'package:flutter/services.dart'; |
| 10 | import 'package:markdown/markdown.dart' as markdown; | 8 | import 'package:markdown/markdown.dart' as markdown; |
| 11 | import 'package:path_provider/path_provider.dart'; | 9 | import 'package:path_provider/path_provider.dart'; |
| 12 | - | ||
| 13 | import 'package:pdf/pdf.dart'; | 10 | import 'package:pdf/pdf.dart'; |
| 14 | import 'package:pdf/widgets.dart' as pw; | 11 | import 'package:pdf/widgets.dart' as pw; |
| 15 | import 'package:printing/printing.dart'; | 12 | import 'package:printing/printing.dart'; |
| @@ -156,21 +153,17 @@ class MyAppState extends State<MyApp> { | @@ -156,21 +153,17 @@ class MyAppState extends State<MyApp> { | ||
| 156 | } | 153 | } |
| 157 | 154 | ||
| 158 | Future<void> _printScreen() async { | 155 | Future<void> _printScreen() async { |
| 159 | - final RenderRepaintBoundary boundary = | ||
| 160 | - previewContainer.currentContext.findRenderObject(); | ||
| 161 | - final ui.Image im = await boundary.toImage(); | ||
| 162 | - final ByteData bytes = | ||
| 163 | - await im.toByteData(format: ui.ImageByteFormat.rawRgba); | ||
| 164 | - print('Print Screen ${im.width}x${im.height} ...'); | ||
| 165 | - | ||
| 166 | final bool result = | 156 | final bool result = |
| 167 | - await Printing.layoutPdf(onLayout: (PdfPageFormat format) { | 157 | + await Printing.layoutPdf(onLayout: (PdfPageFormat format) async { |
| 168 | final pw.Document document = pw.Document(); | 158 | final pw.Document document = pw.Document(); |
| 169 | 159 | ||
| 170 | - final PdfImage image = PdfImage(document.document, | ||
| 171 | - image: bytes.buffer.asUint8List(), | ||
| 172 | - width: im.width, | ||
| 173 | - height: im.height); | 160 | + final PdfImage image = await wrapWidget( |
| 161 | + document.document, | ||
| 162 | + key: previewContainer, | ||
| 163 | + pixelRatio: 2.0, | ||
| 164 | + ); | ||
| 165 | + | ||
| 166 | + print('Print Screen ${image.width}x${image.height}...'); | ||
| 174 | 167 | ||
| 175 | document.addPage(pw.Page( | 168 | document.addPage(pw.Page( |
| 176 | pageFormat: format, | 169 | pageFormat: format, |
| @@ -20,3 +20,4 @@ export 'src/printer.dart'; | @@ -20,3 +20,4 @@ export 'src/printer.dart'; | ||
| 20 | export 'src/printing.dart'; | 20 | export 'src/printing.dart'; |
| 21 | export 'src/printing_info.dart'; | 21 | export 'src/printing_info.dart'; |
| 22 | export 'src/raster.dart'; | 22 | export 'src/raster.dart'; |
| 23 | +export 'src/widget_wrapper.dart'; |
printing/lib/src/widget_wrapper.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 | +import 'dart:async'; | ||
| 18 | +import 'dart:typed_data'; | ||
| 19 | +import 'dart:ui' as ui; | ||
| 20 | + | ||
| 21 | +import 'package:flutter/material.dart'; | ||
| 22 | +import 'package:flutter/rendering.dart'; | ||
| 23 | +import 'package:pdf/pdf.dart'; | ||
| 24 | + | ||
| 25 | +/// Wrap a Flutter Widget identified by a GlobalKey to a PdfImage. | ||
| 26 | +Future<PdfImage> wrapWidget( | ||
| 27 | + PdfDocument document, { | ||
| 28 | + @required GlobalKey key, | ||
| 29 | + int width, | ||
| 30 | + int height, | ||
| 31 | + double pixelRatio = 1.0, | ||
| 32 | +}) async { | ||
| 33 | + assert(key != null); | ||
| 34 | + assert(pixelRatio != null && pixelRatio > 0); | ||
| 35 | + | ||
| 36 | + final RenderRepaintBoundary wrappedWidget = | ||
| 37 | + key.currentContext.findRenderObject(); | ||
| 38 | + ui.Image image = await wrappedWidget.toImage(pixelRatio: pixelRatio); | ||
| 39 | + | ||
| 40 | + image = await _resize(image, width, height); | ||
| 41 | + | ||
| 42 | + final ByteData byteData = | ||
| 43 | + await image.toByteData(format: ui.ImageByteFormat.rawRgba); | ||
| 44 | + final Uint8List imageData = byteData.buffer.asUint8List(); | ||
| 45 | + return PdfImage(document, | ||
| 46 | + image: imageData, width: image.width, height: image.height); | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +Future<ui.Image> _resize( | ||
| 50 | + ui.Image image, | ||
| 51 | + int width, | ||
| 52 | + int height, | ||
| 53 | +) async { | ||
| 54 | + if (width == null && height == null) { | ||
| 55 | + return image; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + width ??= (height / image.height * image.width).toInt(); | ||
| 59 | + height ??= (width / image.width * image.height).toInt(); | ||
| 60 | + | ||
| 61 | + final Completer<ui.Image> ptr = Completer<ui.Image>(); | ||
| 62 | + final Uint8List data = | ||
| 63 | + (await image.toByteData(format: ui.ImageByteFormat.rawRgba)) | ||
| 64 | + .buffer | ||
| 65 | + .asUint8List(); | ||
| 66 | + ui.decodeImageFromPixels( | ||
| 67 | + data, | ||
| 68 | + image.width, | ||
| 69 | + image.height, | ||
| 70 | + ui.PixelFormat.rgba8888, | ||
| 71 | + (ui.Image result) { | ||
| 72 | + ptr.complete(result); | ||
| 73 | + }, | ||
| 74 | + targetWidth: width, | ||
| 75 | + targetHeight: height, | ||
| 76 | + ); | ||
| 77 | + return ptr.future; | ||
| 78 | +} |
| @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to | @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to | ||
| 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing | 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing |
| 5 | repository: https://github.com/DavBfr/dart_pdf | 5 | repository: https://github.com/DavBfr/dart_pdf |
| 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues | 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues |
| 7 | -version: 3.2.1 | 7 | +version: 3.3.0 |
| 8 | 8 | ||
| 9 | environment: | 9 | environment: |
| 10 | sdk: ">=2.3.0 <3.0.0" | 10 | sdk: ">=2.3.0 <3.0.0" |
-
Please register or login to post a comment