Home

Awesome

<p align="left"> <b><a href="https://github.com/katzer/cordova-plugin-printer/blob/example/README.md">SAMPLE APP</a> :point_right:</b> </p>

Cordova Print Plugin <br> npm version License PayPayl donate button

Plugin for Cordova to print documents, photos, HTML and plain text from iOS, Android and Windows Universal apps.

cordova.plugins.printer.print('<b>Hello Cordova!</b>');
<img width="280px" align="right" src="https://github.com/katzer/cordova-plugin-printer/blob/example/images/print.png">

Supported Printer Interfaces

Supported Content

Supported Platforms

Basics

The plugin creates the object cordova.plugins.printer and is accessible after the deviceready event has been fired.

document.addEventListener('deviceready', function () {
    // cordova.plugins.printer is now available
}, false);

Prints the contents of the web view:

cordova.plugins.printer.print();

Plain text:

cordova.plugins.printer.print("Hello\nWorld!");

HTML & CSS:

cordova.plugins.printer.print('<h1>Hello World!</h1>');

Images, PDF and other documents:

cordova.plugins.printer.print('file://img/logo.png');

Base64 encoded content:

cordova.plugins.printer.print('base64://...');

Note: On the browser platform the plugin only supports to print the contents of the web view.

Formatting

It's possible to pass format options to the print method that overrides the defaults:

cordova.plugins.printer.print(content, options, callback);

The defaults are defined as follows:

cordova.plugins.printer.setDefaults({ monochrome: true });

The list of possible options depend on the platform, the content type and the capabilities of the printer.

NameDescriptionTypePlatform
nameThe name of the print job and of the document.Stringall
copiesThe number of copies for the print task.NumberiOS<br>Windows
pageCountLimits the pages to print even the document contains more.<br>To skip the last n pages you can assign a negative value on iOS.NumberiOS<br>Android
duplexEither double-sided on short site (duplex:'short'), double-sided on long site (duplex:'long') or single-sided (duplex:'none').Stringall
orientationThe orientation of the printed content, portrait or landscape.Stringall
monochromeIf your application only prints black text, setting this property to true can result in better performance in many cases.Booleanall
photoSet to true to change the media type to photography for higher quality.BooleaniOS<br>Windows
autoFitSet to false to disable downscaling the image to fit into the content aread.BooleanAndroid
printerThe network URL to the printer.StringiOS
maxHeight<br>maxWidthDefines the maximum size of the content area.UnitiOS
marginSet to false to avoid margins.Booleanall
margin.top<br>margin.left<br>margin.right<br>margin.bottomThe margins for each printed page. Each printer might have its own minimum margins depends on media type and paper format.UnitiOS
ui.hideNumberOfCopiesSet to true to hide the control for the number of copies.BooleaniOS
ui.hidePaperFormatSet to true to hide the control for the paper format.BooleaniOS
ui.top<br>ui.leftThe position of the printer picker.NumberiPad
ui.height<br>ui.widthThe size of the printer picker.NumberiPad
paper.width<br>paper.heightThe dimensions of the paper – iOS will will try to choose a format which fits bests.UnitiOS
paper.nameThe name of the format like IsoA4 or Roll22Inch.<br>https://docs.microsoft.com/en-us/uwp/api/windows.graphics.printing.printmediasizeStringWindows
paper.lengthOn roll-fed printers you can decide when the printer cuts the paper.UnitiOS
font.nameThe name of the font familyStringiOS
font.sizeThe size of the fontNumberiOS<br>Android
font.italic<br>font.boldSet to true to enable these font traits.BooleaniOS
font.alignPossible alignments are left, right, center and justified.StringiOS
font.colorThe color of the font in hexa-decimal RGB format - "FF0000" means red.StringiOS
header.height<br>footer.heightThe height of the header or footer on each page.UnitiOS
header.labels<br>footer.labelsAn array of labels to display. Only use if there are more then one.ArrayiOS
header.label.text<br>footer.label.textThe plain text to display. Use %ld to indicate where to insert the page index.<br>For example "Page %ld" would result into "Page 1", "Page 2", ...StringiOS
header.label.top<br>header.label.right<br>header.label.left<br>header.label.bottom<br>footer.label.*The relative position where to place the label within the footer or header area.UnitiOS
header.label.font<br>footer.label.fontThe font attributes for the label.ObjectiOS
header.label.showPageIndex<br>footer.label.showPageIndexSet to true if you want to display the page index.<br>BooleaniOS

The Unit type can be either a (float) number or a string with a special suffix.

Direct Print

For iOS its possible to send the content directly to the printer without any dialog. Todo so pass the network URL as an option:

cordova.plugins.printer.print(content, { printer: 'ipp://...' });

To let the user pick an available printer:

cordova.plugins.printer.pick(function (url) {});

It's possible to specify the position of the picker:

cordova.plugins.printer.pick({ top: 40, left: 30 }, callback);

Note: By passing an invalid URL, the application will throw an Unable to connect to (null) exception and possibly crash.

Printable Document Types

The list of supported document types differ between mobile platforms. As of writing, Windows UWP only supports HTML and plain text.

To get a list of all printable document types:

cordova.plugins.printer.getPrintableTypes(callback);

To check if printing is supported in general:

cordova.plugins.printer.canPrintItem(callback);

Or in particular:

cordova.plugins.printer.canPrintItem('file://css/index.css', callback);

Sample

var options = {
    font: {
        size: 22,
        italic: true,
        align: 'center'
    },
    header: {
        height: '6cm',
        label: {
            text: "\n\nDie Freuden",
            font: {
                bold: true,
                size: 37,
                align: 'center'
            }
        }
    },
    footer: {
        height: '4cm',
        label: {
            text: 'Johann Wolfgang von Goethe, 1749-1832, deutscher Dichter, Naturforscher',
            font: { align: 'center' }
        }
    }
};

cordova.plugins.printer.print("Es flattert um die Quelle\nDie wechselnde Libelle,...", options);

The result will look like this for iOS:

ttt

Installation

Execute from the projects root folder:

$ cordova plugin add cordova-plugin-printer

Or install a specific version:

$ cordova plugin add cordova-plugin-printer@VERSION

Or install the latest head version:

$ cordova plugin add https://github.com/katzer/cordova-plugin-printer.git

Or install from local source:

$ cordova plugin add <path> --nofetch --nosave

Then execute:

cordova build

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

This software is released under the Apache 2.0 License.

Made with :yum: from Leipzig

© 2013 appPlant GmbH