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 >
2019-02-06 18:55:54 +08:00
# Cordova Print Plugin <br> [![npm version](https://badge.fury.io/js/cordova-plugin-printer.svg)](http://badge.fury.io/js/cordova-plugin-printer) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![PayPayl donate button](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=L3HKQCD9UA35A "Donate once-off to this project using Paypal")
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
< img width = "280px" align = "right" hspace = "20" vspace = "10" src = "https://github.com/katzer/cordova-plugin-printer/blob/example/images/print-ios.png" >
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
Plugin for [Cordova][cordova] to print documents or photos from iOS, Android and Windows Universal apps.
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
### Supported Printer Interfaces
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
- Apple AirPrint
- Android Print
- Windows Print
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
### Supported Content
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
- HTML
- Text
- Base64
- Images
- PDF
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
### Supported Platforms
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
- Android 4.4+
- iOS 10+
- Windows 10 UWP
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
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
The plugin can be installed via [CLI][CLI] and is publicly available on [NPM][npm].
Execute from the projects root folder:
$ cordova plugin add cordova-plugin-printer
2013-09-08 01:00:14 +08:00
2019-02-06 18:55:54 +08:00
Or install a specific version:
2013-12-11 22:50:56 +08:00
2019-02-06 18:55:54 +08:00
$ cordova plugin add cordova-plugin-printer@VERSION
2013-08-10 18:44:28 +08:00
2019-02-06 18:55:54 +08:00
Or install the latest head version:
2013-08-10 18:44:28 +08:00
2019-02-06 18:55:54 +08:00
$ cordova plugin add https://github.com/katzer/cordova-plugin-printer.git
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
Or install from local source:
2016-08-03 23:38:33 +08:00
2019-02-06 18:55:54 +08:00
$ cordova plugin add < path > --nofetch --nosave
2016-08-03 23:38:33 +08:00
And then execute:
cordova build
2013-08-19 20:29:13 +08:00
2013-12-01 21:31:27 +08:00
2019-02-06 18:55:54 +08:00
## Basics
2016-12-24 01:09:18 +08:00
2019-02-06 18:55:54 +08:00
The plugin creates the object `cordova.plugins.printer` and is accessible after the *deviceready* event has been fired.
2016-12-19 22:16:59 +08:00
2019-02-06 18:55:54 +08:00
To print plain text:
2013-08-25 02:28:11 +08:00
2019-02-06 18:55:54 +08:00
```javascript
cordova.plugins.printer.print("Hello\nWorld!");
```
2013-08-10 18:44:28 +08:00
2019-02-06 18:55:54 +08:00
Or HTML+CSS:
2013-08-10 18:44:28 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.print('< h1 > Hello World!< / h1 > ');
2014-09-08 15:31:14 +08:00
```
2019-02-06 18:55:54 +08:00
Or images or documents:
2014-09-08 15:31:14 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.print('file://img/logo.png');
2013-08-10 18:44:28 +08:00
```
2019-02-06 18:55:54 +08:00
Or Base64 content:
2014-09-08 15:31:14 +08:00
2016-08-03 23:38:33 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.print('base64://...');
2016-08-03 23:38:33 +08:00
```
2019-02-06 18:55:54 +08:00
## Formatting
It's possible to pass by additional format options. The list of possible options depend on the platform and the content type:
2014-09-08 15:31:14 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.print(content, options);
2014-09-08 15:31:14 +08:00
```
2019-02-06 18:55:54 +08:00
TODO
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 |
2018-12-17 18:33:28 +08:00
| paperWidth | Ability to hint width of the paper – iOS will get a printer supported paperformat which fits the best to this width. Only works when `paperHeight` is given. Width in millimeters. | Number | iOS |
| paperHeight | Ability to hint height of the paper – iOS will get a printer paperformat which fits the best to this heigth. Only works when `paperWidth` is given. Height in millimeters. | Number | iOS |
| paperCutLength | On roll-fed printers you can decide after how many milimeters the printer cuts the paper. | Number | iOS |
2016-08-03 23:38:33 +08:00
| bounds | The Size and position of the print view< br > Defaults to: [40, 30, 0, 0] | Array | iPad |
2019-02-06 18:55:54 +08:00
Use `getDefaults()` or `setDefaults()` to specify default options.
2016-08-03 23:38:33 +08:00
2013-12-11 20:21:23 +08:00
2019-02-06 18:55:54 +08:00
## Direct Print
2013-08-10 18:44:28 +08:00
2019-02-06 18:55:54 +08:00
For iOS its possible to send the content directly to the printer without any dialog. Todo so pass the network URL as an option:
2016-08-03 23:38:33 +08:00
2013-08-10 18:44:28 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.print(content, { printer: 'ipp://...' });
2014-09-12 05:27:17 +08:00
```
2019-02-06 18:55:54 +08:00
To let the user pick an available printer:
2016-08-03 23:38:33 +08:00
2014-09-12 05:27:17 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.pick(function (url) {});
2014-09-08 15:31:14 +08:00
```
2019-02-06 18:55:54 +08:00
__Note:__ By passing an invalid URL, the application will throw an `Unable to connect to (null)` exception and possibly crash.
2016-08-03 23:38:33 +08:00
2014-09-08 15:31:14 +08:00
2019-02-06 18:55:54 +08:00
## 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:
```javascript
cordova.plugins.printer.getPrintableTypes(callback);
2014-09-08 15:31:14 +08:00
```
2019-02-06 18:55:54 +08:00
To check if printing is supported in general:
2014-09-12 05:27:17 +08:00
2014-09-08 15:31:14 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.canPrintItem(callback);
2013-08-10 18:44:28 +08:00
```
2019-02-06 18:55:54 +08:00
Or in articular:
2015-06-24 01:58:41 +08:00
```javascript
2019-02-06 18:55:54 +08:00
cordova.plugins.printer.canPrintItem('file://css/index.css', callback);
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
2019-02-06 18:55:54 +08:00
2013-12-01 21:33:10 +08:00
## 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
2019-02-06 18:55:54 +08:00
© 2013 [appPlant GmbH][appplant]
2014-09-08 15:31:14 +08:00
[cordova]: https://cordova.apache.org
2019-02-06 18:55:54 +08:00
[CLI]: http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface
[npm]: https://www.npmjs.com/package/cordova-plugin-printer
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