printing 2.1.6
printing: ^2.1.6 copied to clipboard
Plugin that allows Flutter apps to generate and print documents to android or ios compatible printers
Printing #
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.
![Example document](https://raw.githubusercontent.com/DavBfr/dart_pdf/master/printing/example.png)
This plugin uses the pdf
package https://pub.dartlang.org/packages/pdf
for pdf creation. Please refer to https://pub.dartlang.org/documentation/pdf/latest/
for documentation.
Example:
final pdf = Document();
pdf.addPage(Page(
pageFormat: PdfPageFormat.a4,
build: (Context context) {
return Center(
child: 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: pdf.document, image: imageProvider);
pdf.addPage(Page(
build: (Context context) {
return Center(
child: Image(image),
); // Center
})); // Page
To use a TrueType font from a flutter bundle:
final font = await rootBundle.load("assets/open-sans.ttf");
final ttf = Font.ttf(font);
pdf.addPage(Page(
build: (Context context) {
return Center(
child: Text('Dart is awesome', style: 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(pdf.save());
You can also print the document using the iOS or Android print service:
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => pdf.save());
Or share the document to other applications:
await Printing.sharePdf(bytes: pdf.save(), filename: 'my-document.pdf');
To print an HTML document, simply do:
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => await Printing.convertHtml(
format: format,
html: '<html><body><p>Hello!</p></body></html>',
));
Installing #
-
Add this to your package's
pubspec.yaml
file:dependencies: printing: any # <-- Add this line
-
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 19 // <-- Change this line to 19 or more ... }