printing 3.5.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

Printing #

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.

Example document

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.

Buy Me A Coffee

Installing #

  1. Add this package to your package's pubspec.yaml file as described on the installation tab

  2. Import the libraries

    import 'package:pdf/pdf.dart';
    import 'package:pdf/widgets.dart' as pw;
    import 'package:printing/printing.dart';
    
  3. Enable Swift on the iOS project, in ios/Podfile:

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

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

Examples #

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 and Digital Signature #

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.

Changelog #

3.5.0 #

  • Add decoration options to the PdfPreview Widget [Marcos Rodriguez]
  • Allow building for Android SDK 16
  • Fix font scaling in convertHtml()

3.4.0 #

  • Add PdfPreview Widget
  • Implement Printing.raster() on Flutter Web
  • Fix Swift 5 deprecated function
  • Improve code documentation

3.3.1 #

  • Remove width and height parameters from wrapWidget helper

3.3.0 #

  • Add wrapWidget helper
  • Add integration tests for wrapWidget

3.2.1 #

  • Add meta and image dependencies

3.2.0 #

  • Update README
  • Remove deprecated API
  • Use plugin_platform_interface
  • Fix inconsistent API
  • Add Unit tests
  • Update example tab
  • Uniformize examples
  • Optimize memory footprint
  • Add PdfRaster.asImage()

3.1.0 #

  • Migrate to the new Android plugins APIs
  • Fix Android app freeze

3.0.2 #

  • Add Raster PDF to Image

3.0.1 #

  • Add a link to the Web example

3.0.0 #

Breaking change: this version is only compatible with flutter >= 1.12

  • Simplify iOS code
  • Improve native code
  • Add Printing.info()
  • Use PageTheme in example
  • Save shared pdf in the cache on Android
  • Implement macOS embedding support
  • Implement Flutter Web support

2.1.9 #

  • Add Markdown example
  • Update printing example
  • Change the channel name
  • Add Builder widget
  • Improve Android registration

2.1.8 #

  • Revert "Update plugin platforms" (Flutter 1.9.1)

2.1.7 #

  • Add iOS Direct Print
  • Fix iOS 13 bug

2.1.6 #

  • Add QrCode to example
  • Cancel print job in case of layout error

2.1.5 #

  • Add printing completion

2.1.4 #

  • Update example to show saved documents on iOS Files app
  • Fix Html to Pdf paper size on iOS

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 the 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/lib/main.dart

// ignore_for_file: always_specify_types
// ignore_for_file: public_member_api_docs

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    const title = 'Printing Demo';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: const Text(title),
        ),
        body: Center(
          child: IconButton(
            icon: const Icon(Icons.print),
            onPressed: _printDocument,
          ),
        ),
      ),
    );
  }

  void _printDocument() {
    Printing.layoutPdf(
      onLayout: (pageFormat) {
        final doc = pw.Document();

        doc.addPage(
          pw.Page(
            build: (pw.Context context) => pw.Center(
              child: pw.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: ^3.5.0

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]
95
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]
97
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:printing/printing.dart] that declares support for platforms: android, ios, macos, web

Package does not support Flutter platform windows

Because of import path [package:printing/printing.dart] that declares support for platforms: android, ios, macos, web

Package not compatible with SDK dart

because of import path [printing] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
flutter 0.0.0
flutter_web_plugins 0.0.0
image ^2.1.4 2.1.14
js ^0.6.1 0.6.2
meta ^1.1.5 1.1.8 1.2.1
pdf ^1.6.0 1.9.0
plugin_platform_interface ^1.0.2 1.0.2
Transitive dependencies
archive 2.0.13
args 1.6.0
barcode 1.13.0
charcode 1.1.3
collection 1.14.12 1.14.13
convert 2.1.1
crypto 2.1.5
path 1.7.0
path_parsing 0.1.4
petitparser 3.0.4
qr 1.3.0
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
utf 0.9.0+5
vector_math 2.0.8
xml 4.2.0
Dev dependencies
flutter_test
mockito ^4.1.1