From 8d648392edf8c6a2ae4fee7f71c22e6ee8a18e7e Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Mon, 9 Sep 2024 11:38:53 -0400 Subject: [PATCH] Maybe ready to test --- mccs-it/thermal/app.js | 25 ++------------ mccs-it/thermal/modules/Thermal.js | 55 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 mccs-it/thermal/modules/Thermal.js diff --git a/mccs-it/thermal/app.js b/mccs-it/thermal/app.js index f29ae23..6fd3a49 100644 --- a/mccs-it/thermal/app.js +++ b/mccs-it/thermal/app.js @@ -1,6 +1,5 @@ const express = require('express'); -const ThermalPrinter = require('node-thermal-printer').printer; -const Types = require('node-thermal-printer').types; +const Thermal = require('./modules/Thermal.js'); const printer = require('printer'); const app = express(); @@ -9,30 +8,10 @@ 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' }); + Thermal.generateFile('Grant.Aiden2', '5012345'); } catch (error) { console.error('Print Error:', error); res.status(500).json({ message: 'Print Error', error: error.message }); diff --git a/mccs-it/thermal/modules/Thermal.js b/mccs-it/thermal/modules/Thermal.js new file mode 100644 index 0000000..ac9ecd5 --- /dev/null +++ b/mccs-it/thermal/modules/Thermal.js @@ -0,0 +1,55 @@ +const PDFDocument = require('pdfkit'); +const { spawn } = require('child_process'); + + +module.exports = { + generateFile(username, studentId) { + // Create the output text + const infoArray = [ + 'Username:', + `${username}`, + '', + 'Email:', + `${username}@kvcc.me.edu`, + '', + 'Student ID:', + `${studentId}`, + '', + 'Note: When signing into the MyKV Portal, use your Username not your Email.', + 'For further assistance contact ithelp@mainecc.edu' + ]; + const infoString = infoArray.join('\n'); + // Create a new PDF document + const doc = new PDFDocument(); + + // Create a print process (e.g., using `lp` to print the PDF directly) + const printer = spawn('lp', ['-d', 'ITThermal']); // Replace 'printer_name' with your actual printer + + // Pipe the PDF document output to the printing process + doc.pipe(printer.stdin); + + // Add image and text to the PDF + doc.image('../public/itslogo.png', { + fit: [576, 261], + align: 'center', + valign: 'top' + }); + + doc.moveDown(); + doc.fontSize(16).text(infoString, { + align: 'left' + }); + + // Finalize the PDF and close the stream + doc.end(); + + // Listen for the print process to finish + printer.on('close', (code) => { + if (code === 0) { + console.log('Printed successfully'); + } else { + console.error('Failed to print'); + } + }); + } +} \ No newline at end of file