Giter Club home page Giter Club logo

cordova-plugin-usbprint's Introduction

cordova-plugin-usbprint

This Plugin allows you to print documents via USB printer.

Supported Platform

  • Android

Usage

Installation

cordova plugin add cordova-plugin-usbprint --save

Once application was started "UsbPrint" will be available in "window.cordova.plugins". So you can access plugin as

window['cordova'].plugins.UsbPrinter....()

Methods

  • getConnectedPrinters(successCallback, failureCallback) :=> Gets all connected USB printer.
  • connect(printername, successCallback, failureCallback) :=> Connect or gets permission for the mentioned printer identified via "printername". "printername" can be identified from the printers list got from above method call.
  • disconnect(printername, successCallback, failureCallback) :=> Disconnect or gets permission for the mentioned printer identified via "printername". "printername" can be identified from the printers list got from above method call.
  • print(printername, msg, successCallback, failureCallback) :=> Get all connected USB printer.
  • isPaperAvailable(printername, successCallback, failureCallback) :=> Tells whether paper is available in mentioned printer.
  • sendCommand(printername, command, successCallback, failureCallback) :=> Sends POS command to the printer device.
  • cutPaper(printername, successCallback, failureCallback) :=> Trigger Full cut paper event to printer.

Step 1:

First scan for all connected printers via USB

            window['cordova'].plugins.UsbPrinter.getConnectedPrinters((result) => {
                console.log(result);
                // result will be list of printers connected to the usb device
                // success callback execution
            }, err => {
                console.error(err)
                // failure callback execution
            });

Step 2:

Connect or get permission for the printer from app. Connect to the printer using printer_name.

            window['cordova'].plugins.UsbPrinter.connect(printer.printername, (result) => {
                console.log(result)
                // success callback execution
            }, err => {
                // use this method to recognise the disconnection of usb device
                console.error(err)
                // failure callback execution
            });

Note: When USB gets disconnected from the mobile device, then failure callback is invoked. So use this to reecognise the unsuccesful connection and disconnection from mobile device.

Step 3:

Send string to print.

            window['cordova'].plugins.UsbPrinter.print(printer.printername, message, (result) => {
                console.log("result of usb print action", result)
                // successful callback execution
            }, err => {
                console.error('Error in usb print action', err)
                // failure callback execution
            });

cordova-plugin-usbprint's People

Contributors

arunyogi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cordova-plugin-usbprint's Issues

how to print an image and format the text

hello, I get this result when I run an impression.
Can you help me print the image and format the text ?
20190701_114754

<script> var message=document.getElementById("ticket").innerHTML; window['cordova'].plugins.UsbPrinter.print(printername, message, (result) => { console.log("result of usb print action", result) // successful callback execution }, err => { console.error('Error in usb print action', err) // failure callback execution }); </script>
<img src='img/logo_gras.png'><br/>
<span style='font-size: 24px'>PROFORMA</span><br/>
<span style='font-size: 15px'> vous serez recu sous le numero</span><br/>
<span style='font-size:150px; font-weight:bold'>C0002</span><br/>
<span>23 janv 2019 à 2019
</span><br/>
<span style='font-size: 15px'> 10 usages avant vous</span><br/>
<span style='font-size: 15px'> Veuillez patienter s'il vous plait</span><br/>

How to use sendCommand function?

I tried to send ESC Command to USB Printer but got into problem.
How should I format the message parameter?

 var message = [27, 64, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];
 window['cordova'].plugins.UsbPrinter.sendCommand(printername, message, (result) => {
            alert("result of usb print action: " + result);
            // successful callback execution
        }, err => {
            alert('Error in usb print action: ' + err);
            // failure callback execution
        });

The printer is connected successfully. I got response "Send" from success callback.
But the printer printed garbled text.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.