Merge branch 'kitkat'

Conflicts:
	CHANGELOG.md
	README.md
	plugin.xml
	src/android/Printer.java
	src/ios/APPPrinter.m
	www/printer.js
This commit is contained in:
Sebastián Katzer 2014-09-12 11:45:58 +02:00
commit b28d85ca9f
8 changed files with 463 additions and 517 deletions

View File

@ -1,33 +1,8 @@
##ChangeLog ## ChangeLog
#### Version 0.6.0 (not yet released) #### Version 0.7.0 (not yet released)
- [feature]: Added Windows8 support<br> - Android Printing Framework support
*Thanks to* ***pirvudoru*** - [__change__:] Renamed `isServiceAvailable` to `isAvailable`
- [enhancement:] New print options like `name`, `landscape` or `duplex`
#### Version 0.5.3 (13.07.2014) - [enhancement:] Ability to print remote content via URI
- [bugfix]: Printing wasn't possible because `isServiceAvailable` returns False IOS - [enhancement:] Callback support
- [bugfix]: Using plugin prevents openDatabase() on Android - [bugfix:] `isAvailable` does not block the main thread anymore.
#### Version 0.5.2 (22.03.2014)
- [bugfix:] `isServiceAvailable` on Android did not return a list of available printing apps.
#### Version 0.5.1 (15.12.2013)
- Removed Android KitKat support *(See kitkat branch)*
#### Version 0.5.0 (yanked)
- Release under the Apache 2.0 license.
- [***change:***] Removed the `callback` property from the `print` interface.
- [enhancement:] Added Android KitKat support<br>
*Based on the Print Android plugin made by* ***Eion Robb***
#### Version 0.4.0 (24.08.2013)
- [feature]: Added Android support<br>
*Based on the Print Android plugin made by* ***Eion Robb***
- [feature]: `print()` accepts a 4th arguments for platform specific properties.
- [change]: the callback of `print()` will be called with a result code about the user action.
#### Version 0.2.1 (13.08.2013)
- [feature]: Support for callback scopes.
#### Version 0.2.0 (11.08.2013)
- [feature]: Added iOS support<br>
*Based on the Print iOS plugin made by* ***Randy McMillan***

202
README.md
View File

