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:
parent
675b3f5962
commit
6e44eb9cde
@ -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,15 +168,19 @@ public class Printer extends CordovaPlugin {
|
||||
* The exec arguments as JSON
|
||||
*/
|
||||
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);
|
||||
|
||||
cordova.getActivity().runOnUiThread( new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if( content.isEmpty() ) {
|
||||
do_print((WebView)_webView.getView(), props);
|
||||
} else {
|
||||
initWebView(props);
|
||||
loadContent(content);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -239,26 +246,12 @@ public class Printer extends CordovaPlugin {
|
||||
setWebViewClient(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) {
|
||||
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");
|
||||
|
||||
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();
|
||||
PrintDocumentAdapter adapter = getAdapter(webView, docName);
|
||||
|
||||
@ -282,6 +275,24 @@ public class Printer extends CordovaPlugin {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
@ -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.
|
||||
*/
|
||||
|
@ -382,20 +382,24 @@
|
||||
*/
|
||||
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
|
||||
{
|
||||
UIWebView* page = [[UIWebView 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;
|
||||
|
||||
if ([NSURL URLWithString:content]) {
|
||||
if([content length] == 0) {
|
||||
// do nothing, already loaded
|
||||
} else if ([NSURL URLWithString:content]) {
|
||||
NSURL *url = [NSURL URLWithString:content];
|
||||
|
||||
[page loadRequest:[NSURLRequest requestWithURL:url]];
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
|
||||
ofType:nil];
|
||||
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
|
||||
@ -403,8 +407,14 @@
|
||||
|
||||
[page loadHTMLString:content baseURL:baseURL];
|
||||
}
|
||||
}
|
||||
|
||||
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
|
||||
controller.printPageRenderer = renderer;
|
||||
|
||||
// just trigger the finish load fn straight off if using current webView
|
||||
if([content length] == 0)
|
||||
[self webViewDidFinishLoad:(UIWebView *)self.webView];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user