PrintHub is a JavaScript plugin for printing text using a Bluetooth or USB thermal printer. Demo: PrintHub Demo
- Print text with various options like bold, underline, alignment, and text size.
- Print text in two columns.
- Print dashed lines.
- Print line breaks.
- Supports two paper sizes: "58mm" and "80mm".
- Supports connecting to Bluetooth thermal printers.
- Compatible with modern browsers such as Chrome, Firefox, and Edge.
- Node.js compatible.
- Supports usage via CDN.
- Supports usage via NPM.
- ES6 compatible.
npm install printhub
Import or require PrintHub into your project.
import PrintHub from "printhub";
or
const PrintHub = require("printhub");
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.global.js"></script>
You can create an instance of PrintHub with or without specifying the desired paper size. Supported paper sizes are "58" and "80". If the paper size is not specified, the default is "58".
-
Creating a PrintHub instance with "80" paper size
import PrintHub from "printhub"; let printer = new PrintHub();
or
import PrintHub from "printhub"; let printer = new PrintHub({ paperSize: "80", });
-
Creating a PrintHub instance with "80" paper size
let printer = new PrintHub.init({ paperSize: "80", });
-
Creating a PrintHub instance with "58" paper size
let printer = new PrintHub.init();
or you can use the default paper size
let printer = new PrintHub.init({ paperSize: "58", });
You can select the type of printer to use. Supported types are bluetooth
and usb
. If the printer type is not specified, the default is bluetooth
.
- NPM Usage
let printer = new PrintHub({
printerType: "usb",
});
- CDN Usage
let printer = new PrintHub.init({
printerType: "usb",
});
Use the connectToPrint
method to connect to a Bluetooth printer and print text. You need to provide two callback functions: onReady
and onFailed
.
Callback | Description |
---|---|
onReady |
Called when the printer connection is successful. You can use the print object passed to this callback to print text. |
onFailed |
Called when the printer connection fails. You can use the message parameter to get the error message. |
- Connect to the printer and print text.
printer.connectToPrint({
onReady: async (print) => {
await print.writeText("Hello, World!");
},
onFailed: (message) => {
console.log(message);
},
});
-
Print bold text.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { bold: true }); }, onFailed: (message) => { console.log(message); }, });
-
Print underlined text.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { underline: true }); }, onFailed: (message) => { console.log(message); }, });
-
Print text with alignment.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { align: "center" }); }, onFailed: (message) => { console.log(message); }, });
-
Print text with a specific size.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { size: "double" }); }, onFailed: (message) => { console.log(message); }, });
-
Print text in two columns.
printer.connectToPrint({ onReady: async (print) => { await print.writeTextWith2Column("Name", "John Doe"); }, onFailed: (message) => { console.log(message); }, });
-
Print dashed lines.
printer.connectToPrint({ onReady: async (print) => { await print.writeDashLine(); }, onFailed: (message) => { console.log(message); }, });
-
Print line breaks.
printer.connectToPrint({ onReady: async (print) => { await print.writeLineBreak(); }, onFailed: (message) => { console.log(message); }, });
-
Print text with multiple options.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { bold: true, underline: true, align: "center", size: "double", }); }, onFailed: (message) => { console.log(message); }, });
-
Print text with multiple options in two columns.
printer.connectToPrint({ onReady: async (print) => { await print.writeTextWith2Column("Name", "John Doe", { bold: true, underline: true, align: "center", size: "double", }); }, onFailed: (message) => { console.log(message); }, });
Method | Description |
---|---|
writeLineBreak({ count = 1 }) |
Writes a line break. |
writeDashLine() |
Writes a dashed line. |
writeTextWith2Column(text1, text2, options) |
Writes text in two columns. |
writeText(text, options) |
Writes text. |
connectToPrint({ onReady, onFailed }) |
Connects to the printer and calls the onReady or onFailed callback. |
Option | Description | Default |
---|---|---|
bold |
Specifies whether the text is printed in bold. | false |
underline |
Specifies whether the text is printed with an underline. | false |
align |
Specifies text alignment. Supported values: "left", "center", "right". | left |
size |
Specifies the text size. Supported values: "normal", "double". | normal |
- Install Zadig.
- Connect the USB Printer to your computer.
- Open Zadig and select your USB Printer.
- Install the WinUSB driver for your USB Printer.
- Done.
- Connect the USB Printer to your computer.
- Open the terminal and run
lsusb
. - Find your USB Printer and note the vendor id and product id.
- Run
sudo modprobe usblp
. - Run
sudo echo "ATTRS{idVendor}=="YOUR_VENDOR_ID", ATTRS{idProduct}=="YOUR_PRODUCT_ID", MODE="0666", GROUP="plugdev" > /etc/udev/rules.d/99-usb-printer.rules
. - Run
sudo udevadm control --reload-rules
. - Run
sudo udevadm trigger
. - Done.
- Connect the USB Printer to your computer.
- Open the terminal and run
ls /dev/cu.*
. - Find your USB Printer and note the device name.
- Run `sudo chmod
- Run `sudo chown
- Done.
Browser | Version | Status |
---|---|---|
Chrome | 61 | ✔️ |
Firefox | No | ❌ |
Edge | 79 | ✔️ |
Safari | No | ❌ |
Opera | 48 | ✔️ |
Browser | Version | Status |
---|---|---|
Chrome | 61 | ✔️ |
Firefox | No | ❌ |
Safari | No | ❌ |
Opera | 45 | ✔️ |
Samsung | 8.0 | ✔️ |
WebView | No | ❌ |
- Update README.md
- Add support for Node.js (typescript, es6, commonjs)
- Change the way to use instance of PrintHub
- Fix writeTextWith2Column method if length of text1 + text2 is greater than 32 or 42 characters
- Remove unused code
- Update paper size from "58mm" and "80mm" to "58" and "80"
- Added instructions for USB Printer on Windows, Linux, and macOS
- Fixed USB Printer not working