This commit is contained in:
Skylar Grant 2024-09-12 15:40:41 -04:00
parent 5f1e3b1215
commit 7cc0c363ff
2 changed files with 47 additions and 30 deletions

View File

@ -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,7 +63,19 @@ app.post('/print-mcc', async (req, res) => {
const infoString = Thermal.generateInfo.maineCC();
// Generate the PDF, saved to disk
Thermal.generatePdf(infoString).then(result => {
Thermal.generatePdf(infoString).catch(e => {
console.error(e);
res.status(500).json({ message: 'Error writing PDF file', error: e.message });
});
} catch (e) {
console.error('Print Error:', e);
res.status(500).json({ message: 'Print Error', error: e.message });
}
});
// 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.');
@ -87,10 +83,6 @@ app.post('/print-mcc', async (req, res) => {
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(e);
res.status(500).json({ message: 'Error writing PDF file', error: e.message });
});
} catch (e) {
console.error('Print Error:', e);
res.status(500).json({ message: 'Print Error', error: e.message });

View File

@ -34,7 +34,7 @@
<button type="submit"
style="background-color: #1F3C70;"
class="w-full text-white py-2 px-4 rounded-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50">
Print Credentials
Generate Credentials
</button>
</div>
</form>
@ -57,7 +57,7 @@
<button type="submit"
id="printTempPw"
class="w-full bg-gray-500 text-white py-2 px-4 rounded-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50 mt-2">
Print Temp Password
Generate Temp Password
</button>
</div>
</div>
@ -67,7 +67,7 @@
<button
id="print-mcc"
class="w-full bg-indigo-700 text-white py-2 px-4 rounded-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50 mt-2">
Print MaineCC Contact
Generate MaineCC Contact
</button>
</div>
@ -84,6 +84,12 @@
<div id="pdfViewer" class="border border-gray-300 p-4 rounded-md">
<!-- PDF content will be loaded here -->
</div>
<button
id="print"
class="w-full bg-indigo-700 text-white py-2 px-4 rounded-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50 mt-2">
Print Information
</button>
</div>
@ -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');