src | ||
www | ||
.gitignore | ||
CHANGELOG.md | ||
LICENSE | ||
plugin.xml | ||
README.md |
Cordova Print Plugin
Cordova plugin to print HTML documents using AirPrint and Android Printing Framework.
‼️ Choose the right branch for you! ‼️
The plugin provides multiple branches to support different printer types and android versions while AirPrint is supported with each one.
- master Branch for iOS and Android >= 4.4 (>= v0.7.x)
- google-cloud-print Branch for Android <= 4.3 (~> v0.6.x)
- Deprecated network-printer Branch for Android <= 4.3 (<= v0.5.x)
About Apple AirPrint
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.
AirPrint printers are available for these devices when using the latest version of iOS available for them:
- iPad (all models)
- iPhone (3GS or later)
- iPod touch (3rd generation or later)
Android Printing Framework
Printing in Android 4.4 and later is provided by the Printing framework. By default, 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.
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.
Supported Printers
Printing is supported on AirPrint- and Google Cloud Print-enabled printers or from Android devices to compatible network printers over Wi-Fi. The following pages contain more information:
- AirPrint-enabled printers: http://www.apple.com/ipad/features/airprint.html
- Enabling AirPrint on your computer: http://reviews.cnet.com/8301-19512_7-20023976-233.html, or http://www.ecamm.com/mac/printopia/
- Google Cloud-ready printers: http://www.google.com/cloudprint/learn/printers.html
- Connect network printers with Google Cloud Print: https://support.google.com/cloudprint/answer/1686197?rd=1
- Printing with the Android Printing Framework: http://www.techotopia.com/index.php/Printing_with_the_Android_Printing_Framework
Plugin's Purpose
This Cordova plugin serves as a platform independent JavaScript bridge to call the underlying native SDKs.
Supported Platforms
-
iOS (Print from iOS devices to AirPrint compatible printers)
See Drawing and Printing Guide for iOS for detailed informations and screenshots. -
Android KitKat (Print from Android devices to compatible printers over Wi-Fi or cloud-hosted services such as Google Cloud Print)
Installation
The plugin can either be installed from git repository, from local file system through the Command-line Interface. Or cloud based through PhoneGap Build.
Local development environment
From master:
# ~~ from master ~~
cordova plugin add https://github.com/katzer/cordova-plugin-printer.git
from a local folder:
# ~~ local folder ~~
cordova plugin add de.appplant.cordova.plugin.printer --searchpath path/to/plugin
or to use the last stable version:
# ~~ stable version ~~
cordova plugin add de.appplant.cordova.plugin.printer
PhoneGap Build
Add the following xml to your config.xml to always use the latest version of this plugin:
<gap:plugin name="de.appplant.cordova.plugin.printer" />
or to use an specific version:
<gap:plugin name="de.appplant.cordova.plugin.printer" version="0.7.0" />
More informations can be found here.
Removing the Plugin
Through the Command-line Interface:
cordova plugin rm de.appplant.cordova.plugin.printer
ChangeLog
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
toisAvailable
- [enhancement:] New print options like
name
,landscape
orduplex
- [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 to get the full changelog for the plugin.
Using the plugin
The plugin creates the object cordova.plugins.printer
with the following methods:
- printer.isAvailable
- printer.print
- [printer.print][printerPicker] (iOS only)
Plugin initialization
The plugin and its methods are not available before the deviceready event has been fired.
document.addEventListener('deviceready', function () {
// cordova.plugins.printer is now available
}, false);
Find out if printing is available on the device
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.
/**
* 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');
}
);
Send content to a printer
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 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 to get an overview on how to use the plugin.
/**
* 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
// 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
// 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
// Either a DOM node or a string
var page = '<h1>Hello Document</h1>';
cordova.plugins.printer.print(page, 'Document.html', function () {
alert('printing finished or canceled')
});
4. Print remote web page
cordova.plugins.printer.print('http://blackberry.de', 'BB!!!', function () {
alert('printing finished or canceled')
});
5. Adjust the page
cordova.plugins.printer.print('123', { name:'Document.html', landscape:true }, function () {
alert('printing finished or canceled')
});
6. Custom size and position on iPad
// 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 } });
Display printer picker (iOS 8.0+ only)
Displays a system interface allowing the user to select an available printer. The callback function will return the network URL of the selected printer (null if no printer selected). The URL can be passed into the print function (printerId option), allowing a page to be printed without prompting the user. This feature is only available in iOS 8.0 and later.
Available Options
Name | Description | Type | Support |
---|---|---|---|
bounds | The Size and position of the printer picker view | Array | iPad |
Examples
1. Display the picker
cordova.plugins.printer.printerPicker(function (printerId) {
alert(printerId)
});
2. Display picker at particular point on the screen and print current page to the selected printer
cordova.plugins.printer.printerPicker(function (printerId) {
if(printerId)
{
// URI for the index.html
var page = location.href;
cordova.plugins.printer.print(page, {name: 'Document.html', printerId: printerId }, function () {
alert('Printing finished?')
});
}
else
{
alert('Printer not selected');
}
}, { bounds: { left:100, top:300, width:0, height: 0 } } );
Quirks
Adding Page Breaks to Printouts
Use the 'page-break-before' property to specify a page break, e.g.
<p>
First page.
</p>
<p style="page-break-before: always">
Second page.
</p>
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
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
This software is released under the Apache 2.0 License.
© 2013-2014 appPlant UG, Inc. All rights reserved