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

This plugin uses the pdf package pub.dev/packages/pdf for pdf creation. Please refer to pub.dev/documentation/pdf/latest/ for documentation.

Buy Me A Coffee

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

  1. Add this to your package's pubspec.yaml file:

    dependencies:
      printing: any       # <-- Add this line
    
  2. Enable Swift on the iOS project, in ios/Podfile:

    target 'Runner' do
       use_frameworks!    # <-- Add this line
    
  3. Set minimum Android version in android/app/build.gradle:

    defaultConfig {
        ...
        minSdkVersion 19  // <-- Change this line to 19 or more
        ...
    }
    

Libraries

printing