diff --git a/mccs-it/thermal/app.js b/mccs-it/thermal/app.js index a90cce8..c11e0b4 100644 --- a/mccs-it/thermal/app.js +++ b/mccs-it/thermal/app.js @@ -24,6 +24,20 @@ app.post('/print-credentials', async (req, res) => { } }); +// Print temp password +app.post('/print-temp-pw', async (req, res) => { + try { + const { password } = req.body; + if (!password) { + return res.status(400).json({ message: 'Password is required.' }); + } + await Thermal.printCredentials(password, req, res); + } catch (error) { + console.error('Print Error:', error); + res.status(500).json({ message: 'Print Error', error: error.message }); + } +}); + // Print Student MaineCC Info app.post('/print-mcc', async (req, res) => { try { diff --git a/mccs-it/thermal/modules/Thermal.js b/mccs-it/thermal/modules/Thermal.js index e273827..7fcd57d 100644 --- a/mccs-it/thermal/modules/Thermal.js +++ b/mccs-it/thermal/modules/Thermal.js @@ -160,6 +160,77 @@ module.exports = { }); }); + // Handle any errors during file writing + writeStream.on('error', (err) => { + console.error('Error writing PDF file:', err); + res.status(500).json({ message: 'Error writing PDF file', error: err.message }); + }); + }, + printTempPassword(password, req, res) { + // Create the output text + const infoArray = [ + 'We have changed your password to a temporary password, shown below. Next time you log into Microsoft you will be prompted to change your password.\n', + `Password requirements:\n8+ characters\n1+ Uppercase\n1+ Lowercase\n1+ Special (!/_=.,?)\n`, + `Temporary Password:` + `${password}\n`, + receiptFooter + ]; + const infoString = infoArray.join('\n'); + + // Define the path for the PDF file + const pdfFilePath = path.join(__dirname, 'output.pdf'); + + // Create a new PDF document + const doc = new PDFDocument({ + size: [204, 400], + margin: 5 + }); + + // Create a writable stream to save the PDF to a file + const writeStream = fs.createWriteStream(pdfFilePath); + doc.pipe(writeStream); + + // Add content (image, text) to the PDF + doc.image('./public/itslogo.jpg', { + fit: [196, 100], + align: 'center', + valign: 'top', + }); + + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.fontSize(12).text(infoString, { + align: 'left', + }); + + // Finalize the PDF + doc.end(); + + writeStream.on('finish', () => { + // PDF file has been saved, now print it using lp command + const printer = spawn('lp', ['-d', 'ITThermal', pdfFilePath]); + + // Handle error if the printing process fails + printer.on('error', (err) => { + console.error('Failed to start printing process:', err); + res.status(500).json({ message: 'Failed to start printing process', error: err.message }); + }); + + // Handle the process exit event + printer.on('close', (code) => { + if (code === 0) { + res.send('PDF sent to printer successfully.'); + } else { + res.status(500).json({ message: 'Failed to print PDF' }); + } + }); + }); + // Handle any errors during file writing writeStream.on('error', (err) => { console.error('Error writing PDF file:', err); diff --git a/mccs-it/thermal/public/index.html b/mccs-it/thermal/public/index.html index 17c7858..542e60b 100644 --- a/mccs-it/thermal/public/index.html +++ b/mccs-it/thermal/public/index.html @@ -43,8 +43,25 @@ + +
+