From 7cc0c363ff6320ae84f8411b73c33a4d8e15cc7e Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Thu, 12 Sep 2024 15:40:41 -0400 Subject: [PATCH] ? --- mccs-it/thermal/app.js | 46 +++++++++++++------------------ mccs-it/thermal/public/index.html | 31 +++++++++++++++++++-- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/mccs-it/thermal/app.js b/mccs-it/thermal/app.js index 205e952..56b7b5e 100644 --- a/mccs-it/thermal/app.js +++ b/mccs-it/thermal/app.js @@ -21,15 +21,7 @@ app.post('/print-credentials', async (req, res) => { const infoString = Thermal.generateInfo.credentials(username, studentId); // Generate the PDF, saved to disk - Thermal.generatePdf(infoString).then(result => { - // Print the document - Thermal.print().then(result => { - res.send('PDF sent to printer successfully.'); - }).catch(e => { - console.error('Failed to start printing process:', e); - res.status(500).json({ message: 'Failed to start printing process', error: e.message }); - }); - }).catch(e => { + Thermal.generatePdf(infoString).catch(e => { console.error(e); res.status(500).json({ message: 'Error writing PDF file', error: e.message }); }); @@ -54,15 +46,7 @@ app.post('/print-temp-pw', async (req, res) => { const infoString = Thermal.generateInfo.tempPassword(password); // Generate the PDF, saved to disk - Thermal.generatePdf(infoString).then(result => { - // Print the document - Thermal.print().then(result => { - res.send('PDF sent to printer successfully.'); - }).catch(e => { - console.error('Failed to start printing process:', err); - res.status(500).json({ message: 'Failed to start printing process', error: err.message }); - }); - }).catch(e => { + Thermal.generatePdf(infoString).catch(e => { console.error(e); res.status(500).json({ message: 'Error writing PDF file', error: err.message }); }); @@ -79,15 +63,7 @@ app.post('/print-mcc', async (req, res) => { const infoString = Thermal.generateInfo.maineCC(); // Generate the PDF, saved to disk - Thermal.generatePdf(infoString).then(result => { - // Print the document - Thermal.print().then(result => { - res.send('PDF sent to printer successfully.'); - }).catch(e => { - console.error('Failed to start printing process:', err); - res.status(500).json({ message: 'Failed to start printing process', error: err.message }); - }); - }).catch(e => { + Thermal.generatePdf(infoString).catch(e => { console.error(e); res.status(500).json({ message: 'Error writing PDF file', error: e.message }); }); @@ -97,6 +73,22 @@ app.post('/print-mcc', async (req, res) => { } }); +// Print Student MaineCC Info +app.post('/print', async (req, res) => { + try { + // Print the document + Thermal.print().then(result => { + res.send('PDF sent to printer successfully.'); + }).catch(e => { + console.error('Failed to start printing process:', err); + res.status(500).json({ message: 'Failed to start printing process', error: err.message }); + }); + } catch (e) { + console.error('Print Error:', e); + res.status(500).json({ message: 'Print Error', error: e.message }); + } +}); + // Start the server app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); diff --git a/mccs-it/thermal/public/index.html b/mccs-it/thermal/public/index.html index c9b2136..0f20d41 100644 --- a/mccs-it/thermal/public/index.html +++ b/mccs-it/thermal/public/index.html @@ -34,7 +34,7 @@ @@ -57,7 +57,7 @@ @@ -67,7 +67,7 @@ @@ -84,6 +84,12 @@
+ + @@ -125,6 +131,23 @@ fetch('/print-mcc', { method: 'POST' }) + .then(response => { + if (response.ok) { + loadPDF(); + return response.text(); // or .json() if your server returns JSON + } else { + throw new Error('Failed to send print request'); + } + }) + .then(message => alert(message)) + .catch(error => alert('Error: ' + error)); + }); + + document.getElementById('print').addEventListener('click', (event) => { + // Send a POST request with the form data to the server + fetch('/print', { + method: 'POST' + }) .then(response => { if (response.ok) { return response.text(); // or .json() if your server returns JSON @@ -157,6 +180,7 @@ }) .then(response => { if (response.ok) { + loadPDF(); return response.text(); // or .json() if your server returns JSON } else { throw new Error('Failed to send print request'); @@ -181,6 +205,7 @@ }) .then(response => { if (response.ok) { + loadPDF(); return response.text(); // or .json() if your server returns JSON } else { throw new Error('Failed to send print request');