A plugin that allows Flutter apps to generate and print documents to android or ios compatible printers
See the example on how to use the plugin.
This example is also available on the web here: https://davbfr.github.io/dart_pdf/.
This plugin uses the pdf
package https://pub.dev/packages/pdf
for pdf creation. Please refer to https://pub.dev/documentation/pdf/latest/
for documentation.
-
Add this package to your package's
pubspec.yaml
file as described on the installation tab -
Import the libraries
import 'package:pdf/pdf.dart'; import 'package:pdf/widgets.dart' as pw; import 'package:printing/printing.dart';
-
Enable Swift on the iOS project, in
ios/Podfile
:target 'Runner' do use_frameworks! # <-- Add this line
-
Set minimum Android version in
android/app/build.gradle
:defaultConfig { ... minSdkVersion 21 // <-- Change this line to 21 or more ... }
final doc = pw.Document();
doc.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child: pw.Text("Hello World"),
); // Center
})); // Page
To load an image from an ImageProvider:
const imageProvider = const AssetImage('assets/image.png');
final PdfImage image = await pdfImageFromImageProvider(pdf: doc.document, image: imageProvider);
doc.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Image(image),
); // Center
})); // Page
To use a TrueType font from a flutter bundle:
final font = await rootBundle.load("assets/open-sans.ttf");
final ttf = pw.Font.ttf(font);
doc.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Text('Dart is awesome', style: pw.TextStyle(font: ttf, fontSize: 40)),
); // Center
})); // Page
To save the pdf file using the path_provider library:
final output = await getTemporaryDirectory();
final file = File("${output.path}/example.pdf");
await file.writeAsBytes(doc.save());
You can also print the document using the iOS or Android print service:
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => doc.save());
Or share the document to other applications:
await Printing.sharePdf(bytes: doc.save(), filename: 'my-document.pdf');
To print an HTML document:
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => await Printing.convertHtml(
format: format,
html: '<html><body><p>Hello!</p></body></html>',
));
Convert a Pdf to images, one image per page, get only pages 1 and 2 at 72 dpi:
await for (var page in Printing.raster(doc.save(), pages: [0, 1], dpi: 72)) {
final image = page.toImage(); // ...or page.toPng()
print(image);
}
To print an existing Pdf file from a Flutter asset:
final pdf = await rootBundle.load('document.pdf');
await Printing.layoutPdf(onLayout: (_) => pdf.buffer.asUint8List());
Encryption using RC4-40, RC4-128, AES-128, and AES-256 is fully supported using a separate library. This library also provides SHA1 or SHA-256 Digital Signature using your x509 certificate. The graphic signature is represented by a clickable widget that shows Digital Signature information.
Drop me an email for availability and more information.