Cordova plugin to print HTML documents using AirPrint and Android Printing Framework.
npm install de.appplant.cordova.plugin.printer
master
v0.6
v0.5
EXAMPLE :point_right:
Cordova Print Plugin
====================
[Cordova][cordova] plugin to print HTML documents using [__AirPrint__][AirPrint] and [__Android Printing Framework__][APF].
:bangbang: __Choose the right branch for you!__ :bangbang:
The plugin provides multiple branches to support different printer types and android versions while _AirPrint_ is supported with each one.
- [master Branch][master_branch] for iOS and Android >= 4.4 (>= v0.7.x)
- [google-cloud-print Branch][google-cloud-print_branch] for Android <= 4.3 (~> v0.6.x)
- __Deprecated__ [network-printer Branch][network-printer_branch] for Android <= 4.3 (<= v0.5.x)
- Android KitKat (Print from Android devices to compatible printers over Wi-Fi or cloud-hosted services such as Google Cloud Print)
bash
npm install de.appplant.cordova.plugin.printer
`$3
From master:
`bash
~~ from master ~~
cordova plugin add https://github.com/katzer/cordova-plugin-printer.git
`
from a local folder:
`bash
~~ local folder ~~
cordova plugin add de.appplant.cordova.plugin.printer --searchpath path/to/plugin
`
or to use the last stable version:
`bash
~~ stable version ~~
cordova plugin add de.appplant.cordova.plugin.printer
`$3
`javascript
//package.js
Cordova.depends({
'de.appplant.cordova.plugin.printer': '0.7.2'// Meteor 1.3
//'de.appplant.cordova.plugin.printer': '0.7.1-dev'// Meteor 1.2
});
`$3
Add the following xml to your config.xml to always use the latest version of this plugin:
`xml
`
or to use an specific version:
`xml
`
More informations can be found [here][PGB_plugin].$3
Through the [Command-line Interface][CLI]:
`bash
cordova plugin rm de.appplant.cordova.plugin.printer
`
ChangeLog
###Version 0.7.2 (01.06.2016)
- [enhancement:] Cordova 6.0.0 / iOS9 support.
- [enhancement:] Publish on NPM.
- [bugfix:] iOS device detection.#### Version 0.7.1 (23.04.2015)
- [bugfix:]
isAvailable does not block the main thread anymore.
- [bugfix:] iPad+iOS8 incompatibility (Thanks to __zmagyar__)
- [enhancement:] Print-View positioning on iPad
- [enhancement:] Send direct to printer when printerId: is specified.#### Version 0.7.0 (12.09.2014)
- Android Printing Framework support
- [__change__:] Renamed
isServiceAvailable to isAvailable
- [enhancement:] New print options like name, landscape or duplex
- [enhancement:] Ability to print remote content via URI
- [enhancement:] Callback support
- [bugfix:] isAvailable does not block the main thread anymore.#### Further informations
- See [CHANGELOG.md][changelog] to get the full changelog for the plugin.
Using the plugin
The plugin creates the object cordova.plugins.printer with the following methods:1. [printer.isAvailable][available]
2. [printer.print][print]
$3
The plugin and its methods are not available before the deviceready event has been fired.`javascript
document.addEventListener('deviceready', function () {
// cordova.plugins.printer is now available
}, false);
`$3
The device his printing capabilities can be reviewed through the printer.isAvailable interface.
You can use this function to hide print functionality from users who will be unable to use it.
The method takes a callback function, passed to which is a boolean property. Optionally you can assign the scope in which the callback will be executed as a second parameter (default to window).__Note:__ Printing is only available on devices capable of multi-tasking (iPhone 3GS, iPhone 4 etc.) running iOS 4.2 or later or Android KitKat and above.
`javascript
/**
* Checks if the printer service is avaible (iOS)
* or if connected to the Internet (Android).
*
* @param {Function} callback
* A callback function
* @param {Object?} scope
* The scope of the callback (default: window)
*
* @return {Boolean}
*/
cordova.plugins.printer.isAvailable(
function (isAvailable) {
alert(isAvailable ? 'Service is available' : 'Service NOT available');
}
);
`$3
Content can be send to a printer through the printer.print interface.
The method takes a string or a HTML DOM node. The string can contain HTML content or an URI pointing to another web page. Optional parameters allows to specify the name of the document and a callback. The callback will be called if the user cancels or completes the print job.#### Available Options
| Name | Description | Type | Support |
| ---- | ----------- |:----:| -------:|
| name | The name of the print job and of the document | String | all |
| printerId| The network URL to the printer. | String | iOS |
| duplex | Specifies the duplex mode to use for the print job.
Either double-sided (duplex:true) or single-sided (duplex:false).
Double-sided by default. | Boolean | iOS |
| landscape| The orientation of the printed content, portrait or landscape.
_Portrait_ by default. | Boolean | all |
| graystyle | If your application only prints black text, setting this property to _true_ can result in better performance in many cases.
_False_ by default. | Boolean | all |
| bounds | The Size and position of the print view | Array | iPad |
#### Further informations
- See the [isAvailable][available] method to find out if printing is available on the device.
- 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.
- See the [examples][examples] to get an overview on how to use the plugin.
`javascript
/**
* Sends the content to the Google Cloud Print service.
*
* @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
* @param {Function?} callback
* A callback function
* @param {Object?} scope
* The scope of the callback (default: window)
*/
cordova.plugins.printer.print(content, options, callback, scope);
`
Examples
__NOTE:__ All CSS rules needs to be embedded or accessible via absolute URLs in order to print out HTML encoded content.#### 1. Print the whole HTML page
`javascript
// URI for the index.html
var page = location.href;cordova.plugins.printer.print(page, 'Document.html', function () {
alert('printing finished or canceled')
});
`#### 2. Print the content from a part of the page
`javascript
// Either a DOM node or a string
var page = document.getElementById('legal-notice');cordova.plugins.printer.print(page, 'Document.html', function () {
alert('printing finished or canceled')
});
`#### 3. Print custom specific content
`javascript
// Either a DOM node or a string
var page = 'Hello Document
';cordova.plugins.printer.print(page, 'Document.html', function () {
alert('printing finished or canceled')
});
`#### 4. Print remote web page
`javascript
cordova.plugins.printer.print('http://blackberry.de', 'BB!!!', function () {
alert('printing finished or canceled')
});
`#### 5. Adjust the page
`javascript
cordova.plugins.printer.print('123', { name:'Document.html', landscape:true }, function () {
alert('printing finished or canceled')
});
`#### 6. Custom size and position on iPad
`javascript
// Option one
cordova.plugins.printer.print('123', { bounds:[40, 30, 0, 0] });
// Option two
cordova.plugins.printer.print('123', { bounds:{ left:40, top:30, width:0 height:0 } });
`
Quirks
$3
Use the 'page-break-before' property to specify a page break, e.g.`html
First page.
Second page.
`See W3Schools for more more information: http://www.w3schools.com/cssref/pr_print_pagebb.asp
__Note:__ You will need to add an extra top margin to new pages.
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`)This software is released under the [Apache 2.0 License][apache2_license].
© 2013-2014 appPlant UG, Inc. All rights reserved
Maintained by Citylims
[cordova]: https://cordova.apache.org
[GCP]: http://www.google.com/cloudprint/learn/index.html
[APF]: http://www.techotopia.com/index.php/Printing_with_the_Android_Printing_Framework
[AirPrint]: http://support.apple.com/kb/ht4356
[master_branch]: #
[google-cloud-print_branch]: https://github.com/katzer/cordova-plugin-printer/tree/google-cloud-print
[network-printer_branch]: https://github.com/katzer/cordova-plugin-printer/tree/network-printer
[ios_guide]: http://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/Printing/Printing.html
[CLI]: http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface
[PGB]: http://docs.build.phonegap.com/en_US/index.html
[PGB_plugin]: https://build.phonegap.com/plugins/
[changelog]: CHANGELOG.md
[available]: #find-out-if-printing-is-available-on-the-device
[print]: #send-content-to-a-printer
[examples]: #examples
[apache2_license]: http://opensource.org/licenses/Apache-2.0
[katzer]: katzer@appplant.de
[appplant]: www.appplant.de
[npmLink]: https://www.npmjs.com/package/de.appplant.cordova.plugin.printer