printing 1.0.5

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 87

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.

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.

To load an image it is possible to use Image.toByteData:

var Image im;
var bytes = await im.toByteData(format: ui.ImageByteFormat.rawRgba);

PDFImage image = PDFImage(
    pdf,
    image: bytes.buffer.asUint8List(), 
    width: im.width, 
    height: im.height);
g.drawImage(image, 100.0, 100.0, 80.0);

To use a TrueType font from a flutter bundle:

var font = await rootBundle.load("assets/open-sans.ttf");
PDFTTFFont ttf = new PDFTTFFont(pdf, font);
g.setColor(new PDFColor(0.3, 0.3, 0.3));
g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0);

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

import 'dart:io';
import 'dart:ui';

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

void main() => runApp(new MaterialApp(home: new MyApp()));

class MyApp extends StatelessWidget {
  final shareWidget = new GlobalKey();

  PDFDocument _generateDocument() {
    final pdf = new PDFDocument(deflate: zlib.encode);
    final page = new PDFPage(pdf, pageFormat: PDFPageFormat.A4);
    final g = page.getGraphics();
    final font = new PDFFont(pdf);
    final top = page.pageFormat.height;

    g.setColor(new PDFColor(0.0, 1.0, 1.0));
    g.drawRect(50.0 * PDFPageFormat.MM, top - 80.0 * PDFPageFormat.MM,
        100.0 * PDFPageFormat.MM, 50.0 * PDFPageFormat.MM);
    g.fillPath();

    g.setColor(new PDFColor(0.3, 0.3, 0.3));
    g.drawString(font, 12.0, "Hello World!", 10.0 * PDFPageFormat.MM,
        top - 10.0 * PDFPageFormat.MM);

    return pdf;
  }

  void _printPdf() {
    print("Print ...");
    final pdf = _generateDocument();
    Printing.printPdf(document: pdf);
  }

  void _sharePdf() {
    print("Share ...");
    final pdf = _generateDocument();

    // Calculate the widget center for iPad sharing popup position
    final RenderBox referenceBox =
        shareWidget.currentContext.findRenderObject();
    final topLeft =
        referenceBox.localToGlobal(referenceBox.paintBounds.topLeft);
    final bottomRight =
        referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight);
    final bounds = new Rect.fromPoints(topLeft, bottomRight);

    Printing.sharePdf(document: pdf, bounds: bounds);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Printing example'),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            new RaisedButton(
                child: new Text('Print Document'), onPressed: _printPdf),
            new RaisedButton(
                key: shareWidget,
                child: new Text('Share Document'),
                onPressed: _sharePdf),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  printing: ^1.0.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages 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';
  
Version Uploaded Documentation Archive
1.0.5 Sep 2, 2018 Go to the documentation of printing 1.0.5 Download printing 1.0.5 archive
1.0.4 Aug 21, 2018 Go to the documentation of printing 1.0.4 Download printing 1.0.4 archive
1.0.3 Aug 15, 2018 Go to the documentation of printing 1.0.3 Download printing 1.0.3 archive
1.0.2 Aug 15, 2018 Go to the documentation of printing 1.0.2 Download printing 1.0.2 archive
1.0.1 Aug 15, 2018 Go to the documentation of printing 1.0.1 Download printing 1.0.1 archive
1.0.0 Aug 14, 2018 Go to the documentation of printing 1.0.0 Download printing 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
74
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]
87
Learn more about scoring.

We analyzed this package on Sep 18, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3
  • Flutter: 0.8.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0
flutter 0.0.0
pdf ^1.0.5 1.0.6
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8