From 1fb025a603f1652ed21cef04c32e13436415eff4 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Fri, 6 Sep 2024 19:26:30 -0400 Subject: [PATCH] Basics of credential printer --- .gitignore | 4 ++- mccs-it/thermal/app.js | 45 +++++++++++++++++++++++++++++++ mccs-it/thermal/package.json | 20 ++++++++++++++ mccs-it/thermal/public/index.html | 21 +++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 mccs-it/thermal/app.js create mode 100644 mccs-it/thermal/package.json create mode 100644 mccs-it/thermal/public/index.html diff --git a/.gitignore b/.gitignore index c5fd749..8907fea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -mccs-it/data \ No newline at end of file +mccs-it/data +node_modules +package-lock.json \ No newline at end of file diff --git a/mccs-it/thermal/app.js b/mccs-it/thermal/app.js new file mode 100644 index 0000000..f29ae23 --- /dev/null +++ b/mccs-it/thermal/app.js @@ -0,0 +1,45 @@ +const express = require('express'); +const ThermalPrinter = require('node-thermal-printer').printer; +const Types = require('node-thermal-printer').types; +const printer = require('printer'); + +const app = express(); +const port = 3000; + +// Middleware to serve static files (HTML) +app.use(express.static('public')); + +// Setup thermal printer +const thermalPrinter = new ThermalPrinter({ + type: Types.STAR, // Replace with the correct printer type (e.g., STAR, EPSON) + interface: 'printer:TSP100', // Printer name as recognized by the OS + driver: printer, // Use node-printer driver +}); + +// Test print function +app.post('/test-print', async (req, res) => { + try { + // Initialize the printer + const isConnected = await thermalPrinter.isPrinterConnected(); + if (!isConnected) { + return res.status(500).json({ message: 'Printer not connected' }); + } + + // Print a test line + thermalPrinter.println('This is a test print'); + thermalPrinter.cut(); + + // Execute the print job + await thermalPrinter.execute(); + + res.json({ message: 'Print Success' }); + } catch (error) { + console.error('Print Error:', error); + res.status(500).json({ message: 'Print Error', error: error.message }); + } +}); + +// Start the server +app.listen(port, () => { + console.log(`Server running at http://localhost:${port}`); +}); diff --git a/mccs-it/thermal/package.json b/mccs-it/thermal/package.json new file mode 100644 index 0000000..521796c --- /dev/null +++ b/mccs-it/thermal/package.json @@ -0,0 +1,20 @@ +{ + "name": "thermal", + "version": "1.0.0", + "description": "Web portal to print to a TSP100 thermal printer.", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://git.vfsh.dev/voidf1sh/custom-scripts" + }, + "author": "Skylar Grant", + "license": "MIT", + "dependencies": { + "express": "^4.19.2", + "node-thermal-printer": "^4.4.3", + "printer": "^0.4.0" + } +} diff --git a/mccs-it/thermal/public/index.html b/mccs-it/thermal/public/index.html new file mode 100644 index 0000000..f96c119 --- /dev/null +++ b/mccs-it/thermal/public/index.html @@ -0,0 +1,21 @@ + + + + + + Test Print + + +

Thermal Printer Test

+ + + + +