Maybe ready to test

This commit is contained in:
Skylar Grant 2024-09-09 11:38:53 -04:00
parent 1fb025a603
commit 8d648392ed
2 changed files with 57 additions and 23 deletions

View File

@ -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 });

View File

@ -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');
}
});
}
}