Enable printing current webview (#196)

Sometimes when doing complex rendering you want to print the current
webview rather than another page or link. This enables you to call
.print('') and it will print the current webview rather than a blank
page on both ios and android.
This commit is contained in:
Mark Zealey 2018-12-21 14:13:01 +03:00 committed by Sebastián Katzer
parent 675b3f5962
commit 6e44eb9cde
2 changed files with 77 additions and 50 deletions

View File

@ -35,6 +35,8 @@ import android.webkit.WebViewClient;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -60,6 +62,7 @@ import static de.appplant.cordova.plugin.printer.ui.SelectPrinterActivity.EXTRA_
* the print adapter of that web view and initializes a print job. * the print adapter of that web view and initializes a print job.
*/ */
public class Printer extends CordovaPlugin { public class Printer extends CordovaPlugin {
CordovaWebView _webView;
/** /**
* The web view that loads all the content. * The web view that loads all the content.
@ -165,14 +168,18 @@ public class Printer extends CordovaPlugin {
* The exec arguments as JSON * The exec arguments as JSON
*/ */
private void print (final JSONArray args) { private void print (final JSONArray args) {
final String content = args.optString(0, "<html></html>"); final String content = args.optString(0);
final JSONObject props = args.optJSONObject(1); final JSONObject props = args.optJSONObject(1);
cordova.getActivity().runOnUiThread( new Runnable() { cordova.getActivity().runOnUiThread( new Runnable() {
@Override @Override
public void run() { public void run() {
initWebView(props); if( content.isEmpty() ) {
loadContent(content); do_print((WebView)_webView.getView(), props);
} else {
initWebView(props);
loadContent(content);
}
} }
}); });
} }
@ -239,6 +246,37 @@ public class Printer extends CordovaPlugin {
setWebViewClient(props); setWebViewClient(props);
} }
private void do_print(WebView webView, 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);
final String duplex = props.optString("duplex", "none");
PrintAttributes.Builder builder = new PrintAttributes.Builder();
PrintDocumentAdapter adapter = getAdapter(webView, docName);
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);
if (!duplex.equals("none") && Build.VERSION.SDK_INT >= 23) {
boolean longEdge = duplex.equals("long");
Method setDuplexModeMethod = Meta.getMethod(
builder.getClass(), "setDuplexMode", int.class);
Meta.invokeMethod(builder, setDuplexModeMethod,
longEdge ? 2 : 4);
}
pm.getInstance().print(docName, adapter, builder.build());
}
/** /**
* Creates the web view client which sets the print document. * Creates the web view client which sets the print document.
* *
@ -246,11 +284,6 @@ public class Printer extends CordovaPlugin {
* The JSON object with the containing page properties * The JSON object with the containing page properties
*/ */
private void setWebViewClient (JSONObject props) { private void setWebViewClient (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);
final String duplex = props.optString("duplex", "none");
view.setWebViewClient(new WebViewClient() { view.setWebViewClient(new WebViewClient() {
@Override @Override
public boolean shouldOverrideUrlLoading (WebView view, String url) { public boolean shouldOverrideUrlLoading (WebView view, String url) {
@ -259,29 +292,7 @@ public class Printer extends CordovaPlugin {
@Override @Override
public void onPageFinished (WebView webView, String url) { public void onPageFinished (WebView webView, String url) {
PrintAttributes.Builder builder = new PrintAttributes.Builder(); do_print(webView, props);
PrintDocumentAdapter adapter = getAdapter(webView, docName);
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);
if (!duplex.equals("none") && Build.VERSION.SDK_INT >= 23) {
boolean longEdge = duplex.equals("long");
Method setDuplexModeMethod = Meta.getMethod(
builder.getClass(), "setDuplexMode", int.class);
Meta.invokeMethod(builder, setDuplexModeMethod,
longEdge ? 2 : 4);
}
pm.getInstance().print(docName, adapter, builder.build());
view = null; view = null;
} }
}); });
@ -342,6 +353,12 @@ public class Printer extends CordovaPlugin {
pm.setOnPrintJobStateChangeListener(listener); pm.setOnPrintJobStateChangeListener(listener);
} }
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
_webView = webView;
}
/** /**
* The final call you receive before your activity is destroyed. * The final call you receive before your activity is destroyed.
*/ */

View File

@ -382,29 +382,39 @@
*/ */
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller - (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
{ {
UIWebView* page = [[UIWebView alloc] init];
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init]; UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
UIViewPrintFormatter* formatter = [page viewPrintFormatter]; UIViewPrintFormatter* formatter;
if([content length] == 0) {
formatter = [self.webView viewPrintFormatter];
} else {
UIWebView* page = [[UIWebView alloc] init];
formatter = [page viewPrintFormatter];
page.delegate = self;
if([content length] == 0) {
// do nothing, already loaded
} else if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]];
} else {
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
[page loadHTMLString:content baseURL:baseURL];
}
}
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0]; [renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
page.delegate = self;
if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]];
}
else {
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
[page loadHTMLString:content baseURL:baseURL];
}
controller.printPageRenderer = renderer; controller.printPageRenderer = renderer;
// just trigger the finish load fn straight off if using current webView
if([content length] == 0)
[self webViewDidFinishLoad:(UIWebView *)self.webView];
} }
/** /**