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,15 +168,19 @@ 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() {
if( content.isEmpty() ) {
do_print((WebView)_webView.getView(), props);
} else {
initWebView(props); initWebView(props);
loadContent(content); loadContent(content);
} }
}
}); });
} }
@ -239,26 +246,12 @@ public class Printer extends CordovaPlugin {
setWebViewClient(props); setWebViewClient(props);
} }
/** private void do_print(WebView webView, JSONObject props) {
* Creates the web view client which sets the print document.
*
* @param props
* The JSON object with the containing page properties
*/
private void setWebViewClient (JSONObject props) {
final String docName = props.optString("name", DEFAULT_DOC_NAME); final String docName = props.optString("name", DEFAULT_DOC_NAME);
final boolean landscape = props.optBoolean("landscape", false); final boolean landscape = props.optBoolean("landscape", false);
final boolean graystyle = props.optBoolean("graystyle", false); final boolean graystyle = props.optBoolean("graystyle", false);
final String duplex = props.optString("duplex", "none"); final String duplex = props.optString("duplex", "none");
view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
return false;
}
@Override
public void onPageFinished (WebView webView, String url) {
PrintAttributes.Builder builder = new PrintAttributes.Builder(); PrintAttributes.Builder builder = new PrintAttributes.Builder();
PrintDocumentAdapter adapter = getAdapter(webView, docName); PrintDocumentAdapter adapter = getAdapter(webView, docName);
@ -282,6 +275,24 @@ public class Printer extends CordovaPlugin {
} }
pm.getInstance().print(docName, adapter, builder.build()); pm.getInstance().print(docName, adapter, builder.build());
}
/**
* Creates the web view client which sets the print document.
*
* @param props
* The JSON object with the containing page properties
*/
private void setWebViewClient (JSONObject props) {
view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
return false;
}
@Override
public void onPageFinished (WebView webView, String url) {
do_print(webView, props);
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,20 +382,24 @@
*/ */
- (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;
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0]; if([content length] == 0) {
formatter = [self.webView viewPrintFormatter];
} else {
UIWebView* page = [[UIWebView alloc] init];
formatter = [page viewPrintFormatter];
page.delegate = self; page.delegate = self;
if ([NSURL URLWithString:content]) { if([content length] == 0) {
// do nothing, already loaded
} else if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content]; NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]]; [page loadRequest:[NSURLRequest requestWithURL:url]];
} } else {
else {
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www" NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil]; ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath]; NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
@ -403,8 +407,14 @@
[page loadHTMLString:content baseURL:baseURL]; [page loadHTMLString:content baseURL:baseURL];
} }
}
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
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];
} }
/** /**