@ -1,36 +1,77 @@
Cordova Printer-Plugin
======================
A bunch of printing plugins for Cordova 3.x.x <p align="right">
<b><a href="#">master</a></b>
<span>&nbsp;</span>
<a href="https://github.com/katzer/cordova-plugin-printer/tree/google-cloud-print">v0.6</a>
<span>&nbsp;</span>
<a href="https://github.com/katzer/cordova-plugin-printer/tree/network-printer">v0.5</a>
</p>
Cordova Print Plugin
====================
[Cordova][cordova] plugin to print HTML documents using [__AirPrint__][AirPrint] and [__Android Printing Framework__][APF].
:bangbang:&nbsp;__Choose the right branch for you!__&nbsp;: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)
### 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.<br>
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)
<img src="http://static1.businessinsider.com/image/4cf67b8149e2aeb00b020000/only-12-printers-work-with-apples-airprint-heres-the-list.jpg" />
### 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.<br>
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.
<img src="http://androidtopnews.com/wp-content/uploads/2013/10/46888__android-4.4-kitkat-official-4.jpg" />
### 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.
by Sebastián Katzer ([github.com/katzer](https://github.com/katzer))
## Supported Platforms ## Supported Platforms
- **iOS** *(Print from iOS devices to AirPrint compatible printers)*<br> - **iOS** *(Print from iOS devices to AirPrint compatible printers)*<br>
See [Drawing and Printing Guide for iOS](http://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/Printing/Printing.html) for detailed informations and screenshots. See [Drawing and Printing Guide for iOS][ios_guide] for detailed informations and screenshots.
- **Android** *(Print through 3rd party printing apps)* - **Android KitKat** *(Print from Android devices to compatible printers over Wi-Fi or cloud-hosted services such as Google Cloud Print)*
## Installation ## Installation
The plugin can either be installed from git repository or from local file system through the [Command-line Interface][CLI].<br> The plugin can either be installed from git repository, from local file system through the [Command-line Interface][CLI]. Or cloud based through [PhoneGap Build][PGB].
Or cloud based through [PhoneGap Build][PGB].
### Local development environment ### Local development environment
From master: From master:
```bash ```bash
# ~~ from master ~~ # ~~ from master ~~
cordova plugin add https://github.com/katzer/cordova-plugin-printer.git && cordova prepare cordova plugin add https://github.com/katzer/cordova-plugin-printer.git
``` ```
from a local folder: from a local folder:
```bash ```bash
# ~~ local folder ~~ # ~~ local folder ~~
cordova plugin add de.appplant.cordova.plugin.printer --searchpath path/to/plugin && cordova prepare cordova plugin add de.appplant.cordova.plugin.printer --searchpath path/to/plugin
``` ```
or to use the last stable version: or to use the last stable version:
```bash ```bash
# ~~ stable version ~~ # ~~ stable version ~~
cordova plugin add de.appplant.cordova.plugin.printer && cordova prepare cordova plugin add de.appplant.cordova.plugin.printer
``` ```
### PhoneGap Build ### PhoneGap Build
@ -40,7 +81,7 @@ Add the following xml to your config.xml to always use the latest version of thi
``` ```
or to use an specific version: or to use an specific version:
```xml ```xml
<gap:plugin name="de.appplant.cordova.plugin.printer" version="0.5.2" /> <gap:plugin name="de.appplant.cordova.plugin.printer" version="0.7.0" />
``` ```
More informations can be found [here][PGB_plugin]. More informations can be found [here][PGB_plugin].
@ -52,44 +93,44 @@ cordova plugin rm de.appplant.cordova.plugin.printer
## ChangeLog ## ChangeLog
#### Version 0.6.0 (not yet released) #### Version 0.7.0 (not yet released)
- [feature]: Added Windows8 support<br> - Android Printing Framework support
*Thanks to* ***pirvudoru*** - [__change__:] Renamed `isServiceAvailable` to `isAvailable`
- [enhancement:] New print options like `name`, `landscape` or `duplex`
#### Version 0.5.3 (13.07.2014) - [enhancement:] Ability to print remote content via URI
- [bugfix]: Printing wasn't possible because `isServiceAvailable` returns False IOS - [enhancement:] Callback support
- [bugfix]: Using plugin prevents openDatabase() on Android - [bugfix:] `isAvailable` does not block the main thread anymore.
#### Further informations #### Further informations
- See [CHANGELOG.md][changelog] to get the full changelog for the plugin. - See [CHANGELOG.md][changelog] to get the full changelog for the plugin.
## Using the plugin ## Using the plugin
The plugin creates the object `window.plugin.printer` with the following methods: The plugin creates the object `cordova.plugins.printer` with the following methods:
1. [plugin.printer.isServiceAvailable][available] 1. [printer.isAvailable][available]
2. [plugin.printer.print][print] 2. [printer.print][print]
### Plugin initialization ### Plugin initialization
The plugin and its methods are not available before the *deviceready* event has been fired. The plugin and its methods are not available before the *deviceready* event has been fired.
```javascript ```javascript
document.addEventListener('deviceready', function () { document.addEventListener('deviceready', function () {
// window.plugin.printer is now available // cordova.plugins.printer is now available
}, false); }, false);
``` ```
### Find out if printing is available on the device ### Find out if printing is available on the device
The device his printing capabilities can be reviewed through the `printer.isServiceAvailable` interface. 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.<br> You can use this function to hide print functionality from users who will be unable to use it.<br>
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*). 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 through a pre-installed printer app (Android).<br> __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.<br>
```javascript ```javascript
/** /**
* Checks if the printer service is avaible (iOS) * Checks if the printer service is avaible (iOS)
* or if a printing app is installed on the device (Android). * or if connected to the Internet (Android).
* *
* @param {Function} callback * @param {Function} callback
* A callback function * A callback function
@ -98,7 +139,7 @@ __Note:__ Printing is only available on devices capable of multi-tasking (iPhone
* *
* @return {Boolean} * @return {Boolean}
*/ */
window.plugin.printer.isServiceAvailable( cordova.plugins.printer.isAvailable(
function (isAvailable) { function (isAvailable) {
alert(isAvailable ? 'Service is available' : 'Service NOT available'); alert(isAvailable ? 'Service is available' : 'Service NOT available');
} }
@ -107,61 +148,93 @@ window.plugin.printer.isServiceAvailable(
### Send content to a printer ### Send content to a printer
Content can be send to a printer through the `printer.print` interface.<br> Content can be send to a printer through the `printer.print` interface.<br>
The method takes a string or a HTML DOM node. 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| An identifier of the printer to use for the print job. | String | iOS |
| duplex | Specifies the duplex mode to use for the print job.<br>Either double-sided (duplex:true) or single-sided (duplex:false).<br>Double-sided by default. | Boolean | iOS |
| landscape| The orientation of the printed content, portrait or landscape.<br>_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.<br>_False_ by default. | Boolean | all |
#### Further informations #### Further informations
- See the [isServiceAvailable][available] method to find out if printing is available on the device. - See the [isAvailable][available] method to find out if printing is available on the device.
- All required CSS rules needs to be included in order to print HTML encoded content. - All CSS rules needs to be embedded or accessible via absolute URLs in order to print out HTML encoded content.
- On Android the functionality between different print apps may vary. - 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 ```javascript
/** /**
* Sends the content to a printer app or service. * Sends the content to the Google Cloud Print service.
* *
* @param {String} content * @param {String} content
* HTML string or DOM node * HTML string or DOM node
* if latter, innerHTML is used to get the content * if latter, innerHTML is used to get the content
* @param {Object?} options * @param {Object} options
* Platform specific options * Options for the print job
* @param {Function?} callback
* A callback function
* @param {Object?} scope
* The scope of the callback (default: window)
*/ */
window.plugin.printer.print(content, options); cordova.plugins.printer.print(content, options, callback, scope);
``` ```
## Example ## Examples
The following exmaple demonstrates how to print out the whole HTML page. __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 ```javascript
// Get the content // URI for the index.html
var page = document.body; var page = location.href;
// Pass to the printer cordova.plugins.printer.print(page, 'Document.html', function () {
window.plugin.printer.print(page); alert('printing finished or canceled')
});
``` ```
#### 2. Print the content from a part of the page
## Platform specifics
### Get all available printing apps on Android
The callback function will be called with a second argument which is an array, indicating which printer apps are available for printing.
```javascript ```javascript
window.plugin.printer.isServiceAvailable( // Either a DOM node or a string
function (isAvailable, installedAppIds) { var page = document.getElementById('legal-notice');
alert('The following print apps are installed on your device: ' + installedAppIds.join(', '));
} cordova.plugins.printer.print(page, 'Document.html', function () {
); alert('printing finished or canceled')
});
``` ```
### Specify printing app on Android #### 3. Print custom specific content
An App-ID can be assigned as a platform configuration to indicate which 3rd party printing app shall be used. Otherwise the first found application will be used.
```javascript ```javascript
window.plugin.printer.print(page, { appId: 'epson.print' }); // 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
```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')
});
```
## Quirks ## Quirks
### Testing in the iOS Simulator ### Testing in the iOS Simulator
There's no need to waste lots of paper when testing - if you're using the iOS simulator, select File->Open Printer Simulator to open some dummy printers (print outs will appear as PDF files). There's no need to waste lots of paper when testing - if you're using the iOS simulator, select _File -> Open Printer Simulator_ to open some dummy printers (print outs will appear as PDF files).
### Adding Page Breaks to Printouts ### Adding Page Breaks to Printouts
Use the 'page-break-before' property to specify a page break, e.g. Use the 'page-break-before' property to specify a page break, e.g.
@ -178,14 +251,9 @@ Second page.
See W3Schools for more more information: http://www.w3schools.com/cssref/pr_print_pagebb.asp 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. __Note:__ You will need to add an extra top margin to new pages.
### Printing on Real Printers (iOS)
Printing is only supported on AirPrint-enabled printers or with the use of third-party software on your computer. 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/
## Contributing ## Contributing
1. Fork it 1. Fork it
@ -202,12 +270,20 @@ This software is released under the [Apache 2.0 License][apache2_license].
[cordova]: https://cordova.apache.org [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 [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/3.3.0/index.html [PGB]: http://docs.build.phonegap.com/en_US/index.html
[PGB_plugin]: https://build.phonegap.com/plugins/676 [PGB_plugin]: https://build.phonegap.com/plugins/
[changelog]: CHANGELOG.md [changelog]: CHANGELOG.md
[available]: #find-out-if-printing-is-available-on-the-device [available]: #find-out-if-printing-is-available-on-the-device
[print]: #send-content-to-a-printer [print]: #send-content-to-a-printer
[examples]: #examples
[apache2_license]: http://opensource.org/licenses/Apache-2.0 [apache2_license]: http://opensource.org/licenses/Apache-2.0
[katzer]: katzer@appplant.de [katzer]: katzer@appplant.de
[appplant]: www.appplant.de [appplant]: www.appplant.de

View File

@ -3,17 +3,28 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" <plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
id="de.appplant.cordova.plugin.printer" id="de.appplant.cordova.plugin.printer"
version="0.5.3"> version="0.7.0-dev">
<name>Printer</name> <name>AirPrint and Android Printing Framework Plugin</name>
<description>
Cordova plugin to print HTML documents using AirPrint
and Android Printing Framework.
</description>
<repo>
https://github.com/katzer/cordova-plugin-printer.git
</repo>
<keywords>
print, printer, ios, android, google cloud, air print
</keywords>
<description>A bunch of printig plugins for Cordova 3.x.x</description>
<repo>https://github.com/katzer/cordova-plugin-printer.git</repo>
<keywords>print, printer, ios, android, windows 8</keywords>
<license>Apache 2.0</license> <license>Apache 2.0</license>
<author>Sebastián Katzer</author> <author>Sebastián Katzer</author>
<!-- cordova -->
<engines> <engines>
<engine name="cordova" version=">=3.0.0" /> <engine name="cordova" version=">=3.0.0" />
</engines> </engines>
@ -21,6 +32,7 @@
<!-- interface --> <!-- interface -->
<js-module src="www/printer.js" name="Printer"> <js-module src="www/printer.js" name="Printer">
<clobbers target="plugin.printer" /> <clobbers target="plugin.printer" />
<clobbers target="cordova.plugins.printer" />
</js-module> </js-module>
<!-- ios --> <!-- ios -->
@ -33,6 +45,7 @@
<header-file src="src/ios/APPPrinter.h" /> <header-file src="src/ios/APPPrinter.h" />
<source-file src="src/ios/APPPrinter.m" /> <source-file src="src/ios/APPPrinter.m" />
</platform> </platform>
<!-- android --> <!-- android -->
@ -45,11 +58,4 @@
<source-file src="src/android/Printer.java" target-dir="src/de/appplant/cordova/plugin/printer" /> <source-file src="src/android/Printer.java" target-dir="src/de/appplant/cordova/plugin/printer" />
</platform> </platform>
<!-- windows8
<platform name="windows8">
<js-module src="src/windows8/PrinterProxy.js" name="PrinterProxy">
<clobbers target="" />
</js-module>
</platform>-->
</plugin> </plugin>

View File

@ -17,85 +17,67 @@
KIND, either express or implied. See the License for the KIND, either express or implied. See the License for the
specific language governing permissions and limitations specific language governing permissions and limitations
under the License. under the License.
*/ */
package de.appplant.cordova.plugin.printer; package de.appplant.cordova.plugin.printer;
import java.io.File; import org.apache.cordova.CallbackContext;
import java.io.FileOutputStream; import org.apache.cordova.CordovaPlugin;
import java.io.IOException; import org.apache.cordova.PluginResult;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Context;
import android.content.pm.PackageInfo; import android.os.Build;
import android.content.pm.PackageManager; import android.os.Looper;
import android.graphics.Bitmap; import android.print.PrintAttributes;
import android.graphics.Canvas; import android.print.PrintDocumentAdapter;
import android.graphics.Picture; import android.print.PrintJob;
import android.net.Uri; import android.print.PrintManager;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import org.apache.cordova.CordovaPlugin; @TargetApi(19)
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
public class Printer extends CordovaPlugin { public class Printer extends CordovaPlugin {
private CallbackContext ctx; private WebView view;
private CallbackContext command;
private static final String DEFAULT_DOC_NAME = "unknown";
/** /**
* Auflistung von App-IDs, welche den Content ausdrucken können * Executes the request.
*
* This method is called from the WebView thread.
* To do a non-trivial amount of work, use:
* cordova.getThreadPool().execute(runnable);
*
* To run on the UI thread, use:
* cordova.getActivity().runOnUiThread(runnable);
*
* @param action The action to execute.
* @param rawArgs The exec() arguments in JSON form.
* @param callbackContext The callback context used when calling back into JavaScript.
* @return Whether the action was valid.
*/ */
private String printAppIds[] = {
"kr.co.iconlab.BasicPrintingProfile", // Bluetooth Smart Printing
"com.blueslib.android.app", // Bluetooth SPP Printer API
"com.brother.mfc.brprint", // Brother iPrint&Scan
"com.brother.ptouch.sdk", // Brother Print Library
"jp.co.canon.bsd.android.aepp.activity", // Canon Easy-PhotoPrint
"com.pauloslf.cloudprint", // Cloud Print
"com.dlnapr1.printer", // CMC DLNA Print Client
"com.dell.mobileprint", // Dell Mobile Print
"com.printjinni.app.print", // PrintJinni
"epson.print", // Epson iPrint
"jp.co.fujixerox.prt.PrintUtil.PCL", // Fuji Xerox Print Utility
"jp.co.fujixerox.prt.PrintUtil.Karin", // Fuji Xeros Print&Scan (S)
"com.hp.android.print", // HP ePrint
"com.blackspruce.lpd", // Let's Print Droid
"com.threebirds.notesprint", // NotesPrint print your notes
"com.xerox.mobileprint", // Print Portal (Xerox)
"com.zebra.kdu", // Print Station (Zebra)
"net.jsecurity.printbot", // PrintBot
"com.dynamixsoftware.printhand", // PrintHand Mobile Print
"com.dynamixsoftware.printhand.premium", // PrintHand Mobile Print Premium
"com.sec.print.mobileprint", // Samsung Mobile Print
"com.rcreations.send2printer", // Send 2 Printer
"com.ivc.starprint", // StarPrint
"com.threebirds.easyviewer", // WiFi Print
"com.woosim.android.print", // Woosim BT printer
"com.woosim.bt.app", // WoosimPrinter
"com.zebra.android.zebrautilities", // Zebra Utilities
};
@Override @Override
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { public boolean execute (String action, JSONArray args,
// Etwas soll ausgedruckt werden CallbackContext callbackContext) throws JSONException {
if ("print".equals(action)) {
print(args, callbackContext); command = callbackContext;
if (action.equalsIgnoreCase("isAvailable")) {
isAvailable();
return true; return true;
} }
// Es soll überprüft werden, ob ein Dienst zum Ausdrucken von Inhalten zur Verfügung steht if (action.equalsIgnoreCase("print")) {
if ("isServiceAvailable".equals(action)) { print(args);
isServiceAvailable(callbackContext);
return true; return true;
} }
@ -105,246 +87,140 @@ public class Printer extends CordovaPlugin {
} }
/** /**
* Überprüft, ob ein Drucker zur Verfügung steht. * Informs if the device is able to print documents.
* A Internet connection is required to load the cloud print dialog.
*/ */
private void isServiceAvailable (CallbackContext ctx) { private void isAvailable () {
JSONArray appIds = this.getInstalledAppIds(); Boolean supported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
Boolean available = appIds.length() > 0; PluginResult result = new PluginResult(PluginResult.Status.OK, supported);
JSONArray args = new JSONArray();
PluginResult result;
args.put(available); command.sendPluginResult(result);
args.put(appIds);
result = new PluginResult(PluginResult.Status.OK, args);
ctx.sendPluginResult(result);
} }
/** /**
* Druckt den HTML Content aus. * Loads the HTML content into the web view and invokes the print manager.
*
* @param args
* The exec arguments as JSON
*/ */
private void print (final JSONArray args, CallbackContext ctx) { private void print (final JSONArray args) {
final Printer self = this; final String content = args.optString(0, "<html></html>");
final JSONObject props = args.optJSONObject(1);;
this.ctx = ctx;
cordova.getActivity().runOnUiThread( new Runnable() { cordova.getActivity().runOnUiThread( new Runnable() {
public void run() {
JSONObject platformConfig = args.optJSONObject(1);
String appId = self.getPrintAppId(platformConfig);
if (appId == null) {
self.ctx.success(4);
return;
};
String content = args.optString(0, "<html></html>");
Intent controller = self.getPrintController(appId);
self.adjustSettingsForPrintController(controller);
self.loadContentIntoPrintController(content, controller);
self.startPrinterApp(controller);
}
});
}
/**
* Gibt die zu verwendende App-ID an.
*/
private String getPrintAppId (JSONObject platformConfig) {
String appId = platformConfig.optString("appId", null);
if (appId != null) {
return (this.isAppInstalled(appId)) ? appId : null;
} else {
return this.getFirstInstalledAppId();
}
}
/**
* Erstellt den Print-View.
*/
private Intent getPrintController (String appId) {
String intentId = "android.intent.action.SEND";
if (appId.equals("com.rcreations.send2printer")) {
intentId = "com.rcreations.send2printer.print";
} else if (appId.equals("com.dynamixsoftware.printershare")) {
intentId = "android.intent.action.VIEW";
} else if (appId.equals("com.hp.android.print")) {
intentId = "org.androidprinting.intent.action.PRINT";
}
Intent intent = new Intent(intentId);
if (appId != null)
intent.setPackage(appId);
return intent;
}
/**
* Stellt die Eigenschaften des Druckers ein.
*/
private void adjustSettingsForPrintController (Intent intent) {
String mimeType = "image/png";
String appId = intent.getPackage();
// Check for special cases that can receive HTML
if (appId.equals("com.rcreations.send2printer") || appId.equals("com.dynamixsoftware.printershare")) {
mimeType = "text/html";
}
intent.setType(mimeType);
}
/**
* Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll.
*/
private void loadContentIntoPrintController (String content, Intent intent) {
String mimeType = intent.getType();
if (mimeType.equals("text/html")) {
loadContentAsHtmlIntoPrintController(content, intent);
} else {
loadContentAsBitmapIntoPrintController(content, intent);
}
}
/**
* Lädt den zu druckenden Content als HTML in ein WebView, welcher vom Drucker ausgedruckt werden soll.
*/
private void loadContentAsHtmlIntoPrintController (String content, Intent intent) {
intent.putExtra(Intent.EXTRA_TEXT, content);
}
/**
* Lädt den zu druckenden Content als BMP in ein WebView, welcher vom Drucker ausgedruckt werden soll.
*/
private void loadContentAsBitmapIntoPrintController (String content, final Intent intent) {
Activity ctx = cordova.getActivity();
final WebView page = new WebView(ctx);
final Printer self = this;
page.setVisibility(View.INVISIBLE);
page.getSettings().setJavaScriptEnabled(false);
page.getSettings().setDatabaseEnabled(true);
page.setWebViewClient( new WebViewClient() {
@Override @Override
public void onPageFinished(final WebView page, String url) { public void run() {
new Handler().postDelayed( new Runnable() { initWebView(content, props);
@Override loadContent(content);
public void run() {
Bitmap screenshot = self.takeScreenshot(page);
File tmpFile = self.saveScreenshotToTmpFile(screenshot);
ViewGroup vg = (ViewGroup)(page.getParent());
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tmpFile));
vg.removeView(page);
}
}, 1000);
} }
}); });
//Set base URI to the assets/www folder
String baseURL = webView.getUrl();
baseURL = baseURL.substring(0, baseURL.lastIndexOf('/') + 1);
ctx.addContentView(page, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
page.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null);
} }
/** /**
* Nimmt einen Screenshot der Seite auf. * Loads the content into the web view.
*
* @param content
* Either an HTML string or URI
*/ */
private Bitmap takeScreenshot (WebView page) { private void loadContent(String content) {
Picture picture = page.capturePicture(); if (content.startsWith("http") || content.startsWith("file:")) {
Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888); view.loadUrl(content);
Canvas canvas = new Canvas(bitmap); } else {
//Set base URI to the assets/www folder
String baseURL = webView.getUrl();
baseURL = baseURL.substring(0, baseURL.lastIndexOf('/') + 1);
picture.draw(canvas); view.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null);
return bitmap;
}
/**
* Speichert den Screenshot der Seite in einer tmp. Datei ab.
*/
private File saveScreenshotToTmpFile (Bitmap screenshot) {
try {
File tmpFile = File.createTempFile("screenshot", ".tmp");
FileOutputStream stream = new FileOutputStream(tmpFile);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
return tmpFile;
} catch (IOException e) {
e.printStackTrace();
} }
return null;
} }
/** /**
* Öffnet die Printer App, damit der Content ausgedruckt werden kann. * Configures the WebView components which will call the Google Cloud Print
* Service.
*
* @param content
* HTML encoded string
* @param props
* The JSON object with the containing page properties
*/ */
private void startPrinterApp (Intent intent) { private void initWebView (String content, JSONObject props) {
cordova.startActivityForResult(this, intent, 0); Activity ctx = cordova.getActivity();
view = new WebView(ctx);
view.getSettings().setDatabaseEnabled(true);
setWebViewClient(content, props);
} }
/** /**
* Findet heraus, ob die Anwendung installiert ist. * Creates the web view client which sets the print document.
*
* @param content
* HTML encoded string
* @param props
* The JSON object with the containing page properties
*/ */
private boolean isAppInstalled (String appId) { private void setWebViewClient (final String content, JSONObject props) {
PackageManager pm = cordova.getActivity().getPackageManager(); final String docName = props.optString("name", DEFAULT_DOC_NAME);
final boolean landscape = props.optBoolean("landscape", false);
final boolean graystyle = props.optBoolean("graystyle", false);
try { view.setWebViewClient(new WebViewClient() {
PackageInfo pi = pm.getPackageInfo(appId, 0); @Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (pi != null){ return false;
return true;
} }
} catch (PackageManager.NameNotFoundException e) {}
return false; @Override
public void onPageFinished (WebView webView, String url) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) cordova.getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Get a print builder instance
PrintAttributes.Builder builder = new PrintAttributes.Builder();
// The page does itself set its own margins
builder.setMinMargins(PrintAttributes.Margins.NO_MARGINS);
builder.setColorMode(graystyle ? PrintAttributes.COLOR_MODE_MONOCHROME
: PrintAttributes.COLOR_MODE_COLOR);
builder.setMediaSize(landscape ? PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE
: PrintAttributes.MediaSize.UNKNOWN_PORTRAIT);
// Create a print job with name and adapter instance
PrintJob job = printManager.print(docName, printAdapter, builder.build());
invokeCallbackOnceCompletedOrCanceled(job);
view = null;
}
});
} }
/** /**
* Die IDs aller verfügbaren Drucker-Apps. * Invokes the callback once the print job is complete or was canceled.
*
* @param job
* The reference to the print job
*/ */
private JSONArray getInstalledAppIds () { private void invokeCallbackOnceCompletedOrCanceled (final PrintJob job) {
JSONArray appIds = new JSONArray(); cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
Looper.prepare();
for (int i = 0; i < printAppIds.length; i++) { for (;;) {
String appId = printAppIds[i]; if (job.isCancelled() || job.isCompleted() || job.isFailed()) {
Boolean isInstalled = this.isAppInstalled(appId); command.success();
break;
if (isInstalled){ }
appIds.put(appId); }
} }
} });
return appIds;
}
/**
* Die erste ID in der Liste, deren App installiert ist.
*/
private String getFirstInstalledAppId () {
for (int i = 0; i < printAppIds.length; i++) {
String appId = printAppIds[i];
Boolean isInstalled = this.isAppInstalled(appId);
if (isInstalled){
return appId;
}
}
return null;
} }
} }

