A simple Cordova plugin for printing receipts from an Epson TM printer. π§Ύπ¨οΈ
This plugin defines a global EpsonTM object, which is available after the
deviceready event.
IMPORTANT: If you are building an app with this plugin for a simulator on a Mac with Apple Silicon, then you may encounter a build error. To resolve the issue, exclude
arm64for the simulator SDK under both <Project>.xcodeproj and CordovaLib.xcodeproj. See technote TN3117 for more details.
cordova plugin add cordova-plugin-epson-tm
The EpsonTM object has constants for the following compatible printer models:
m10m30P20P60P60IIP80T20T60T70T81T82T83T88T90T90KPU220U330L90H6000T83IIIT100
Additionally, but most importantly, the EpsonTM object has three methods.
Ideally, they should be used in the same component (yes, this plugin is
UI-library-agnostic).
printReceipt(args, success, error)startPrinterSearch(success, error)stopPrinterSearch(success, error)
Prints a receipt using the specified args.
Parameters:
argsβ An object that contains the following properties:modelβ The Epson TM printer model (e.g.,EpsonTM.m30).linesβ The lines on the receipt.includeCustomerCopyβ Indicates whether a second copy of the receipt will be printed. The default isfalse.
successβ A callback to invoke when the receipt is printed. It passestrue.errorβ A callback to invoke when there is an error. It passes one of the following errors:PRINTER_NOT_FOUNDβ An Epson TM printer was not found by the service.INVALID_PRINTER_MODELβ The Epson TM printer model is invalid.CANNOT_CONNECT_PRINTERβ The service cannot connect to an Epson TM printer.BLANK_RECEIPTβ The receipt is blank.
EpsonTM.printReceipt( { model: EpsonTM.m30, lines: [ " BUSINESS NAME ", " 1234 Main Street ", " City, ST 54321 ", " 1(123)456-7890 ", "------------------------------", "Lorem ipsum $1.25", "Dolor sit amet $7.99", "Consectetur $26.70", "Adipiscing elit $15.49", "Sed semper $18.79", "Accumsan ante $42.99", "Non laoreet $9.99", "Dui dapibus eu $27.50\n", "Sub Total $150.70", "Sales Tax $5.29", "------------------------------", "TOTAL $155.99", ], includeCustomerCopy: true, }, (result) => console.log(`Success? ${result}`), (error) => { if (error === EpsonTM.Error.CANNOT_CONNECT_PRINTER) { console.log("Cannot connect to a printer.") } } )
Starts searching for an Epson TM printer.
This method will keep running in the background until either a printer is found
or the stopPrinterSearch method is called. Ideally, it should be called when
the component is created.
Parameters:
successβ A callback to invoke when the printer search is started. It passestrue.errorβ A callback to invoke when there is an error. It passes the errorCANNOT_START_PRINTER_SEARCH.
EpsonTM.startPrinterSearch( (result) => console.log(`Success? ${result}`), (error) => { if (error === EpsonTM.Error.CANNOT_START_PRINTER_SEARCH) { console.log("Cannot start printer search.") } } )
Stops searching for an Epson TM printer.
Ideally, this method should be called when the component is destroyed.
Parameters:
successβ A callback to invoke when the printer search is stopped. It passestrue.errorβ A callback to invoke when there is an error. It passes the errorCANNOT_STOP_PRINTER_SEARCH.
EpsonTM.stopPrinterSearch( (result) => console.log(`Success? ${result}`), (error) => { if (error === EpsonTM.Error.CANNOT_STOP_PRINTER_SEARCH) { console.log("Cannot stop printer search.") } } )
Here is how one would use the plugin in a Svelte component:
<script> import {onDestroy, onMount} from "svelte" const printReceipt = () => { EpsonTM.printReceipt( { model: EpsonTM.m30, lines: receiptLines, includeCustomerCopy: true, }, (result) => {}, (error) => { // Handle the error here. } ) } onMount( () => EpsonTM.startPrinterSearch() ) onDestroy( () => EpsonTM.stopPrinterSearch() ) </script> <button on:click={printReceipt}> Print Receipt </button>
For a more concrete example, please check out this repo.