Skip to content

Instantly share code, notes, and snippets.

@pluswave
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pluswave/aa0548c57d3067d456ad to your computer and use it in GitHub Desktop.
Save pluswave/aa0548c57d3067d456ad to your computer and use it in GitHub Desktop.
KItKatPrinter plugin for cordova
package org.xiaofuxing.name;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.Runnable;
import android.print.PrintJob;
import android.print.PrintManager;
import android.print.PrintDocumentAdapter;
import android.print.PrintAttributes;
import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
/**
* This class echoes a string called from JavaScript.
*/
public class KitKatPrinter extends CordovaPlugin implements Runnable {
private WebView offlineWebView;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("printCurrentPage")) {
String message = args.getString(0);
this.printCurrentPage(message, callbackContext);
return true;
}
return false;
}
private void printCurrentPage(String message, CallbackContext callbackContext) {
this.cordova.getActivity().
runOnUiThread(this);
//run();
}
public void run(){
// Get a PrintManager instance
PrintManager printManager = (PrintManager) this.cordova.getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
if( this.offlineWebView == null ){
this.offlineWebView = new WebView(this.cordova.getActivity());
WebSettings settings = this.offlineWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowUniversalAccessFromFileURLs(true);
this.offlineWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
//Log.i(TAG, "page finished loading " + url);
createWebPrintJob(view);
//mWebView = null;
}
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) KitKatPrinter.this.cordova.getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
webView.onPause();
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
String jobName = " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
// Save the job object for later status checking
// mPrintJobs.add(printJob);
}
});
}
//this.offlineWebView.loadUrl(this.webView.getUrl()); //doesn't work on Ionic starter app!
String htmlDocument = "<html><body><h1>Test Content中文标题</h1><p>Testing, " +
"testing, testing...</p></body></html>";
this.offlineWebView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);
// Save the job object for later status checking
//mPrintJobs.add(printJob);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment