From 6e44eb9cdef928d1ec6754fc875805f8652df3ca Mon Sep 17 00:00:00 2001 From: Mark Zealey <6083471+mzealey@users.noreply.github.com> Date: Fri, 21 Dec 2018 14:13:01 +0300 Subject: [PATCH] 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. --- src/android/Printer.java | 79 ++++++++++++++++++++++++---------------- src/ios/APPPrinter.m | 48 ++++++++++++++---------- 2 files changed, 77 insertions(+), 50 deletions(-) diff --git a/src/android/Printer.java b/src/android/Printer.java index 0f5f467..54b115b 100644 --- a/src/android/Printer.java +++ b/src/android/Printer.java @@ -35,6 +35,8 @@ import android.webkit.WebViewClient; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaWebView; +import org.apache.cordova.CordovaInterface; import org.apache.cordova.PluginResult; import org.json.JSONArray; 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. */ public class Printer extends CordovaPlugin { + CordovaWebView _webView; /** * The web view that loads all the content. @@ -165,14 +168,18 @@ public class Printer extends CordovaPlugin { * The exec arguments as JSON */ private void print (final JSONArray args) { - final String content = args.optString(0, ""); + final String content = args.optString(0); final JSONObject props = args.optJSONObject(1); cordova.getActivity().runOnUiThread( new Runnable() { @Override public void run() { - initWebView(props); - loadContent(content); + if( content.isEmpty() ) { + do_print((WebView)_webView.getView(), props); + } else { + initWebView(props); + loadContent(content); + } } }); } @@ -239,6 +246,37 @@ public class Printer extends CordovaPlugin { 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. * @@ -246,11 +284,6 @@ public class Printer extends CordovaPlugin { * The JSON object with the containing page properties */ 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() { @Override public boolean shouldOverrideUrlLoading (WebView view, String url) { @@ -259,29 +292,7 @@ public class Printer extends CordovaPlugin { @Override public void onPageFinished (WebView webView, String url) { - 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()); + do_print(webView, props); view = null; } }); @@ -342,6 +353,12 @@ public class Printer extends CordovaPlugin { 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. */ diff --git a/src/ios/APPPrinter.m b/src/ios/APPPrinter.m index fd0b340..10a273d 100755 --- a/src/ios/APPPrinter.m +++ b/src/ios/APPPrinter.m @@ -382,29 +382,39 @@ */ - (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller { - UIWebView* page = [[UIWebView 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]; - - 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; + + // just trigger the finish load fn straight off if using current webView + if([content length] == 0) + [self webViewDidFinishLoad:(UIWebView *)self.webView]; } /**