我们可以从Web View中获取打印件吗?

时间:2012-07-18 08:58:48

标签: android printing

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

3 个答案:

答案 0 :(得分:2)

尝试以下代码



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
	}
&#13;
&#13;
&#13;

答案 1 :(得分: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();
        }
    }
}

答案 2 :(得分:0)

某些android / ios版本不允许使用window.print()直接打印。因此,可以选择使用浮动按钮来打印页面。