How to print content from a webview in android studio

0

Good afternoon, I'm trying to print the contents of a webview in android on a mini bluetoo printer but I have not been able to do it, this is what I have loaded the web but I need to print what it loads.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView);
    bar=(ProgressBar) findViewById(R.id.progressBar2);
    webView.setWebViewClient(new myWebclient());
    webView.setWebChromeClient(new WebChromeClient());

    webView.getSettings().setJavaScriptEnabled(true);

    webView.loadUrl("http://www.miweb.com/index.html");

}

public class myWebclient extends WebViewClient{
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        bar.setVisibility(View.GONE);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        bar.setVisibility(View.VISIBLE); //* muestra ProgressBar
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return super.shouldOverrideUrlLoading(view, url);
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if((keyCode==KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
        webView.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}

    
asked by juan perez 07.09.2018 в 22:23
source

1 answer

0

What you can do is print an image first with your bluetooth printer, from there you can take a screenshot of the webview and print that image.

With this code you can create a File object and generate a screenshot of the webview.

public File getBitmapFromwebchartView(WebView webView) {
        File file = null;
        if (webView != null) {
            webView.setDrawingCacheEnabled(true);
            Bitmap b = webView.getDrawingCache();
            if (b != null) {
                try {
                    file = new File(Environment.getExternalStorageDirectory(),
                            System.currentTimeMillis() + ".jpg");

                    // write the bytes in file
                    FileOutputStream fo;

                    fo = new FileOutputStream(file);

                    b.compress(Bitmap.CompressFormat.PNG, 60, fo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

The only thing that would be missing would be to know how to print an image with your printer, since depending on the printer the code varies. Example your printer may be Zebra brand, Bixolon, Mobiprint, StarIO, etc. There is an infinity of bluetooth printers and each one has its way of printing according to its library, if you specify the brand it would be easier, but in context that is the idea.

    
answered by 08.09.2018 в 01:21