https://github.com/kruceo/thermal-printer
A basic USB thermal printer API
https://github.com/kruceo/thermal-printer
api thermal-printer usb
Last synced: 2 months ago
JSON representation
A basic USB thermal printer API
- Host: GitHub
- URL: https://github.com/kruceo/thermal-printer
- Owner: Kruceo
- Created: 2024-04-30T12:09:48.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-07T12:19:42.000Z (11 months ago)
- Last Synced: 2025-02-22T08:34:55.615Z (8 months ago)
- Topics: api, thermal-printer, usb
- Language: Go
- Homepage:
- Size: 884 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Thermal Printer Server
This works only with USB printers, (tested only with EPSON TM-T20X).
## Requirements
- **Linux**
- golang
- libusb-1.0-0
- libusb-1.0-0-dev
- **Windows**
- golang
- wsys2 (optional)
- libusb-1.0
- gcc
- pkg-config
- zadig (optional)
## Running From Source on LinuxOn Linux, setting up to run this program is straightforward. Just install `libusb-1.0` on your distribution, use `go build` to build the program, and then execute the binary.
## Running From Source on Windows
To run this program on Windows, you’ll need to install `gcc`, `pkg-config`, and `libusb-1.0`. It’s recommended to use **MinGW** or a similar tool to install these dependencies. In this example, we’ll use [MSYS2](https://www.msys2.org/) for easy setup, although there are other methods available.
> This guide assumes MSYS2 is installed at `C:/msys2`.
**In an MSYS2 shell:**
```shell
pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-x86_64-pkg-config
pacman -S mingw-w64-x86_64-libusb
```### Important Steps
After installing these packages, you’ll need to update your Windows environment variables:
1. Add `C:/msys2/ucrt/bin` and `C:/msys2/mingw64/bin` to the `PATH` environment variable.
2. If `PKG_CONFIG_PATH` doesn’t exist, create it and set it to `C:/msys2/mingw64/lib/pkgconfig`.This setup allows you to successfully run `go build`.
### Troubleshooting Device Detection
If the program doesn’t recognize your device, it may be due to driver issues. You can use the [Zadig](https://zadig.akeo.ie/) tool to replace the device driver with *WinUSB* or *libusbK*, then try running the program again.
## Configuration
Use `.env` file to configure the server.
```env
PRINTER_VENDOR=EPSON
PRINTER_NAME=TM-T20X
PRINTER_WIDTH=48
PRINTER_CHARSET=WPC1254_Turkish
```## Tips
### List Devices
You can use `usbutils` to easily reach your device vendor and name.
Using the command `lsusb`.### Running in Windows WSL and Windows Docker
You will like to use `usbip` in windows WSL to run this in a Windows Platform
## Containers
This works in containers, you will like to pass your USB hub with `--device` option in docker.
```bash
docker run --device=/dev/bus/usb:/dev/bus/usb rafola/thermal-printer
```Extended:
```bash
docker run -p 8888:8888 --device=/dev/bus/usb:/dev/bus/usb -e PRINTER_CHARSET=WPC1254_Turkish -e PRINTER_WIDTH=48 -e PRINTER_VENDOR=EPSON -e PRINTER_NAME=TM-T20X rafola/thermal-printer
```## Endpoints
- GET /get/width
- POST /query### ***get/width***
Returns the printer previous configured printer width.
```json
{ "width":48 }
```#### Example:
```js
const res = await fetch("http://localhost:8888/get/width");
const data = await res.json();
console.log(data)
```### ***/query***
Prints the requested form.
#### POST FORM:
```json
{
"query":[
["center"],
["println","Hello world"],
["left"],
["println","Ass: Kruceo"],
["cut"]
]
}
```#### Example:
```js
const body = {
query:[
["println","Hello World"],
["println","This is a simple test!"],
["println",""],
["cut"]
]
}const options = {
method:"POST",
headers:{"Content-Type":"application/json"},
body:JSON.stringify(body)
}const res = await fetch("http://localhost:8888/query",options);
console.log(res.status)
```