Showing
4 changed files
with
47 additions
and
28 deletions
| @@ -134,6 +134,23 @@ final pdf = await rootBundle.load('document.pdf'); | @@ -134,6 +134,23 @@ final pdf = await rootBundle.load('document.pdf'); | ||
| 134 | await Printing.layoutPdf(onLayout: (_) => pdf.buffer.asUint8List()); | 134 | await Printing.layoutPdf(onLayout: (_) => pdf.buffer.asUint8List()); |
| 135 | ``` | 135 | ``` |
| 136 | 136 | ||
| 137 | +## Designing your PDF document | ||
| 138 | + | ||
| 139 | +A good starting point is to use PdfPreview which features hot-reload pdf build | ||
| 140 | +and refresh. | ||
| 141 | + | ||
| 142 | +Take a look at the [example tab](example) for a sample project. | ||
| 143 | + | ||
| 144 | +This widget also features a debug switch at the bottom right to display the | ||
| 145 | +drawing constraints used. This switch is available only on debug builds. | ||
| 146 | + | ||
| 147 | +Moving on to your production application, you can keep only the `_generatePdf` | ||
| 148 | +function and print the document using: | ||
| 149 | + | ||
| 150 | +```dart | ||
| 151 | +await Printing.layoutPdf(onLayout: (format) => _generatePdf(format, title)); | ||
| 152 | +``` | ||
| 153 | + | ||
| 137 | ## Encryption and Digital Signature | 154 | ## Encryption and Digital Signature |
| 138 | 155 | ||
| 139 | Encryption using RC4-40, RC4-128, AES-128, and AES-256 is fully supported using a separate library. | 156 | Encryption using RC4-40, RC4-128, AES-128, and AES-256 is fully supported using a separate library. |
| 1 | // ignore_for_file: always_specify_types | 1 | // ignore_for_file: always_specify_types |
| 2 | // ignore_for_file: public_member_api_docs | 2 | // ignore_for_file: public_member_api_docs |
| 3 | 3 | ||
| 4 | +import 'dart:typed_data'; | ||
| 5 | + | ||
| 4 | import 'package:flutter/material.dart'; | 6 | import 'package:flutter/material.dart'; |
| 7 | +import 'package:pdf/pdf.dart'; | ||
| 5 | import 'package:pdf/widgets.dart' as pw; | 8 | import 'package:pdf/widgets.dart' as pw; |
| 6 | import 'package:printing/printing.dart'; | 9 | import 'package:printing/printing.dart'; |
| 7 | 10 | ||
| 8 | -void main() => runApp(MyApp()); | 11 | +void main() => runApp(const MyApp('Printing Demo')); |
| 9 | 12 | ||
| 10 | class MyApp extends StatelessWidget { | 13 | class MyApp extends StatelessWidget { |
| 14 | + const MyApp(this.title); | ||
| 15 | + | ||
| 16 | + final String title; | ||
| 17 | + | ||
| 11 | @override | 18 | @override |
| 12 | Widget build(BuildContext context) { | 19 | Widget build(BuildContext context) { |
| 13 | - const title = 'Printing Demo'; | ||
| 14 | - | ||
| 15 | return MaterialApp( | 20 | return MaterialApp( |
| 16 | - title: title, | ||
| 17 | home: Scaffold( | 21 | home: Scaffold( |
| 18 | - appBar: AppBar( | ||
| 19 | - title: const Text(title), | ||
| 20 | - ), | ||
| 21 | - body: Center( | ||
| 22 | - child: IconButton( | ||
| 23 | - icon: const Icon(Icons.print), | ||
| 24 | - onPressed: _printDocument, | ||
| 25 | - ), | 22 | + appBar: AppBar(title: Text(title)), |
| 23 | + body: PdfPreview( | ||
| 24 | + build: (format) => _generatePdf(format, title), | ||
| 26 | ), | 25 | ), |
| 27 | ), | 26 | ), |
| 28 | ); | 27 | ); |
| 29 | } | 28 | } |
| 30 | 29 | ||
| 31 | - void _printDocument() { | ||
| 32 | - Printing.layoutPdf( | ||
| 33 | - onLayout: (pageFormat) { | ||
| 34 | - final doc = pw.Document(); | ||
| 35 | - | ||
| 36 | - doc.addPage( | ||
| 37 | - pw.Page( | ||
| 38 | - build: (pw.Context context) => pw.Center( | ||
| 39 | - child: pw.Text('Hello World!'), | ||
| 40 | - ), | ||
| 41 | - ), | ||
| 42 | - ); | ||
| 43 | - | ||
| 44 | - return doc.save(); | ||
| 45 | - }, | 30 | + Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async { |
| 31 | + final pdf = pw.Document(); | ||
| 32 | + | ||
| 33 | + pdf.addPage( | ||
| 34 | + pw.Page( | ||
| 35 | + pageFormat: format, | ||
| 36 | + build: (context) { | ||
| 37 | + return pw.Center( | ||
| 38 | + child: pw.Text(title), | ||
| 39 | + ); | ||
| 40 | + }, | ||
| 41 | + ), | ||
| 46 | ); | 42 | ); |
| 43 | + | ||
| 44 | + return pdf.save(); | ||
| 47 | } | 45 | } |
| 48 | } | 46 | } |
| @@ -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.6.0 | 7 | +version: 3.6.1 |
| 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