bprinter
v1.0.3
Published
A unified printing API for Electron applications supporting thermal and standard printers
Maintainers
Readme
electron-print-service
API d'impression unifiée pour applications Electron - supporte les imprimantes thermiques (ESC/POS) et standard.
Prérequis
- Node.js >= 16.0.0
- Electron >= 20.0.0
Démarrage rapide
const { getPrinters, printHtml, printPdf, printThermal } = require('bprinter');
// Lister les imprimantes disponibles
const printers = await getPrinters();
// Imprimer du HTML sur une imprimante standard
await printHtml('<h1>Bonjour</h1>', { printerName: 'HP LaserJet' });
// Imprimer un PDF
await printPdf('./document.pdf', { printerName: 'HP LaserJet' });
// Imprimer sur une imprimante thermique
await printThermal({
printerName: 'EPSON TM-T88V',
text: 'Reçu #123',
cut: 'full'
});API
Découverte d'imprimantes
getPrinters()
Retourne la liste des imprimantes disponibles avec détection automatique du type (thermique/standard).
const printers = await getPrinters();
// [{ name: 'HP LaserJet', displayName: 'HP LaserJet Pro', isDefault: true, status: 0, type: 'standard' }]Impression standard (A4/A5/Letter/Legal)
printHtml(html, options)
Imprime du contenu HTML sur une imprimante standard.
await printHtml('<h1>Facture</h1><p>Total: 100€</p>', {
printerName: 'HP LaserJet',
pageSize: 'A4', // 'A4', 'A5', 'Letter', 'Legal' ou { width, height } en mm
landscape: false,
margins: { top: 10, right: 10, bottom: 10, left: 10 }, // en mm
copies: 2,
duplex: 'longEdge', // 'simplex', 'longEdge', 'shortEdge'
silent: true
});printPdf(pdfPath, options)
Imprime un fichier PDF.
await printPdf('./document.pdf', {
printerName: 'HP LaserJet',
copies: 2,
silent: true
});Impression thermique (ESC/POS)
printThermal(options)
Imprime du texte ou des images sur une imprimante thermique.
// Texte simple
await printThermal({
printerName: 'EPSON TM-T88V',
text: 'Hello World!',
cut: 'full'
});
// Avec formatage
await printThermal({
printerName: 'EPSON TM-T88V',
text: 'REÇU',
bold: true,
align: 'center',
textWidth: 2,
textHeight: 2,
cut: 'full',
openCashDrawer: true
});
// Image
await printThermal({
printerName: 'EPSON TM-T88V',
image: './receipt.png',
width: 80,
cut: 'partial'
});| Option | Type | Défaut | Description |
|--------|------|--------|-------------|
| printerName | string | - | Nom de l'imprimante (requis) |
| text | string | - | Texte à imprimer |
| image | string/Buffer | - | Image (chemin ou buffer) |
| width | number | 80 | Largeur papier en mm (58 ou 80) |
| printerType | string | 'epson' | 'epson', 'star', 'brother' |
| cut | string | - | 'full' ou 'partial' |
| openCashDrawer | boolean | false | Ouvrir le tiroir-caisse |
| bold | boolean | false | Texte en gras |
| underline | boolean | false | Texte souligné |
| align | string | 'left' | 'left', 'center', 'right' |
| textWidth | number | 1 | Multiplicateur largeur (1-8) |
| textHeight | number | 1 | Multiplicateur hauteur (1-8) |
printThermalHtml(html, options)
Imprime du HTML sur une imprimante thermique (rendu en image).
await printThermalHtml('<h1>Reçu</h1><p>Merci!</p>', {
printerName: 'EPSON TM-T88V',
width: 80,
cut: 'full'
});printThermalPdf(pdfPath, options)
Imprime un PDF sur une imprimante thermique (rendu en images).
await printThermalPdf('./receipt.pdf', {
printerName: 'EPSON TM-T88V',
width: 58,
cut: 'full'
});Retour des fonctions
Toutes les fonctions d'impression retournent un PrintResult :
{
success: true,
printerName: 'HP LaserJet',
timestamp: Date,
error: 'Message d\'erreur' // uniquement si success = false
}Utilisation avancée
EscPosEncoder (Builder Pattern)
Pour construire des reçus complexes :
const { EscPosEncoder } = require('bprinter');
const encoder = new EscPosEncoder('epson');
encoder
.alignCenter()
.bold(true)
.println('NOM DU MAGASIN')
.bold(false)
.alignLeft()
.drawLine()
.leftRight('Article 1', '10.00€')
.leftRight('Article 2', '15.00€')
.drawLine()
.leftRight('Total', '25.00€')
.newLine(2)
.qrCode('https://example.com')
.cut();
const buffer = encoder.build();Méthodes disponibles :
| Méthode | Description |
|---------|-------------|
| text(content) | Texte sans retour à la ligne |
| println(content) | Texte avec retour à la ligne |
| newLine(count) | Lignes vides |
| alignLeft/Center/Right() | Alignement |
| bold(enabled) | Gras |
| underline(enabled) | Souligné |
| fontSize(width, height) | Taille (1-8) |
| drawLine(char) | Ligne horizontale |
| leftRight(left, right) | Texte gauche-droite |
| barcode(data, type) | Code-barres |
| qrCode(data, options) | QR code (voir ci-dessous) |
| image(pathOrBuffer) | Image (async) |
| cut(partial) | Couper le papier |
| cashDrawer(pin) | Ouvrir tiroir-caisse |
| build() | Obtenir le buffer |
QR Code
// QR Code simple
encoder.qrCode('https://example.com');
// QR Code avec options
encoder.qrCode('https://example.com/receipt/12345', {
size: 6, // Taille du module (1-16, défaut: 6)
errorLevel: 'M' // Niveau de correction: 'L', 'M', 'Q', 'H' (défaut: 'M')
});| Option | Type | Défaut | Description |
|--------|------|--------|-------------|
| size | number | 6 | Taille du module QR (1-16) |
| errorLevel | string | 'M' | Correction d'erreur: L(7%), M(15%), Q(25%), H(30%) |
Classes exportées
const {
ThermalPrintHandler, // Impression thermique bas niveau
StandardPrintHandler, // Impression standard bas niveau
EscPosEncoder, // Constructeur de commandes ESC/POS
HtmlRenderer, // Rendu HTML vers image
PdfRenderer // Rendu PDF vers image
} = require('bprinter');Constantes
const {
DEFAULTS, // Valeurs par défaut
PAGE_SIZES, // { A4, A5, Letter, Legal }
DUPLEX_MODES, // { simplex, longEdge, shortEdge }
SUPPORTED_PRINTER_TYPES, // ['epson', 'star', 'brother']
THERMAL_DPI, // 203
ErrorCodes // Codes d'erreur
} = require('bprinter');Gestion des erreurs
const { PrintServiceError, ErrorCodes } = require('bprinter');
try {
await printHtml('<h1>Test</h1>', { printerName: 'Imprimante invalide' });
} catch (error) {
if (error instanceof PrintServiceError) {
console.log(error.code); // ex: 'PRINTER_NOT_FOUND'
console.log(error.message);
console.log(error.details);
}
}Codes d'erreur :
| Code | Description |
|------|-------------|
| PRINTER_NAME_REQUIRED | printerName manquant |
| PRINTER_NOT_FOUND | Imprimante introuvable |
| PRINTER_UNAVAILABLE | Imprimante hors ligne |
| INVALID_OPTIONS | Options invalides |
| RENDER_FAILED | Échec du rendu HTML/PDF |
| PDF_LOAD_FAILED | Fichier PDF introuvable ou invalide |
| PRINT_FAILED | Échec de l'impression |
| ESCPOS_ENCODE_FAILED | Échec de l'encodage ESC/POS |
| DISCOVERY_FAILED | Échec de la découverte d'imprimantes |
Imprimantes supportées
Thermiques (ESC/POS)
- Epson TM series (TM-T88, TM-T20, TM-M30, etc.)
- Star TSP series
- Brother QL series
- Citizen CT series
- Bixolon SRP series
- Imprimantes POS génériques
Standard
- Toute imprimante système supportant A4, A5, Letter, Legal
Dépendances
node-thermal-printer- Encodage ESC/POSpdf-to-printer- Impression PDF nativesharp- Traitement d'images
Licence
MIT
