PrintHub is a JavaScript plugin for printing text using a Bluetooth or USB thermal printer. Demo: PrintHub Demo
- Print QR Code - Generate and print QR codes for payments (QRIS), URLs, tracking, and more. π
- Print Barcode - Generate and print barcodes in various formats (CODE128, EAN13, UPC, etc). π
- Print Image from URL with alignment option.
- 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.
- Supports connecting to USB 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 printhubImport or require PrintHub into your project.
import PrintHub from "printhub";or
const PrintHub = require("printhub");<script src="https://cdn.jsdelivr.net/npm/printhub@latest/dist/index.global.js"></script>Or use specific version:
<script src="https://cdn.jsdelivr.net/npm/printhub@1.2.1/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); }, });
-
Print an image from a URL.
printer.connectToPrint({ onReady: async (print) => { await print.putImageWithUrl("https://example.com/image.png", { align: "center", }); }, onFailed: (message) => { console.log(message); }, });
-
Print QR Code π
Print QR codes for payment (QRIS), URLs, tracking numbers, or any data. Perfect for modern receipts!
printer.connectToPrint({ onReady: async (print) => { // Simple QR Code await print.printQRCode("https://example.com"); // QR Code with options await print.printQRCode("https://example.com/payment", { size: "large", // "small", "medium", "large" align: "center", // "left", "center", "right" errorCorrection: "M", // "L", "M", "Q", "H" }); }, onFailed: (message) => { console.log(message); }, });
Example: Receipt with QRIS Payment
printer.connectToPrint({ onReady: async (print) => { // Header await print.writeText("TOKO SAYA", { align: "center", bold: true, size: "double", }); await print.writeDashLine(); // Items await print.writeTextWith2Column("Nasi Goreng", "Rp 25.000"); await print.writeTextWith2Column("Es Teh", "Rp 5.000"); await print.writeDashLine(); // Total await print.writeTextWith2Column("TOTAL", "Rp 30.000", { bold: true }); await print.writeDashLine(); // QR Code for payment await print.writeText("SCAN UNTUK BAYAR", { align: "center", bold: true, }); await print.writeLineBreak(); await print.printQRCode(qrisData, { size: "large", align: "center", errorCorrection: "M", }); await print.writeLineBreak({ count: 3 }); }, onFailed: (message) => { console.log(message); }, });
QR Code Sizes:
small: 100x100 pixels - Good for URLs and simple datamedium: 200x200 pixels (default) - Recommended for most use caseslarge: 300x300 pixels - Best for payment QR (QRIS, GoPay, OVO, etc.)
Error Correction Levels:
L: ~7% recovery - Clean environmentsM: ~15% recovery (default) - Normal use, recommendedQ: ~25% recovery - Outdoor useH: ~30% recovery - Critical data
For more details, see QR Code Guide.
-
Print Barcode π
Print barcodes in various formats for products, invoices, tickets, and inventory management.
β¨ v1.2.1 Improvements: Barcode now prints in full width with proper aspect ratio and is scannable!
printer.connectToPrint({ onReady: async (print) => { // Simple barcode await print.printBarcode("1234567890"); // Barcode with options await print.printBarcode("5901234123457", { format: "EAN13", // Barcode format width: 2, // Line width (1-4) height: 60, // Height in pixels (default: 60) displayValue: true, // Show text below barcode align: "center", // Alignment }); }, onFailed: (message) => { console.log(message); }, });
Example: Invoice with Barcode
printer.connectToPrint({ onReady: async (print) => { // Header await print.writeText("INVOICE", { align: "center", bold: true, size: "double", }); await print.writeDashLine(); // Invoice Info await print.writeTextWith2Column("No Invoice", "INV-001"); await print.writeTextWith2Column("Tanggal", "06/10/2024"); await print.writeDashLine(); // Items await print.writeTextWith2Column("Product A", "Rp 50.000"); await print.writeTextWith2Column("Product B", "Rp 30.000"); await print.writeDashLine(); // Total await print.writeTextWith2Column("TOTAL", "Rp 80.000", { bold: true }); await print.writeDashLine(); // Barcode for tracking await print.writeText("Scan untuk tracking:", { align: "center", bold: true, }); await print.writeLineBreak(); await print.printBarcode("INV001", { format: "CODE128", width: 2, height: 50, displayValue: true, align: "center", }); await print.writeLineBreak({ count: 3 }); }, onFailed: (message) => { console.log(message); }, });
Supported Barcode Formats:
CODE128(default) - Most versatile, alphanumericCODE39- Alphanumeric, widely usedEAN13- 13-digit product barcodes (e.g., 5901234123457)EAN8- 8-digit product barcodesUPC- Universal Product CodeITF14- 14-digit shipping container codesMSI- Modified Plessey, inventory managementpharmacode- Pharmaceutical packagingcodabar- Libraries, blood banks, logistics
Barcode Options:
format: Barcode type (default: "CODE128")width: Line width, 1-4 (default: 2)height: Height in pixels (default: 50)displayValue: Show text below barcode (default: true)align: Alignment - "left", "center", "right" (default: "center")
For more details, see Barcode Status.
| 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. |
putImageWithUrl(url, options) |
Prints an image from a URL. |
printQRCode(text, options) π |
Generates and prints a QR code. |
printBarcode(text, options) π |
Generates and prints a barcode. |
| Option | Type | Description | Default |
|---|---|---|---|
size |
string | QR Code size: "small" (100px), "medium" (200px), "large" (300px) | "medium" |
align |
string | Alignment: "left", "center", "right" | "center" |
errorCorrection |
string | Error correction level: "L", "M", "Q", "H" | "M" |
onFailed |
function | Callback function when QR code generation fails | - |
| Option | Type | Description | Default |
|---|---|---|---|
format |
string | Barcode format: "CODE128", "CODE39", "EAN13", "EAN8", "UPC", etc | "CODE128" |
width |
number | Line width (1-4) | 2 |
height |
number | Height in pixels | 50 |
displayValue |
boolean | Show text below barcode | true |
align |
string | Alignment: "left", "center", "right" | "center" |
onFailed |
function | Callback function when barcode generation fails | - |
| 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 |
PrintHub is perfect for various business needs:
- Print receipts with QRIS payment QR codes
- Product labels with barcodes (EAN13, UPC)
- Order tracking QR codes for customers
- Product information and pricing labels
- Inventory management with barcode scanning
- Kitchen orders and bills
- Table receipts with payment QR (QRIS, GoPay, OVO)
- Customer feedback survey QR codes
- Digital menu QR codes
- Order number tickets
- E-tickets with QR codes for validation
- Event passes with barcodes
- Queue management tickets
- Access control
- Ticket verification systems
- Sales receipts with payment options
- Product barcodes for inventory
- Customer loyalty program QR codes
- Promotional coupons
- Store information and social media QR codes
- Shipping labels with tracking barcodes (CODE128)
- Package tracking QR codes
- Delivery receipts
- Warehouse management
- Inventory tracking
- Use large size for payment QR codes (QRIS) for easy scanning
- Use medium size for URLs and general purpose
- Use small size when space is limited
- Always use center alignment for better scanning
- For payment QR, use error correction level M or Q
- Add descriptive text above/below the QR code
- Use CODE128 for alphanumeric data (invoices, tracking)
- Use EAN13 or UPC for product barcodes (must be valid format)
- Use CODE39 for simple alphanumeric needs
- Set
displayValue: trueto show the barcode text - Use center alignment for better scanning
- Recommended height: 60-80 pixels for good readability (default is 60px)
- Keep width at 2 for standard printers
- Ensure good contrast (dark barcode on light background)
- New in v1.2.1: Barcode automatically adjusts to paper size
- 58mm paper: Up to 380 pixels wide
- 80mm paper: Up to 570 pixels wide
- Barcode maintains proper aspect ratio for accurate scanning
- Use high contrast images (black & white works best)
- Recommended max size: 120x120 pixels
- Use PNG or JPG format
- Ensure good lighting for best print quality
- Use bold for important information (totals, headers)
- Use double size for emphasis (store name, total amount)
- Use center alignment for headers and important messages
- Use two columns for item-price pairs
- Add line breaks for better readability
- 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 | β |
- BUGFIX: Fixed barcode printing quality issues
- Barcode now prints in full width based on paper size (58mm: 380px, 80mm: 570px)
- Fixed aspect ratio distortion - barcode no longer prints as square box
- Improved barcode rendering quality with dedicated
printBarcodeImage()method - Increased default barcode height from 50px to 60px for better readability
- Barcode now scannable with standard barcode scanners
- Added smart scaling to prevent barcode from exceeding paper width
- Optimized ESC/POS image commands for better print quality
- NEW FEATURE:
printBarcode()- Generate and print barcodes in 9 different formats - Support for CODE128, CODE39, EAN13, EAN8, UPC, ITF14, MSI, pharmacode, and codabar
- Customizable barcode width, height, and display options
- Perfect for product labels, inventory management, invoices, and tickets
- Added comprehensive barcode documentation
- See Barcode Status for implementation details
- NEW FEATURE:
printQRCode()- Generate and print QR codes for payments, URLs, tracking, and more - Added support for 3 QR code sizes (small, medium, large)
- Added support for 4 error correction levels (L, M, Q, H)
- Perfect for QRIS payment, e-commerce tracking, feedback surveys, and more
- Optimized browser build (reduced bundle size by ~50%)
- Improved browser compatibility for bundling
- Added comprehensive QR Code guide documentation
- See QR Code Guide for complete usage examples
- Now supports to print image from URL (
putImageWithUrl) with alignment option - Refactor printing methods for USB and Bluetooth
- Improve error handling and add image printing functionality
- Fix minor bugs
- 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
- Complete Usage Guide - Comprehensive guide with examples for all features
- QR Code Guide - Detailed guide for QR code printing with real-world examples
- Barcode Status - Barcode implementation details and troubleshooting
- Feature Ideas - Upcoming features and roadmap
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the ISC License.
If you encounter any issues or have questions:
- Open an issue on GitHub Issues
- Check the documentation
- See examples in the demo
- QR Code generation powered by qrcode
- Built with TypeScript and modern JavaScript
Made with β€οΈ by Dede Fuji Abdul
Star β this repository if you find it helpful!
