2013-08-10 00:44:19 +08:00
2016-08-03 23:38:33 +08:00
< p align = "left" >
< b > < a href = "https://github.com/katzer/cordova-plugin-printer/blob/example/README.md" > SAMPLE APP< / a > :point_right:< / b >
2014-09-08 15:31:14 +08:00
< / p >
2016-08-12 17:53:41 +08:00
Cordova Print Plugin [![npm version ](https://badge.fury.io/js/cordova-plugin-printer.svg )](http://badge.fury.io/js/cordova-plugin-printer) [![Build Status ](https://travis-ci.org/katzer/cordova-plugin-printer.svg?branch=master )](https://travis-ci.org/katzer/cordova-plugin-printer)
2014-09-08 15:31:14 +08:00
====================
2016-08-03 23:38:33 +08:00
Plugin for the [Cordova][cordova] framework to print HTML from iOS, Android and Windows Universal apps.
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
< p align = "center" >
< img width = "23.8%" src = "https://github.com/katzer/cordova-plugin-printer/blob/example/images/print-ios.png" > < / img >
< img width = "26.8%" src = "https://github.com/katzer/cordova-plugin-printer/blob/example/images/print-windows.png" > < / img >
< img width = "23.8%" src = "https://github.com/katzer/cordova-plugin-printer/blob/example/images/print-android.png" > < / img >
< / p >
2014-09-08 15:31:14 +08:00
### About Apple AirPrint
2016-08-03 23:38:33 +08:00
AirPrint is an Apple™ technology that helps you create full-quality printed output without the need to download or install drivers. AirPrint is built in to many printer models from most popular printer manufacturers. Just select an AirPrint printer on your local network to print from your favorite iOS or OS X app.
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
See [Drawing and Printing Guide for iOS][ios_guide] for detailed informations.
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
### About Android Printing Framework
Starting with _KitKat_ , most Android devices have print service plugins installed to enable printing using the Google Cloud Print and Google Drive services. Print service plugins from other printer manufactures are available through the App Store though the Google Cloud Print service plugin can also be used to print from an Android device to just about any printer type and model.< br >
2014-09-08 15:31:14 +08:00
In addition to supporting physical printers, it is also possible to save printed output to your Google Drive account or locally as a PDF file on the Android device.
2016-08-03 23:38:33 +08:00
See [Building Apps with Multimedia for Android][android_guide] for detailed informations.
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
## Supported Platforms
2016-08-08 15:22:23 +08:00
- iOS 8 or newer
- Android KitKat or newer
2016-08-03 23:38:33 +08:00
- Universal Windows Platform
2013-08-10 18:05:56 +08:00
2013-08-10 18:44:28 +08:00
2016-08-03 23:38:33 +08:00
## Installation
Install the latest version:
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
cordova plugin add cordova-plugin-printer
2013-09-08 01:00:14 +08:00
2016-08-03 23:38:33 +08:00
Or a specific version:
2013-12-11 22:50:56 +08:00
2016-08-03 23:38:33 +08:00
cordova plugin add cordova-plugin-printer@VERSION
2013-08-10 18:44:28 +08:00
2016-08-03 23:38:33 +08:00
Or the latest dev version:
2013-08-10 18:44:28 +08:00
2016-08-03 23:38:33 +08:00
cordova plugin add https://github.com/katzer/cordova-plugin-printer.git
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
Or a custom version:
cordova plugin add cordova-plugin-printer --searchpath path/to/plugin
And then execute:
cordova build
2013-08-19 20:29:13 +08:00
2013-12-01 21:31:27 +08:00
2014-09-08 15:31:14 +08:00
## ChangeLog
2016-12-19 22:16:59 +08:00
#### Version 0.7.3 (19.12.2016)
- Fixed incompatibility with Android KitKat (4.4)
Known limitations
- Plugin crashes on Windows OS 10.0.14
2016-08-04 03:56:35 +08:00
#### Version 0.7.2 (03.08.2016)
2016-08-04 03:53:00 +08:00
- Finally on __NPM__
- __Windows__ support
- New __pick__ method
2016-08-16 02:24:34 +08:00
- __print__ returns boolean flag
2016-08-04 03:53:00 +08:00
- Fixed iOS build issue
- Various enhancements
2013-08-13 22:05:24 +08:00
2016-08-08 15:22:23 +08:00
Known limitations
- _check_ and _pick_ are broken on Android N
2016-08-03 23:38:33 +08:00
See [CHANGELOG.md][changelog] to get the full changelog for the plugin.
2013-08-25 02:28:11 +08:00
2013-08-10 18:44:28 +08:00
2016-08-03 23:38:33 +08:00
## Usage
2016-08-04 03:53:00 +08:00
The plugin and its methods are not available before the *deviceready* event has been fired.
2013-08-10 18:44:28 +08:00
```javascript
2014-09-08 15:31:14 +08:00
document.addEventListener('deviceready', function () {
// cordova.plugins.printer is now available
}, false);
```
2016-08-03 23:38:33 +08:00
### Check printer
The device his printing capabilities can be reviewed through the `printer.check` interface. Use this function to hide print functionality from users who will be unable to use it.
2014-09-08 15:31:14 +08:00
```javascript
/**
* Checks if the printer service is avaible (iOS)
2016-08-03 23:38:33 +08:00
* or if printer services are installed and enabled (Android).
2014-09-08 15:31:14 +08:00
*
* @param {Function} callback
* A callback function
2016-08-03 23:38:33 +08:00
* @param {Object} scope
* Optional scope of the callback
* Defaults to: window
2013-08-10 18:44:28 +08:00
*/
2016-08-03 23:38:33 +08:00
cordova.plugins.printer.check(function (avail, count) {
alert(avail ? 'Found ' + count + ' services' : 'No');
});
2013-08-10 18:44:28 +08:00
```
2016-08-03 23:38:33 +08:00
### Pick a printer
Displays a system interface allowing the user to select an available printer.
To speak with a printer directly you need to know the network address by picking them before via `printer.pick` .
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
Note that picking a printer is not supported for windows platform.
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
```javascript
/**
* Displays system interface for selecting a printer.
*
* @param {Function} callback
* A callback function
*/
cordova.plugins.printer.pick(function (uri) {
alert(uri ? uri : 'Canceled');
});
```
### Print content
Content can be send to a printer through the `printer.print` interface. The method takes a string with HTML content, an URI pointing to another web page or any DOM node.
2014-09-08 15:31:14 +08:00
```javascript
/**
2016-08-03 23:38:33 +08:00
* Sends the content to print service.
2014-09-08 15:31:14 +08:00
*
* @param {String} content
* HTML string or DOM node
* if latter, innerHTML is used to get the content
* @param {Object} options
* Options for the print job
2016-08-03 23:38:33 +08:00
* @param {Function} callback
* An optional callback function
* @param {Object} scope
* An optional scope of the callback
* Defaults to: window
2014-09-08 15:31:14 +08:00
*/
2016-08-03 23:38:33 +08:00
cordova.plugins.printer.print('< html > ..< / html > ', { duplex: 'long' }, function (res) {
alert(res ? 'Done' : 'Canceled');
});
2014-09-08 15:31:14 +08:00
```
2016-08-03 23:38:33 +08:00
The method accepts a list of attributes. Not all are supported on each platform and by each printer!
| Name | Description | Type | Platform |
2016-08-12 15:23:03 +08:00
|:---- |:----------- |:----:| --------:|
2016-08-03 23:38:33 +08:00
| name | The name of the print job and of the document | String | all |
| duplex | Specifies the duplex mode to use for the print job.< br > Either double-sided on short site (duplex:'short'), double-sided on long site (duplex:'long') or single-sided (duplex:'none').< br > Defaults to: 'none' | String | all |
| landscape| The orientation of the printed content, portrait or landscape.< br > Defaults to: false | Boolean | all |
| graystyle | If your application only prints black text, setting this property to _true_ can result in better performance in many cases.< br > Defaults to: false | Boolean | all |
| printerId | The network URL to the printer. | String | iOS |
2016-08-05 22:50:44 +08:00
| border | Set to _false_ to skip any border. Useful for fullscreen images.< br > Defaults to: true | Boolean | iOS |
2016-08-03 23:38:33 +08:00
| hidePageRange | Set to _true_ to hide the control for the page range.< br > Defaults to: false | Boolean | iOS |
| hideNumberOfCopies | Set to _true_ to hide the control for the number of copies.< br > Defaults to: false | Boolean | iOS |
| hidePaperFormat | Set to _true_ to hide the control for the paper format.< br > Defaults to: false | Boolean | iOS |
| bounds | The Size and position of the print view< br > Defaults to: [40, 30, 0, 0] | Array | iPad |
#### Further informations
- All CSS rules needs to be embedded or accessible via absolute URLs in order to print out HTML encoded content.
- The string can contain HTML content or an URI pointing to another web page.
2013-12-11 20:21:23 +08:00
2014-09-08 15:31:14 +08:00
## Examples
__NOTE:__ All CSS rules needs to be embedded or accessible via absolute URLs in order to print out HTML encoded content.
2013-08-10 18:44:28 +08:00
2016-08-03 23:38:33 +08:00
Print the whole HTML page:
2013-08-10 18:44:28 +08:00
```javascript
2014-09-12 05:27:17 +08:00
var page = location.href;
2016-08-03 23:38:33 +08:00
cordova.plugins.printer.print(page, 'Document.html');
2014-09-12 05:27:17 +08:00
```
2016-08-03 23:38:33 +08:00
Print the content from one part of the page:
2014-09-12 05:27:17 +08:00
```javascript
var page = document.getElementById('legal-notice');
2013-08-10 18:44:28 +08:00
2016-08-03 23:38:33 +08:00
cordova.plugins.printer.print(page, 'Document.html');
2014-09-08 15:31:14 +08:00
```
2016-08-03 23:38:33 +08:00
Print some custom content:
2014-09-08 15:31:14 +08:00
```javascript
var page = '< h1 > Hello Document< / h1 > ';
2016-08-03 23:38:33 +08:00
cordova.plugins.printer.print(page, 'Document.html');
2014-09-08 15:31:14 +08:00
```
2016-08-03 23:38:33 +08:00
Print a remote web page:
2014-09-12 05:27:17 +08:00
2014-09-08 15:31:14 +08:00
```javascript
2016-08-03 23:38:33 +08:00
cordova.plugins.printer.print('http://blackberry.de', 'BB10');
2013-08-10 18:44:28 +08:00
```
2016-08-03 23:38:33 +08:00
Send to printer directly:
2015-06-24 01:58:41 +08:00
```javascript
2016-08-03 23:38:33 +08:00
cordova.plugins.printer.pick(function (uri) {
cordova.plugins.printer.print(page, { printerId: uri });
2015-06-24 01:58:41 +08:00
});
```
2013-12-01 21:33:10 +08:00
## 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
2014-09-08 15:31:14 +08:00
This software is released under the [Apache 2.0 License][apache2_license].
2016-08-03 23:38:33 +08:00
Made with :yum: from Leipzig
© 2016 [appPlant GmbH][appplant]
2014-09-08 15:31:14 +08:00
[cordova]: https://cordova.apache.org
[ios_guide]: http://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/Printing/Printing.html
2016-08-03 23:38:33 +08:00
[android_guide]: https://developer.android.com/training/building-multimedia.html
2014-09-08 15:31:14 +08:00
[changelog]: CHANGELOG.md
2016-08-03 23:38:33 +08:00
[check]: #check -printer
[pick]: #pick -a-printer
[print]: #print -content
2014-09-08 15:31:14 +08:00
[apache2_license]: http://opensource.org/licenses/Apache-2.0
2014-09-12 05:39:21 +08:00
[appplant]: www.appplant.de