From 4c8fb156ee6f5f4657c0a6921839dfed8f9cd956 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Tue, 10 Sep 2024 15:42:42 -0400 Subject: [PATCH] ? --- mccs-it/thermal/app.js | 14 +++++- mccs-it/thermal/modules/Thermal.js | 79 ++++++++++++++++++++++++++++++ mccs-it/thermal/public/index.html | 25 +++++++++- 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/mccs-it/thermal/app.js b/mccs-it/thermal/app.js index e0d5cba..a90cce8 100644 --- a/mccs-it/thermal/app.js +++ b/mccs-it/thermal/app.js @@ -10,8 +10,8 @@ app.use(express.static('public')); // Middleware to parse JSON request bodies app.use(express.json()); -// Test print function -app.post('/print', async (req, res) => { +// Print credentials +app.post('/print-credentials', async (req, res) => { try { const { username, studentId } = req.body; if (!username || !studentId) { @@ -24,6 +24,16 @@ app.post('/print', async (req, res) => { } }); +// Print Student MaineCC Info +app.post('/print-mcc', async (req, res) => { + try { + await Thermal.printStudentMaineCC(req, res); + } 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/modules/Thermal.js b/mccs-it/thermal/modules/Thermal.js index 87bedf6..5fdb574 100644 --- a/mccs-it/thermal/modules/Thermal.js +++ b/mccs-it/thermal/modules/Thermal.js @@ -3,6 +3,13 @@ const fs = require('fs'); const { spawn } = require('child_process'); const path = require('path'); +const mccContractor = { + name: "Contracting Company", + email: "helpme@contractor.com", + phone: "1 (800) 234-5678", + site: "www.google.com" +}; + module.exports = { printCredentials(username, studentId, req, res) { // Create the output text @@ -73,6 +80,78 @@ 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 }); + }); + }, + printStudentMaineCC(req, res) { + // Create the output text + const infoArray = [ + 'Kennebec Valley Community College is migrating email domains from @kvcc.me.edu to @mainecc.edu.\n', + `For support with this transition, please contact ${mccContractor.name}:`, + `Phone: ${mccContractor.phone}`, + `Email: ${mccContractor.email}`, + `Website:\n${mccContractor.site}\n`, + 'For other technical support contact IT Support at ITHelp@MaineCC.edu or (207)453-5079' + ]; + 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]); // Replace 'ITThermal' with your actual printer name + + // 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 ecb4328..2ad504e 100644 --- a/mccs-it/thermal/public/index.html +++ b/mccs-it/thermal/public/index.html @@ -34,13 +34,34 @@ +