Maybe ready to test
This commit is contained in:
parent
1fb025a603
commit
8d648392ed
@ -1,6 +1,5 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const ThermalPrinter = require('node-thermal-printer').printer;
|
const Thermal = require('./modules/Thermal.js');
|
||||||
const Types = require('node-thermal-printer').types;
|
|
||||||
const printer = require('printer');
|
const printer = require('printer');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -9,30 +8,10 @@ const port = 3000;
|
|||||||
// Middleware to serve static files (HTML)
|
// Middleware to serve static files (HTML)
|
||||||
app.use(express.static('public'));
|
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
|
// Test print function
|
||||||
app.post('/test-print', async (req, res) => {
|
app.post('/test-print', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// Initialize the printer
|
Thermal.generateFile('Grant.Aiden2', '5012345');
|
||||||
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) {
|
} catch (error) {
|
||||||
console.error('Print Error:', error);
|
console.error('Print Error:', error);
|
||||||
res.status(500).json({ message: 'Print Error', error: error.message });
|
res.status(500).json({ message: 'Print Error', error: error.message });
|
||||||
|
55
mccs-it/thermal/modules/Thermal.js
Normal file
55
mccs-it/thermal/modules/Thermal.js
Normal 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');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user