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:
commit
b28d85ca9f
41
CHANGELOG.md
41
CHANGELOG.md
@ -1,33 +1,8 @@
|
||||
##ChangeLog
|
||||
#### Version 0.6.0 (not yet released)
|
||||
- [feature]: Added Windows8 support<br>
|
||||
*Thanks to* ***pirvudoru***
|
||||
|
||||
#### Version 0.5.3 (13.07.2014)
|
||||
- [bugfix]: Printing wasn't possible because `isServiceAvailable` returns False IOS
|
||||
- [bugfix]: Using plugin prevents openDatabase() on Android
|
||||
|
||||
#### 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***
|
||||
## ChangeLog
|
||||
#### Version 0.7.0 (not yet released)
|
||||
- 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.
|
202
README.md
202
README.md
@ -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> </span>
|
||||
<a href="https://github.com/katzer/cordova-plugin-printer/tree/google-cloud-print">v0.6</a>
|
||||
<span> </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: __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)
|
||||
|
||||
### 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
|
||||
- **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
|
||||
The plugin can either be installed from git repository or from local file system through the [Command-line Interface][CLI].<br>
|
||||
Or cloud based through [PhoneGap Build][PGB].
|
||||
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].
|
||||
|
||||
### Local development environment
|
||||
From master:
|
||||
```bash
|
||||
# ~~ 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:
|
||||
```bash
|
||||
# ~~ 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:
|
||||
```bash
|
||||
# ~~ stable version ~~
|
||||
cordova plugin add de.appplant.cordova.plugin.printer && cordova prepare
|
||||
cordova plugin add de.appplant.cordova.plugin.printer
|
||||
```
|
||||
|
||||
### 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:
|
||||
```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].
|
||||
|
||||
@ -52,44 +93,44 @@ cordova plugin rm de.appplant.cordova.plugin.printer
|
||||
|
||||
|
||||
## ChangeLog
|
||||
#### Version 0.6.0 (not yet released)
|
||||
- [feature]: Added Windows8 support<br>
|
||||
*Thanks to* ***pirvudoru***
|
||||
|
||||
#### Version 0.5.3 (13.07.2014)
|
||||
- [bugfix]: Printing wasn't possible because `isServiceAvailable` returns False IOS
|
||||
- [bugfix]: Using plugin prevents openDatabase() on Android
|
||||
#### Version 0.7.0 (not yet released)
|
||||
- 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 `window.plugin.printer` with the following methods:
|
||||
The plugin creates the object `cordova.plugins.printer` with the following methods:
|
||||
|
||||
1. [plugin.printer.isServiceAvailable][available]
|
||||
2. [plugin.printer.print][print]
|
||||
1. [printer.isAvailable][available]
|
||||
2. [printer.print][print]
|
||||
|
||||
### Plugin initialization
|
||||
The plugin and its methods are not available before the *deviceready* event has been fired.
|
||||
|
||||
```javascript
|
||||
document.addEventListener('deviceready', function () {
|
||||
// window.plugin.printer is now available
|
||||
// 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.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>
|
||||
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
|
||||
/**
|
||||
* 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
|
||||
* A callback function
|
||||
@ -98,7 +139,7 @@ __Note:__ Printing is only available on devices capable of multi-tasking (iPhone
|
||||
*
|
||||
* @return {Boolean}
|
||||
*/
|
||||
window.plugin.printer.isServiceAvailable(
|
||||
cordova.plugins.printer.isAvailable(
|
||||
function (isAvailable) {
|
||||
alert(isAvailable ? 'Service is available' : 'Service NOT available');
|
||||
}
|
||||
@ -107,61 +148,93 @@ window.plugin.printer.isServiceAvailable(
|
||||
|
||||
### Send content to a printer
|
||||
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
|
||||
- See the [isServiceAvailable][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.
|
||||
- On Android the functionality between different print apps may vary.
|
||||
- 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 a printer app or service.
|
||||
* 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
|
||||
* Platform specific options
|
||||
* @param {Object} 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
|
||||
The following exmaple demonstrates how to print out the whole HTML page.
|
||||
## 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
|
||||
// Get the content
|
||||
var page = document.body;
|
||||
// URI for the index.html
|
||||
var page = location.href;
|
||||
|
||||
// Pass to the printer
|
||||
window.plugin.printer.print(page);
|
||||
cordova.plugins.printer.print(page, 'Document.html', function () {
|
||||
alert('printing finished or canceled')
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
## 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.
|
||||
#### 2. Print the content from a part of the page
|
||||
```javascript
|
||||
window.plugin.printer.isServiceAvailable(
|
||||
function (isAvailable, installedAppIds) {
|
||||
alert('The following print apps are installed on your device: ' + installedAppIds.join(', '));
|
||||
}
|
||||
);
|
||||
// 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')
|
||||
});
|
||||
```
|
||||
|
||||
### Specify printing app on Android
|
||||
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.
|
||||
#### 3. Print custom specific content
|
||||
```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
|
||||
|
||||
### 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
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
1. Fork it
|
||||
@ -202,12 +270,20 @@ This software is released under the [Apache 2.0 License][apache2_license].
|
||||
|
||||
|
||||
[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/3.3.0/index.html
|
||||
[PGB_plugin]: https://build.phonegap.com/plugins/676
|
||||
[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
|
||||
|
30
plugin.xml
30
plugin.xml
@ -3,17 +3,28 @@
|
||||
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
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>
|
||||
|
||||
<author>Sebastián Katzer</author>
|
||||
|
||||
<!-- cordova -->
|
||||
<engines>
|
||||
<engine name="cordova" version=">=3.0.0" />
|
||||
</engines>
|
||||
@ -21,6 +32,7 @@
|
||||
<!-- interface -->
|
||||
<js-module src="www/printer.js" name="Printer">
|
||||
<clobbers target="plugin.printer" />
|
||||
<clobbers target="cordova.plugins.printer" />
|
||||
</js-module>
|
||||
|
||||
<!-- ios -->
|
||||
@ -33,6 +45,7 @@
|
||||
|
||||
<header-file src="src/ios/APPPrinter.h" />
|
||||
<source-file src="src/ios/APPPrinter.m" />
|
||||
|
||||
</platform>
|
||||
|
||||
<!-- android -->
|
||||
@ -45,11 +58,4 @@
|
||||
|
||||
<source-file src="src/android/Printer.java" target-dir="src/de/appplant/cordova/plugin/printer" />
|
||||
</platform>
|
||||
|
||||
<!-- windows8
|
||||
<platform name="windows8">
|
||||
<js-module src="src/windows8/PrinterProxy.js" name="PrinterProxy">
|
||||
<clobbers target="" />
|
||||
</js-module>
|
||||
</platform>-->
|
||||
</plugin>
|
||||
|
@ -17,85 +17,67 @@
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
*/
|
||||
|
||||
package de.appplant.cordova.plugin.printer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Picture;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Looper;
|
||||
import android.print.PrintAttributes;
|
||||
import android.print.PrintDocumentAdapter;
|
||||
import android.print.PrintJob;
|
||||
import android.print.PrintManager;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.PluginResult;
|
||||
|
||||
@TargetApi(19)
|
||||
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
|
||||
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
// Etwas soll ausgedruckt werden
|
||||
if ("print".equals(action)) {
|
||||
print(args, callbackContext);
|
||||
public boolean execute (String action, JSONArray args,
|
||||
CallbackContext callbackContext) throws JSONException {
|
||||
|
||||
command = callbackContext;
|
||||
|
||||
if (action.equalsIgnoreCase("isAvailable")) {
|
||||
isAvailable();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Es soll überprüft werden, ob ein Dienst zum Ausdrucken von Inhalten zur Verfügung steht
|
||||
if ("isServiceAvailable".equals(action)) {
|
||||
isServiceAvailable(callbackContext);
|
||||
if (action.equalsIgnoreCase("print")) {
|
||||
print(args);
|
||||
|
||||
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) {
|
||||
JSONArray appIds = this.getInstalledAppIds();
|
||||
Boolean available = appIds.length() > 0;
|
||||
JSONArray args = new JSONArray();
|
||||
PluginResult result;
|
||||
private void isAvailable () {
|
||||
Boolean supported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
||||
PluginResult result = new PluginResult(PluginResult.Status.OK, supported);
|
||||
|
||||
args.put(available);
|
||||
args.put(appIds);
|
||||
|
||||
result = new PluginResult(PluginResult.Status.OK, args);
|
||||
|
||||
ctx.sendPluginResult(result);
|
||||
command.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) {
|
||||
final Printer self = this;
|
||||
|
||||
this.ctx = ctx;
|
||||
private void print (final JSONArray args) {
|
||||
final String content = args.optString(0, "<html></html>");
|
||||
final JSONObject props = args.optJSONObject(1);;
|
||||
|
||||
cordova.getActivity().runOnUiThread( new Runnable() {
|
||||
@Override
|
||||
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);
|
||||
initWebView(content, props);
|
||||
loadContent(content);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die zu verwendende App-ID an.
|
||||
* Loads the content into the web view.
|
||||
*
|
||||
* @param content
|
||||
* Either an HTML string or URI
|
||||
*/
|
||||
private String getPrintAppId (JSONObject platformConfig) {
|
||||
String appId = platformConfig.optString("appId", null);
|
||||
|
||||
if (appId != null) {
|
||||
return (this.isAppInstalled(appId)) ? appId : null;
|
||||
private void loadContent(String content) {
|
||||
if (content.startsWith("http") || content.startsWith("file:")) {
|
||||
view.loadUrl(content);
|
||||
} 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
|
||||
public void onPageFinished(final WebView page, String url) {
|
||||
new Handler().postDelayed( new Runnable() {
|
||||
@Override
|
||||
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);
|
||||
view.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Nimmt einen Screenshot der Seite auf.
|
||||
* 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 Bitmap takeScreenshot (WebView page) {
|
||||
Picture picture = page.capturePicture();
|
||||
Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
private void initWebView (String content, JSONObject props) {
|
||||
Activity ctx = cordova.getActivity();
|
||||
view = new WebView(ctx);
|
||||
|
||||
picture.draw(canvas);
|
||||
view.getSettings().setDatabaseEnabled(true);
|
||||
|
||||
return bitmap;
|
||||
setWebViewClient(content, props);
|
||||
}
|
||||
|
||||
/**
|
||||
* Speichert den Screenshot der Seite in einer tmp. Datei ab.
|
||||
* 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 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.
|
||||
*/
|
||||
private void startPrinterApp (Intent intent) {
|
||||
cordova.startActivityForResult(this, intent, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Findet heraus, ob die Anwendung installiert ist.
|
||||
*/
|
||||
private boolean isAppInstalled (String appId) {
|
||||
PackageManager pm = cordova.getActivity().getPackageManager();
|
||||
|
||||
try {
|
||||
PackageInfo pi = pm.getPackageInfo(appId, 0);
|
||||
|
||||
if (pi != null){
|
||||
return true;
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {}
|
||||
private void setWebViewClient (final String content, JSONObject props) {
|
||||
final String docName = props.optString("name", DEFAULT_DOC_NAME);
|
||||
final boolean landscape = props.optBoolean("landscape", false);
|
||||
final boolean graystyle = props.optBoolean("graystyle", false);
|
||||
|
||||
view.setWebViewClient(new WebViewClient() {
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading (WebView view, String url) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die IDs aller verfügbaren Drucker-Apps.
|
||||
*/
|
||||
private JSONArray getInstalledAppIds () {
|
||||
JSONArray appIds = new JSONArray();
|
||||
@Override
|
||||
public void onPageFinished (WebView webView, String url) {
|
||||
// Get a PrintManager instance
|
||||
PrintManager printManager = (PrintManager) cordova.getActivity()
|
||||
.getSystemService(Context.PRINT_SERVICE);
|
||||
|
||||
for (int i = 0; i < printAppIds.length; i++) {
|
||||
String appId = printAppIds[i];
|
||||
Boolean isInstalled = this.isAppInstalled(appId);
|
||||
// Get a print adapter instance
|
||||
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
|
||||
|
||||
if (isInstalled){
|
||||
appIds.put(appId);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
return appIds;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Die erste ID in der Liste, deren App installiert ist.
|
||||
* Invokes the callback once the print job is complete or was canceled.
|
||||
*
|
||||
* @param job
|
||||
* The reference to the print job
|
||||
*/
|
||||
private String getFirstInstalledAppId () {
|
||||
for (int i = 0; i < printAppIds.length; i++) {
|
||||
String appId = printAppIds[i];
|
||||
Boolean isInstalled = this.isAppInstalled(appId);
|
||||
private void invokeCallbackOnceCompletedOrCanceled (final PrintJob job) {
|
||||
cordova.getThreadPool().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Looper.prepare();
|
||||
|
||||
if (isInstalled){
|
||||
return appId;
|
||||
for (;;) {
|
||||
if (job.isCancelled() || job.isCompleted() || job.isFailed()) {
|
||||
command.success();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,6 @@
|
||||
// Prints the content
|
||||
- (void) print:(CDVInvokedUrlCommand*)command;
|
||||
// Find out whether printing is supported on this platform
|
||||
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command;
|
||||
- (void) isAvailable:(CDVInvokedUrlCommand*)command;
|
||||
|
||||
@end
|
||||
|
@ -21,21 +21,13 @@
|
||||
|
||||
#import "APPPrinter.h"
|
||||
|
||||
@interface APPPrinter (Private)
|
||||
@interface APPPrinter ()
|
||||
|
||||
// Retrieves an instance of shared print controller
|
||||
- (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;
|
||||
@property (retain) NSString* callbackId;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation APPPrinter
|
||||
|
||||
/*
|
||||
@ -44,8 +36,9 @@
|
||||
* @param {Function} callback
|
||||
* A callback function to be called with the result
|
||||
*/
|
||||
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command
|
||||
- (void) isAvailable:(CDVInvokedUrlCommand*)command
|
||||
{
|
||||
[self.commandDelegate runInBackground:^{
|
||||
CDVPluginResult* pluginResult;
|
||||
BOOL isAvailable = [self isPrintingAvailable];
|
||||
|
||||
@ -54,6 +47,7 @@
|
||||
|
||||
[self.commandDelegate sendPluginResult:pluginResult
|
||||
callbackId:command.callbackId];
|
||||
}];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,17 +62,17 @@
|
||||
return;
|
||||
}
|
||||
|
||||
_callbackId = command.callbackId;
|
||||
|
||||
NSArray* arguments = [command arguments];
|
||||
NSString* content = [arguments objectAtIndex:0];
|
||||
NSMutableDictionary* settings = [arguments objectAtIndex:1];
|
||||
|
||||
UIPrintInteractionController* controller = [self printController];
|
||||
|
||||
[self adjustSettingsForPrintController:controller];
|
||||
[self adjustPrintController:controller withSettings:settings];
|
||||
[self loadContent:content intoPrintController:controller];
|
||||
|
||||
[self openPrintController:controller];
|
||||
|
||||
[self commandDelegate];
|
||||
[self presentPrintController:controller];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,19 +94,54 @@
|
||||
* @return {UIPrintInteractionController} controller
|
||||
* The modified print controller instance
|
||||
*/
|
||||
- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller
|
||||
- (UIPrintInteractionController*) adjustPrintController:(UIPrintInteractionController*)controller
|
||||
withSettings:(NSMutableDictionary*)settings
|
||||
{
|
||||
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
|
||||
UIPrintInfoOrientation orientation = UIPrintInfoOrientationPortrait;
|
||||
UIPrintInfoOutputType outputType = UIPrintInfoOutputGeneral;
|
||||
|
||||
printInfo.outputType = UIPrintInfoOutputGeneral;
|
||||
printInfo.orientation = UIPrintInfoOrientationPortrait;
|
||||
if ([[settings objectForKey:@"landscape"] boolValue]) {
|
||||
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.showsPageRange = YES;
|
||||
controller.showsPageRange = NO;
|
||||
|
||||
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.
|
||||
*
|
||||
@ -123,24 +152,25 @@
|
||||
*/
|
||||
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
|
||||
{
|
||||
UIWebView* page = [[UIWebView alloc] init];
|
||||
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
|
||||
|
||||
[self adjustWebView:page andPrintPageRenderer:renderer];
|
||||
|
||||
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];
|
||||
// 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];
|
||||
renderer.headerHeight = -30.0f;
|
||||
renderer.footerHeight = -30.0f;
|
||||
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
|
||||
[page loadHTMLString:content baseURL:baseURL];
|
||||
}
|
||||
|
||||
controller.printPageRenderer = renderer;
|
||||
}
|
||||
@ -152,9 +182,16 @@
|
||||
* @param {UIPrintInteractionController} controller
|
||||
* 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];
|
||||
}];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
@ -19,14 +19,16 @@
|
||||
under the License.
|
||||
*/
|
||||
|
||||
var Printer = function () {
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
};
|
||||
/**
|
||||
* The default document/job name.
|
||||
*/
|
||||
exports.DEFAULT_DOC_NAME = 'unknown';
|
||||
|
||||
Printer.prototype = {
|
||||
/**
|
||||
/**
|
||||
* 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
|
||||
* A callback function
|
||||
@ -35,38 +37,62 @@ Printer.prototype = {
|
||||
*
|
||||
* @return {Boolean}
|
||||
*/
|
||||
isServiceAvailable: function (callback, scope) {
|
||||
var callbackFn = function () {
|
||||
var args = typeof arguments[0] == 'boolean' ? arguments : arguments[0];
|
||||
exports.isAvailable = function (callback, scope) {
|
||||
var fn = this._createCallbackFn(callback);
|
||||
|
||||
callback.apply(scope || window, args);
|
||||
};
|
||||
exec(fn, null, 'Printer', 'isAvailable', []);
|
||||
};
|
||||
|
||||
cordova.exec(callbackFn, null, 'Printer', 'isServiceAvailable', []);
|
||||
},
|
||||
|
||||
/**
|
||||
* Sends the content to a printer app or service.
|
||||
/**
|
||||
* 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
|
||||
* Platform specific options
|
||||
* @param {Object} options
|
||||
* Options for the print job
|
||||
* @param {Function?} callback
|
||||
* A callback function
|
||||
* @param {Object?} scope
|
||||
* The scope of the callback (default: window)
|
||||
*/
|
||||
print: function (content, options) {
|
||||
exports.print = function (content, options, callback, scope) {
|
||||
var page = content.innerHTML || content,
|
||||
options = options || {};
|
||||
params = options || {},
|
||||
fn = this._createCallbackFn(callback);
|
||||
|
||||
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);
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user