Проблема с отображением диаграммы в режиме предварительного просмотра через Android WebView - PullRequest
0 голосов
/ 29 марта 2019

У меня есть отчет (в формате html), который отображается через WebView.Отчеты в порядке при просмотре через приложение.Но когда я нажимаю кнопку «Печать» (использовала предоставленную функциональность PrintManager), на странице предварительного просмотра печати отсутствуют диаграммы, изображения, которые отображаются в WebView.

Я новичок, поэтому любая помощь очень полезна.приветствуется.

приватный void loadWebView () {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    WebView.setWebContentsDebuggingEnabled(true);
}

final WebView webView = new WebView(NewReportView.this);

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    webSettings.setAllowFileAccessFromFileURLs(true);
    webSettings.setAllowUniversalAccessFromFileURLs(true);
    webSettings.setDomStorageEnabled(true);
}

webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);

webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

//Current-Report
final JSONObject reportJson = new JSONObject();
try {
    reportJson.put("Current-Report", jsonDataCurr);
    reportJson.put("Previous-Report", jsonDataPrev);

    reportJson.put("Master-Info", jsonDataMasterInfo);
    reportJson.put("Peripheral-Info", jsonDataPeripheralInfo);

    webView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    webView.evaluateJavascript("init('" + reportJson + "');", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {
                            //createWebPrintJob(webView);
                            mWebView = null;
                        }
                    });
                } else {
                    webView.loadUrl("javascript:init('" + reportJson + "')");
                    createWebPrintJob(webView);
                }

                if(sessionManager.getMasterVersion().equalsIgnoreCase("v2")) {
                    webView.loadUrl("file:///android_asset/report_print_new.html");
                }else {
                    webView.loadUrl("file:///android_asset/REPORT.html");
                }
                // Keep a reference to WebView object until you pass the PrintDocumentAdapter
                // to the PrintManager
                mWebView = webView;
                createWebPrintJob(webView);

            } catch (JSONException e) {
                e.printStackTrace();
            }
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView mWebView) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
        return;

    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);

    String jobName = getString(R.string.app_name) + " Report";

    // Get a print adapter instance
    PrintDocumentAdapter printAdapter = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        printAdapter = mWebView.createPrintDocumentAdapter("Report.pdf");
    } else {
        printAdapter = mWebView.createPrintDocumentAdapter();
    }

    PrintAttributes attributes = new PrintAttributes.Builder()
            .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
            .build();

    // Create a print job with name and adapter instance
    PrintJob printJob = printManager.print(jobName, printAdapter, attributes);

}
...