【问题标题】:Can we take a print from Web View?我们可以从 Web View 打印吗?
【发布时间】:2012-07-17 06:33:30
【问题描述】:

我的简单问题是我们可以通过 WIFI 打印从 Web 视图打印页面吗?
我已经制作了一页并显示在 Web 视图中,我可以打印该页吗?
在模拟器中它没有任何选项,但在 android wifi 支持手机中有打印选项。
请帮忙。

【问题讨论】:

    标签: android printing


    【解决方案1】:

    试试下面的代码

    public  void createWebPagePrint(WebView webView) {
    		/*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) 
                return;*/
    	    PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
    	    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
    	    String jobName = getString(R.string.webapp_name) + " Document";
    	    PrintAttributes.Builder builder = new PrintAttributes.Builder();
    	    builder.setMediaSize(PrintAttributes.MediaSize.ISO_A5);
    	    PrintJob printJob = printManager.print(jobName, printAdapter, builder.build());
    	   
    	    if(printJob.isCompleted()){
    	    	Toast.makeText(getApplicationContext(), R.string.print_complete, Toast.LENGTH_LONG).show();
    	    }
    	    else if(printJob.isFailed()){
    	    	Toast.makeText(getApplicationContext(), R.string.print_failed, Toast.LENGTH_LONG).show();
    	    }
    	    // Save the job object for later status checking
    	}

    【讨论】:

    • 如何实现?
    • @PraveshAgrawal 请解释您的要求,我可以帮助您。在我的示例中,此方法接受您必须打印的 webView。
    • 我想以这样的方式实现它,以便它可以直接与 HTML 打印按钮一起使用,而无需额外按下打印按钮来运行此功能。
    • @PraveshAgrawal 如果有帮助,请检查以下答案。如果您遇到任何问题或疑问,请发表评论
    【解决方案2】:
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.print.PrintAttributes;
    import android.print.PrintDocumentAdapter;
    import android.print.PrintJob;
    import android.print.PrintManager;
    import android.webkit.JavascriptInterface;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.FrameLayout;
    import android.widget.Toast;
    
    public class TempActivity extends Activity {
        private WebView mMainWebview;
        private WebView print_webview;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mMainWebview = new WebView(TempActivity.this);
            setContentView(R.layout.activity_temp);// mention layout of you activity here
            FrameLayout webViewpar = (FrameLayout) findViewById(R.id.webViewParent);
            webViewpar.addView(mMainWebview);
            print_webview = (WebView) findViewById(R.id.print_view); //view on which you want to take a printout
            mMainWebview.addJavascriptInterface(new TempActivity.WebViewJavaScriptInterface(this), "androidapp"); //
            // "androidapp is used to call methods exposed from javascript interface, in this example case print
            // method can be called by androidapp.print(String)"
            // load your data from the URL in web view
            if (savedInstanceState != null) {
                mMainWebview.restoreState(savedInstanceState);
            } else {
                mMainWebview.loadUrl("your website URL");
            }
        }
    
        public class WebViewJavaScriptInterface {
    
            /*
             * Need a reference to the context in order to sent a post message
             */
            public WebViewJavaScriptInterface(Context context) {
    
            }
    
            /**
             * method defined by developer, which will be called by web page , from web developer
             * this you can call when want to take a print, either on click of a button or directly
             * Convert HTML data, to be printed,  in form of a string and pass to this method.
             *
             * @param data
             */
            @JavascriptInterface
            public void print(final String data) {
    
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        doWebViewPrint(data);
    
                    }
                });
            }
        }
    
        private void doWebViewPrint(String ss) {
    
            print_webview.setWebViewClient(new WebViewClient() {
    
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
    
                    createWebPagePrint(view);
                    super.onPageFinished(view, url);
                }
            });
            // Generate an HTML document on the fly:
            print_webview.loadDataWithBaseURL(null, ss, "text/html", "UTF-8", null);
        }
    
        public void createWebPagePrint(WebView webView) {
    
            PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
            PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
            String jobName = getString(R.string.webapp_name) + " Document";
            PrintAttributes.Builder builder = new PrintAttributes.Builder();
            builder.setMediaSize(PrintAttributes.MediaSize.ISO_A5);
            PrintJob printJob = printManager.print(jobName, printAdapter, builder.build());
    
            if (printJob.isCompleted()) {
                Toast.makeText(getApplicationContext(), R.string.print_complete, Toast.LENGTH_LONG).show();
            } else if (printJob.isFailed()) {
                Toast.makeText(getApplicationContext(), R.string.print_failed, Toast.LENGTH_LONG).show();
            }
        }
    }
    

    【讨论】:

    • 它需要像androidapp.print一样调用,反正答案很不错!谢谢。
    【解决方案3】:

    某些 android/Ios 版本不允许使用 window.print() 直接打印。所以选项是放置一个浮动按钮来打印页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2016-08-31
      • 1970-01-01
      相关资源
      最近更新 更多