printing 2.1.3

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 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 #

  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
        ...
    }
    

Changelog #

2.1.3 #

  • Update Pdf dependency

2.1.2 #

  • Update Flutter and Dart dependency

2.1.0 #

  • Add html to pdf platform conversion
  • Fix issue with flutter 1.6.2+

2.0.4 #

  • Update Readme

2.0.3 #

  • Add file save and view to example application
  • Convert print screen example to Widgets
  • Deprecate the document argument in Printing.sharePdf()

2.0.2 #

  • Fix example application

2.0.1 #

  • Fix Replace FlutterErrorDetails to be compatible with Dart 2.3.0

2.0.0 #

  • Breaking change: Switch libraries to AndroidX
  • Add Page information to PdfDoc object

1.3.5 #

  • Restore compatibility with Flutter 1.0.0
  • Update code to Dart 2.1.0
  • Depends on pdf 1.3.3

1.3.4 #

  • Fix iOS build with Swift
  • Add installation instructions in the Readme
  • Follow Flutter debug painting settings

1.3.3 #

  • Fix dart lint warnings
  • Add documentation
  • Add a filename parameter for sharing
  • Convert Objective-C code to Swift
  • Update Readme

1.3.2 #

  • Fix iOS printing issues

1.3.1 #

  • Fix pana linting notices

1.3.0 #

  • Add a Flutter like widget system

1.2.0 #

  • Fix compileSdkVersion to match appcompat
  • Change license to Apache 2.0
  • Implement asynchronous printing driven by the OS

1.1.0 #

  • Rename classes to satisfy Dart conventions
  • Remove useless new and const keywords
  • Changed appcompat dependency to 26.1.0

1.0.6 #

  • Add screenshot example

1.0.5 #

  • Fix printing from pdf document

1.0.4 #

  • Update example for pdf 1.0.5
  • Add Contributing

1.0.3 #

  • Update source formatting
  • Update README

1.0.2 #

  • Add License file
  • Updated homepage

1.0.1 #

  • Fixed sdk version

1.0.0 #

  • Initial release.

example/README.md

Pdf Printing Example #

import 'package:flutter/material.dart';

import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pdf;
import 'package:printing/printing.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Printing Demo'),
        ),
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.print),
          tooltip: 'Print Document',
          onPressed: () {
            Printing.layoutPdf(
              onLayout: buildPdf,
            );
          },
        ),
        body: Center(
          child: const Text('Click on the print button below'),
        ),
      ),
    );
  }

  List<int> buildPdf(PdfPageFormat format) {
    final Document doc = Document();

    doc.addPage(
      pdf.Page(
        pageFormat: format,
        build: (pdf.Context context) {
          return pdf.ConstrainedBox(
            constraints: const pdf.BoxConstraints.expand(),
            child: pdf.FittedBox(
              child: pdf.Text(
                'Hello World',
              ),
            ),
          );
        },
      ),
    );

    return doc.save();
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  printing: ^2.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:printing/printing.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
90
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
95
Learn more about scoring.

We analyzed this package on Jul 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
pdf ^1.3.15 1.3.15
Transitive dependencies
archive 2.0.10
args 1.5.2
charcode 1.1.2
collection 1.14.11
convert 2.1.1
crypto 2.0.6
meta 1.1.6 1.1.7
path 1.6.2
sky_engine 0.0.99
typed_data 1.1.6
utf 0.9.0+5
vector_math 2.0.8
Dev dependencies
flutter_test

Admin