6

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

4

3 回答 3

12

试试下面的代码

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
	}

于 2015-03-18T12:38:06.020 回答
6
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();
        }
    }
}
于 2016-12-28T14:08:32.980 回答
0

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

于 2018-09-21T07:28:08.373 回答