View File

@ -28,6 +28,6 @@
// Prints the content // Prints the content
- (void) print:(CDVInvokedUrlCommand*)command; - (void) print:(CDVInvokedUrlCommand*)command;
// Find out whether printing is supported on this platform // Find out whether printing is supported on this platform
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command; - (void) isAvailable:(CDVInvokedUrlCommand*)command;
@end @end

View File

@ -21,21 +21,13 @@
#import "APPPrinter.h" #import "APPPrinter.h"
@interface APPPrinter (Private) @interface APPPrinter ()
// Retrieves an instance of shared print controller @property (retain) NSString* callbackId;
- (UIPrintInteractionController*) printController;
// Adjusts the settings for the print controller
- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller;
// Loads the content into the print controller
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller;
// Opens the print controller so that the user can choose between available iPrinters
- (void) informAboutResult:(int)code callbackId:(NSString*)callbackId;
// Checks either the printing service is avaible or not
- (BOOL) isPrintingAvailable;
@end @end
@implementation APPPrinter @implementation APPPrinter
/* /*
@ -44,16 +36,18 @@
* @param {Function} callback * @param {Function} callback
* A callback function to be called with the result * A callback function to be called with the result
*/ */
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command - (void) isAvailable:(CDVInvokedUrlCommand*)command
{ {
CDVPluginResult* pluginResult; [self.commandDelegate runInBackground:^{
BOOL isAvailable = [self isPrintingAvailable]; CDVPluginResult* pluginResult;
BOOL isAvailable = [self isPrintingAvailable];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:isAvailable]; messageAsBool:isAvailable];
[self.commandDelegate sendPluginResult:pluginResult [self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId]; callbackId:command.callbackId];
}];
} }
/** /**
@ -68,17 +62,17 @@
return; return;
} }
NSArray* arguments = [command arguments]; _callbackId = command.callbackId;
NSString* content = [arguments objectAtIndex:0];
NSArray* arguments = [command arguments];
NSString* content = [arguments objectAtIndex:0];
NSMutableDictionary* settings = [arguments objectAtIndex:1];
UIPrintInteractionController* controller = [self printController]; UIPrintInteractionController* controller = [self printController];
[self adjustSettingsForPrintController:controller]; [self adjustPrintController:controller withSettings:settings];
[self loadContent:content intoPrintController:controller]; [self loadContent:content intoPrintController:controller];
[self presentPrintController:controller];
[self openPrintController:controller];
[self commandDelegate];
} }
/** /**
@ -100,19 +94,54 @@
* @return {UIPrintInteractionController} controller * @return {UIPrintInteractionController} controller
* The modified print controller instance * The modified print controller instance
*/ */
- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller - (UIPrintInteractionController*) adjustPrintController:(UIPrintInteractionController*)controller
withSettings:(NSMutableDictionary*)settings
{ {
UIPrintInfo* printInfo = [UIPrintInfo printInfo]; UIPrintInfo* printInfo = [UIPrintInfo printInfo];
UIPrintInfoOrientation orientation = UIPrintInfoOrientationPortrait;
UIPrintInfoOutputType outputType = UIPrintInfoOutputGeneral;
printInfo.outputType = UIPrintInfoOutputGeneral; if ([[settings objectForKey:@"landscape"] boolValue]) {
printInfo.orientation = UIPrintInfoOrientationPortrait; orientation = UIPrintInfoOrientationLandscape;
}
if ([[settings objectForKey:@"graystyle"] boolValue]) {
outputType = UIPrintInfoOutputGrayscale;
}
printInfo.outputType = outputType;
printInfo.orientation = orientation;
printInfo.jobName = [settings objectForKey:@"name"];
printInfo.duplex = [[settings objectForKey:@"duplex"] boolValue];
printInfo.printerID = [settings objectForKey:@"printerId"];
controller.printInfo = printInfo; controller.printInfo = printInfo;
controller.showsPageRange = YES; controller.showsPageRange = NO;
return controller; return controller;
} }
/**
* Adjusts the web view and page renderer.
*/
- (void) adjustWebView:(UIWebView*)page
andPrintPageRenderer:(UIPrintPageRenderer*)renderer
{
UIViewPrintFormatter* formatter = [page viewPrintFormatter];
// margin not required - done in web page
formatter.contentInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
renderer.headerHeight = -30.0f;
renderer.footerHeight = -30.0f;
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
page.scalesPageToFit = YES;
page.dataDetectorTypes = UIDataDetectorTypeNone;
page.userInteractionEnabled = NO;
page.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
}
/** /**
* Loads the content into the print controller. * Loads the content into the print controller.
* *
@ -123,24 +152,25 @@
*/ */
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller - (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
{ {
// Set the base URL to be the www directory. UIWebView* page = [[UIWebView alloc] init];
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
// Load page into a webview and use its formatter to print the page
UIWebView* webPage = [[UIWebView alloc] init];
[webPage loadHTMLString:content baseURL:baseURL];
// Get formatter for web (note: margin not required - done in web page)
UIViewPrintFormatter* formatter = [webPage viewPrintFormatter];
formatter.contentInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init]; UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
renderer.headerHeight = -30.0f;
renderer.footerHeight = -30.0f; [self adjustWebView:page andPrintPageRenderer:renderer];
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]];
}
else {
// Set the base URL to be the www directory.
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
[page loadHTMLString:content baseURL:baseURL];
}
controller.printPageRenderer = renderer; controller.printPageRenderer = renderer;
} }
@ -152,9 +182,16 @@
* @param {UIPrintInteractionController} controller * @param {UIPrintInteractionController} controller
* The prepared print controller with a content * The prepared print controller with a content
*/ */
- (void) openPrintController:(UIPrintInteractionController*)controller - (void) presentPrintController:(UIPrintInteractionController*)controller
{ {
[controller presentAnimated:YES completionHandler:NULL]; [controller presentAnimated:YES completionHandler:
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
CDVPluginResult* pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}];
} }
/** /**
@ -174,4 +211,4 @@
isPrintingAvailable]; isPrintingAvailable];
} }
@end @end

View File

@ -1,50 +0,0 @@
/*
Copyright 2013-2014 appPlant UG
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
PrinterProxy = {
isServiceAvailable: function (successCallback, failCallback, args) {
args[0](true);
},
print: function (successCallback, failCallback, args) {
window.printContent = args[0];
Windows.Graphics.Printing.PrintManager.showPrintUIAsync();
},
printTaskRequested: function (printEvent) {
printEvent.request.createPrintTask("Print", function (args) {
var documentFragment = document.createDocumentFragment();
var content = document.createElement("html");
content.innerHTML = window.printContent;
documentFragment.appendChild(content);
args.setSource(MSApp.getHtmlPrintDocumentSource(documentFragment));
});
}
};
var printManager = Windows.Graphics.Printing.PrintManager.getForCurrentView();
printManager.onprinttaskrequested = PrinterProxy.printTaskRequested;
require("cordova/windows8/commandProxy").add("Printer", PrinterProxy);

View File

@ -19,54 +19,80 @@
under the License. under the License.
*/ */
var Printer = function () { var exec = require('cordova/exec');
/**
* The default document/job name.
*/
exports.DEFAULT_DOC_NAME = 'unknown';
/**
* 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}
*/
exports.isAvailable = function (callback, scope) {
var fn = this._createCallbackFn(callback);
exec(fn, null, 'Printer', 'isAvailable', []);
}; };
Printer.prototype = { /**
/** * Sends the content to the Google Cloud Print service.
* Checks if the printer service is avaible (iOS) *
* or if a printing app is installed on the device (Android). * @param {String} content
* * HTML string or DOM node
* @param {Function} callback * if latter, innerHTML is used to get the content
* A callback function * @param {Object} options
* @param {Object?} scope * Options for the print job
* The scope of the callback (default: window) * @param {Function?} callback
* * A callback function
* @return {Boolean} * @param {Object?} scope
*/ * The scope of the callback (default: window)
isServiceAvailable: function (callback, scope) { */
var callbackFn = function () { exports.print = function (content, options, callback, scope) {
var args = typeof arguments[0] == 'boolean' ? arguments : arguments[0]; var page = content.innerHTML || content,
params = options || {},
fn = this._createCallbackFn(callback);
callback.apply(scope || window, args); if (typeof page != 'string') {
}; console.log('Print function requires an HTML string. Not an object');
return;
cordova.exec(callbackFn, null, 'Printer', 'isServiceAvailable', []);
},
/**
* Sends the content to a printer app or service.
*
* @param {String} content
* HTML string or DOM node
* if latter, innerHTML is used to get the content
* @param {Object?} options
* Platform specific options
*/
print: function (content, options) {
var page = content.innerHTML || content,
options = options || {};
if (typeof page != 'string') {
console.log('Print function requires an HTML string. Not an object');
return;
}
cordova.exec(null, null, 'Printer', 'print', [page, options]);
} }
if (typeof params == 'string')
params = { name: params };
if ([null, undefined, ''].indexOf(params.name) > -1)
params.name = this.DEFAULT_DOC_NAME;
exec(fn, null, 'Printer', 'print', [page, params]);
}; };
var plugin = new Printer(); /**
* @private
*
* Creates a callback, which will be executed within a specific scope.
*
* @param {Function} callbackFn
* The callback function
* @param {Object} scope
* The scope for the function
*
* @return {Function}
* The new callback function
*/
exports._createCallbackFn = function (callbackFn, scope) {
if (typeof callbackFn != 'function')
return;
module.exports = plugin; return function () {
callbackFn.apply(scope || this, arguments);
};
};