Showing
8 changed files
with
199 additions
and
38 deletions
| @@ -10,8 +10,8 @@ images and text using TrueType fonts. With the ease of use you already know. | @@ -10,8 +10,8 @@ images and text using TrueType fonts. With the ease of use you already know. | ||
| 10 | 10 | ||
| 11 | <img alt="Example document" src="https://raw.githubusercontent.com/DavBfr/dart_pdf/master/pdf/example.jpg"> | 11 | <img alt="Example document" src="https://raw.githubusercontent.com/DavBfr/dart_pdf/master/pdf/example.jpg"> |
| 12 | 12 | ||
| 13 | -> Use the `printing` package <https://pub.dartlang.org/packages/printing> | ||
| 14 | -> for full flutter print and share operation. | 13 | +Use the `printing` package <https://pub.dartlang.org/packages/printing> |
| 14 | +for full flutter print and share operation. | ||
| 15 | 15 | ||
| 16 | The coordinate system is using the internal Pdf unit: | 16 | The coordinate system is using the internal Pdf unit: |
| 17 | * 1.0 is defined as 1 / 72.0 inch | 17 | * 1.0 is defined as 1 / 72.0 inch |
| @@ -19,8 +19,9 @@ The coordinate system is using the internal Pdf unit: | @@ -19,8 +19,9 @@ The coordinate system is using the internal Pdf unit: | ||
| 19 | 19 | ||
| 20 | Example: | 20 | Example: |
| 21 | ```dart | 21 | ```dart |
| 22 | -final pdf = Document() | ||
| 23 | - ..addPage(Page( | 22 | +final pdf = Document(); |
| 23 | + | ||
| 24 | +pdf.addPage(Page( | ||
| 24 | pageFormat: PdfPageFormat.a4, | 25 | pageFormat: PdfPageFormat.a4, |
| 25 | build: (Context context) { | 26 | build: (Context context) { |
| 26 | return Center( | 27 | return Center( |
| @@ -32,30 +33,43 @@ final pdf = Document() | @@ -32,30 +33,43 @@ final pdf = Document() | ||
| 32 | To load an image it is possible to use the dart library [image](https://pub.dartlang.org/packages/image): | 33 | To load an image it is possible to use the dart library [image](https://pub.dartlang.org/packages/image): |
| 33 | 34 | ||
| 34 | ```dart | 35 | ```dart |
| 35 | -Image image = decodeImage(Io.File('test.webp').readAsBytesSync()); | ||
| 36 | -PdfImage image = PdfImage( | ||
| 37 | - pdf, | 36 | +final img = decodeImage(File('test.webp').readAsBytesSync()); |
| 37 | +final image = PdfImage( | ||
| 38 | + pdf.document, | ||
| 38 | image: img.data.buffer.asUint8List(), | 39 | image: img.data.buffer.asUint8List(), |
| 39 | width: img.width, | 40 | width: img.width, |
| 40 | - height: img.height); | ||
| 41 | -g.drawImage(image, 100.0, 100.0, 80.0); | 41 | + height: img.height, |
| 42 | +); | ||
| 43 | + | ||
| 44 | +pdf.addPage(Page( | ||
| 45 | + build: (Context context) { | ||
| 46 | + return Center( | ||
| 47 | + child: Image(image), | ||
| 48 | + ); // Center | ||
| 49 | + })); // Page | ||
| 42 | ``` | 50 | ``` |
| 43 | 51 | ||
| 44 | To use a TrueType font: | 52 | To use a TrueType font: |
| 45 | 53 | ||
| 46 | ```dart | 54 | ```dart |
| 47 | -PdfTtfFont ttf = PdfTtfFont( | ||
| 48 | - pdf, | ||
| 49 | - (File("open-sans.ttf").readAsBytesSync() as Uint8List).buffer.asByteData()); | ||
| 50 | -g.setColor(PdfColor(0.3, 0.3, 0.3)); | ||
| 51 | -g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0); | 55 | +final Uint8List fontData = File('open-sans.ttf').readAsBytesSync(); |
| 56 | +final ttf = Font.ttf(fontData.buffer.asByteData()); | ||
| 57 | + | ||
| 58 | +pdf.addPage(Page( | ||
| 59 | + pageFormat: PdfPageFormat.a4, | ||
| 60 | + build: (Context context) { | ||
| 61 | + return Center( | ||
| 62 | + child: Text('Hello World', style: TextStyle(font: ttf, fontSize: 40)), | ||
| 63 | + ); // Center | ||
| 64 | + })); // Page | ||
| 52 | ``` | 65 | ``` |
| 53 | 66 | ||
| 54 | -To save the image on Flutter, use the [path_provider](https://pub.dartlang.org/packages/path_provider) library: | 67 | +To save the pdf file: |
| 55 | 68 | ||
| 56 | ```dart | 69 | ```dart |
| 57 | -Directory tempDir = await getTemporaryDirectory(); | ||
| 58 | -String tempPath = tempDir.path; | ||
| 59 | -var file = File("$tempPath/file.pdf"); | 70 | +// On Flutter, use the [path_provider](https://pub.dartlang.org/packages/path_provider) library: |
| 71 | +// final output = await getTemporaryDirectory(); | ||
| 72 | +// final file = File("${output.path}/example.pdf"); | ||
| 73 | +final file = File("example.pdf"); | ||
| 60 | await file.writeAsBytes(pdf.save()); | 74 | await file.writeAsBytes(pdf.save()); |
| 61 | ``` | 75 | ``` |
| @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl | @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl | ||
| 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf | 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf |
| 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: 1.3.10 | 7 | +version: 1.3.11 |
| 8 | 8 | ||
| 9 | environment: | 9 | environment: |
| 10 | sdk: ">=2.1.0 <3.0.0" | 10 | sdk: ">=2.1.0 <3.0.0" |
| @@ -7,32 +7,71 @@ See the example on how to use the plugin. | @@ -7,32 +7,71 @@ See the example on how to use the plugin. | ||
| 7 | 7 | ||
| 8 | <img alt="Example document" src="https://raw.githubusercontent.com/DavBfr/dart_pdf/master/printing/example.png" width="300"> | 8 | <img alt="Example document" src="https://raw.githubusercontent.com/DavBfr/dart_pdf/master/printing/example.png" width="300"> |
| 9 | 9 | ||
| 10 | -> This plugin uses the `pdf` package <https://pub.dartlang.org/packages/pdf> | ||
| 11 | -> for pdf creation. Please refer to <https://pub.dartlang.org/documentation/pdf/latest/> | ||
| 12 | -> for documentation. | 10 | +This plugin uses the `pdf` package <https://pub.dartlang.org/packages/pdf> |
| 11 | +for pdf creation. Please refer to <https://pub.dartlang.org/documentation/pdf/latest/> | ||
| 12 | +for documentation. | ||
| 13 | 13 | ||
| 14 | -To load an image it is possible to use | ||
| 15 | -[Image.toByteData](https://docs.flutter.io/flutter/dart-ui/Image/toByteData.html): | 14 | +Example: |
| 16 | 15 | ||
| 17 | ```dart | 16 | ```dart |
| 18 | -var Image im; | ||
| 19 | -var bytes = await im.toByteData(format: ui.ImageByteFormat.rawRgba); | ||
| 20 | - | ||
| 21 | -PdfImage image = PdfImage( | ||
| 22 | - pdf, | ||
| 23 | - image: bytes.buffer.asUint8List(), | ||
| 24 | - width: im.width, | ||
| 25 | - height: im.height); | ||
| 26 | -g.drawImage(image, 100.0, 100.0, 80.0); | 17 | +final pdf = PdfDoc(); |
| 18 | + | ||
| 19 | +pdf.addPage(Page( | ||
| 20 | + pageFormat: PdfPageFormat.a4, | ||
| 21 | + build: (Context context) { | ||
| 22 | + return Center( | ||
| 23 | + child: Text("Hello World"), | ||
| 24 | + ); // Center | ||
| 25 | + })); // Page | ||
| 26 | +``` | ||
| 27 | + | ||
| 28 | +To load an image from an ImageProvider: | ||
| 29 | + | ||
| 30 | +```dart | ||
| 31 | +const imageProvider = const AssetImage('assets/image.png'); | ||
| 32 | +final PdfImage image = await pdfImageFromImageProvider(pdf: pdf.document, image: imageProvider); | ||
| 33 | + | ||
| 34 | +pdf.addPage(Page( | ||
| 35 | + build: (Context context) { | ||
| 36 | + return Center( | ||
| 37 | + child: Image(image), | ||
| 38 | + ); // Center | ||
| 39 | + })); // Page | ||
| 27 | ``` | 40 | ``` |
| 28 | 41 | ||
| 29 | To use a TrueType font from a flutter bundle: | 42 | To use a TrueType font from a flutter bundle: |
| 30 | 43 | ||
| 31 | ```dart | 44 | ```dart |
| 32 | -var font = await rootBundle.load("assets/open-sans.ttf"); | ||
| 33 | -PdfTtfFont ttf = PdfTtfFont(pdf, font); | ||
| 34 | -g.setColor(PdfColor(0.3, 0.3, 0.3)); | ||
| 35 | -g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0); | 45 | +final font = await rootBundle.load("assets/open-sans.ttf"); |
| 46 | +final ttf = Font.ttf(font); | ||
| 47 | + | ||
| 48 | +pdf.addPage(Page( | ||
| 49 | + build: (Context context) { | ||
| 50 | + return Center( | ||
| 51 | + child: Text('Dart is awesome', style: TextStyle(font: ttf, fontSize: 40)), | ||
| 52 | + ); // Center | ||
| 53 | + })); // Page | ||
| 54 | +``` | ||
| 55 | + | ||
| 56 | +To save the pdf file using the [path_provider](https://pub.dartlang.org/packages/path_provider) library: | ||
| 57 | + | ||
| 58 | +```dart | ||
| 59 | +final output = await getTemporaryDirectory(); | ||
| 60 | +final file = File("${output.path}/example.pdf"); | ||
| 61 | +await file.writeAsBytes(pdf.save()); | ||
| 62 | +``` | ||
| 63 | + | ||
| 64 | +You can also print the document using the iOS or Android print service: | ||
| 65 | + | ||
| 66 | +```dart | ||
| 67 | +await Printing.layoutPdf( | ||
| 68 | + onLayout: (PdfPageFormat format) async => pdf.save()); | ||
| 69 | +``` | ||
| 70 | + | ||
| 71 | +Or share the document to other applications: | ||
| 72 | + | ||
| 73 | +```dart | ||
| 74 | +await Printing.sharePdf(bytes: pdf.save(), filename: 'my-document.pdf'); | ||
| 36 | ``` | 75 | ``` |
| 37 | 76 | ||
| 38 | ## Installing | 77 | ## Installing |
| @@ -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: 2.0.3 | 7 | +version: 2.0.4 |
| 8 | 8 | ||
| 9 | environment: | 9 | environment: |
| 10 | sdk: ">=2.1.0 <3.0.0" | 10 | sdk: ">=2.1.0 <3.0.0" |
test/extract_readme.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:io'; | ||
| 18 | + | ||
| 19 | +import 'package:markdown/markdown.dart' as md; | ||
| 20 | + | ||
| 21 | +Iterable<String> getCode(List<md.Node> nodes, [bool isCode = false]) sync* { | ||
| 22 | + for (md.Node node in nodes) { | ||
| 23 | + if (node is md.Element) { | ||
| 24 | + // print(node.tag); | ||
| 25 | + // print(node.attributes); | ||
| 26 | + yield* getCode(node.children, | ||
| 27 | + node.tag == 'code' && node.attributes['class'] == 'language-dart'); | ||
| 28 | + } else if (node is md.Text) { | ||
| 29 | + if (isCode) { | ||
| 30 | + yield '// ------------'; | ||
| 31 | + yield node.text; | ||
| 32 | + } | ||
| 33 | + } else { | ||
| 34 | + print(node); | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +void main() { | ||
| 40 | + final md.Document document = md.Document( | ||
| 41 | + extensionSet: md.ExtensionSet.commonMark, | ||
| 42 | + encodeHtml: false, | ||
| 43 | + ); | ||
| 44 | + | ||
| 45 | + final output = File('readme.dart'); | ||
| 46 | + final st = output.openWrite(); | ||
| 47 | + st.writeln('import \'dart:io\';'); | ||
| 48 | + st.writeln('import \'dart:typed_data\';'); | ||
| 49 | + st.writeln('import \'package:pdf/pdf.dart\';'); | ||
| 50 | + st.writeln('import \'package:pdf/widgets.dart\';'); | ||
| 51 | + st.writeln('import \'package:image/image.dart\' show decodeImage;'); | ||
| 52 | + st.writeln('import \'package:printing/printing.dart\';'); | ||
| 53 | + st.writeln('import \'package:flutter/services.dart\' show rootBundle;'); | ||
| 54 | + st.writeln('import \'package:flutter/widgets.dart\' show AssetImage;'); | ||
| 55 | + st.writeln('import \'package:path_provider/path_provider.dart\';'); | ||
| 56 | + | ||
| 57 | + { | ||
| 58 | + final data = File('../pdf/README.md').readAsStringSync(); | ||
| 59 | + final List<String> lines = data.replaceAll('\r\n', '\n').split('\n'); | ||
| 60 | + final List<md.Node> parsedLines = document.parseLines(lines); | ||
| 61 | + final Iterable<String> code = getCode(parsedLines); | ||
| 62 | + | ||
| 63 | + st.writeln('Future pdfReadme() async {'); | ||
| 64 | + st.writeln(code.join('\n')); | ||
| 65 | + st.writeln('}'); | ||
| 66 | + } | ||
| 67 | + { | ||
| 68 | + final data = File('../printing/README.md').readAsStringSync(); | ||
| 69 | + final List<String> lines = data.replaceAll('\r\n', '\n').split('\n'); | ||
| 70 | + final List<md.Node> parsedLines = document.parseLines(lines); | ||
| 71 | + final Iterable<String> code = getCode(parsedLines); | ||
| 72 | + | ||
| 73 | + st.writeln('Future printingReadme() async {'); | ||
| 74 | + st.writeln(code.join('\n')); | ||
| 75 | + st.writeln('}'); | ||
| 76 | + } | ||
| 77 | + st.writeln('Future main() async {'); | ||
| 78 | + st.writeln('await pdfReadme();'); | ||
| 79 | + st.writeln('await printingReadme();'); | ||
| 80 | + st.writeln('}'); | ||
| 81 | + st.close(); | ||
| 82 | +} |
test/pubspec.yaml
0 → 100644
| 1 | +name: readme_to_dart | ||
| 2 | +author: David PHAM-VAN <dev.nfet.net@gmail.com> | ||
| 3 | +description: Readme Markdown to dart source converter. | ||
| 4 | +homepage: https://github.com/DavBfr/dart_pdf/tree/master/test | ||
| 5 | +repository: https://github.com/DavBfr/dart_pdf | ||
| 6 | +issue_tracker: https://github.com/DavBfr/dart_pdf/issues | ||
| 7 | +version: 1.0.0 | ||
| 8 | + | ||
| 9 | +environment: | ||
| 10 | + sdk: ">=2.1.0 <3.0.0" | ||
| 11 | + | ||
| 12 | +dependencies: | ||
| 13 | + pdf: | ||
| 14 | + printing: | ||
| 15 | + markdown: | ||
| 16 | + meta: | ||
| 17 | + string_scanner: | ||
| 18 | + path: | ||
| 19 | + image: | ||
| 20 | + path_provider: |
-
Please register or login to post a comment