From d6e1adbbbcdbbd5e5dcb650a04b0bcf3b0051970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Tue, 29 Jan 2019 16:44:54 +0100 Subject: [PATCH] Print HTML markup --- src/android/PrintManager.java | 28 +++++++++++++++++++++++++--- src/android/Printer.java | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/android/PrintManager.java b/src/android/PrintManager.java index 8df07f4..15a6bce 100644 --- a/src/android/PrintManager.java +++ b/src/android/PrintManager.java @@ -30,8 +30,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.print.PrintHelper; import android.view.View; +import android.webkit.WebView; +import android.webkit.WebViewClient; import org.apache.cordova.engine.SystemWebView; +import org.apache.cordova.engine.SystemWebViewClient; import org.json.JSONArray; import org.json.JSONObject; @@ -46,6 +49,8 @@ class PrintManager { // The application context private final @NonNull Context context; + private @Nullable WebView webView; + /** * Constructor * @@ -140,7 +145,7 @@ class PrintManager { { PrintOptions options = new PrintOptions(settings); String jobName = options.getJobName(); - SystemWebView webView = (SystemWebView) view; + WebView webView = (WebView) view; ((Activity) context).runOnUiThread(() -> { PrintDocumentAdapter adapter; @@ -153,7 +158,6 @@ class PrintManager { printAdapter(adapter, options); }); - } /** @@ -165,7 +169,25 @@ class PrintManager { private void printHtml (@NonNull String content, @NonNull JSONObject settings) { - // TODO implement me + ((Activity) context).runOnUiThread(() -> { + webView = new WebView(context); + + webView.setWebViewClient(new WebViewClient() { + @Override + public boolean shouldOverrideUrlLoading (WebView view, String url) { + return false; + } + + @Override + public void onPageFinished (WebView view, String url) { + printView(webView, settings); + webView = null; + } + }); + + webView.loadDataWithBaseURL("file:///android_asset/www/", content, "text/html", "UTF-8", + null); + }); } /** diff --git a/src/android/Printer.java b/src/android/Printer.java index b3170dd..e15cf52 100644 --- a/src/android/Printer.java +++ b/src/android/Printer.java @@ -22,6 +22,7 @@ package de.appplant.cordova.plugin.printer; import android.support.annotation.Nullable; +import android.webkit.WebView; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; @@ -129,5 +130,4 @@ public class Printer extends CordovaPlugin { pm.print(content, settings, webView.getView(), callback::success); }); } - }