简体   繁体   中英

How to push print to bluetooth thermal printer from android webview?

We configured Odoo POS application in a cloud server and from the desktop, if we click print receipt it will automatically pop up the print dialogue in chrome and can print from the connected device! The same application we developed in android simply putting in webView !

Now I need to print this using connected Bluetooth thermal printer paired to the Android device! I searched a lot but still didn't get any solution! ANy links or suggestions will be great help

If you have access to html/js code of your webpage, you can do as following:

  1. Create special class, that will handle js callbacks:

     private class PrintInterface { @JavascriptInterface // required annotation! public void printSome(String arg) { // here you can pass some args from js code // do some printing here from Java code } } 
  2. Enable js for webview. Then add created interface to webView. As the second param you need to specify the name, that will be used inside js. I called it "PrintInterface", the same as class name:

     webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new PrintInterface(), "PrintInterface");` 
  3. In your js code call appropriate method from PrintInterface. Note that each method must be annotated with @JavascriptInterface (look 1.):

     $("#someHtmlButton").on("click", function() { PrintInterface.printSome("some argument"); } 

So, finally, inside your PrintInterface#printSome method you can do whatever you want straight from Java code.

Some docs about html printing: https://developer.android.com/training/printing/html-docs

Android 4.4 onwards WebView now has the ability to print HTML documents. Please follow this -

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webView = new WebView(this);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new MyJavascriptInterface(this, webView), "Android");
        webView.loadUrl("Odoo receipt url");
        setContentView(webView);

        // Keep a reference to WebView object until you pass the PrintDocumentAdapter
        // to the PrintManager
        mWebView = webView;
    }

    public class MyJavascriptInterface {

        Context context;
        WebView view;

        public MyJavascriptInterface(Context context, WebView view) {
            this.context = context;
            this.view = view;
        }

        @android.webkit.JavascriptInterface
        public void doPrint() {
            createWebPrintJob(view);
        }

        private void createWebPrintJob(WebView webView) {

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

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

            // Get a print adapter instance
            PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);

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

            // Save the job object for later status checking
            mPrintJobs.add(printJob);
        }

    }
}

and then call from the HTML inside the WebView as

<input type="button" onClick="doWebViewPrint()" />

<script type="text/javascript">
    function doWebViewPrint() {
        Android.doPrint();
    }
</script>

For more info, please refer to https://developer.android.com/training/printing/html-docs

such thermal printers merely expect ESC input (escape sequences) and do not support printing HTML markup or graphics (unless they've had been raster-ized before sending them). it's difficult to answer, because the question does not indicate which printer - but in general, one has to render once to HTML - and once to ESC output (assuming common thermal printers, alike Zebra or Epson ). bluetoothserial could also be used to send ESC sequences via SPP ( HTML does not print well). also see answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM