This commit is contained in:
Skylar Grant 2024-09-10 15:42:42 -04:00
parent c071537689
commit 4c8fb156ee
3 changed files with 114 additions and 4 deletions

View File

@ -10,8 +10,8 @@ app.use(express.static('public'));
// Middleware to parse JSON request bodies // Middleware to parse JSON request bodies
app.use(express.json()); app.use(express.json());
// Test print function // Print credentials
app.post('/print', async (req, res) => { app.post('/print-credentials', async (req, res) => {
try { try {
const { username, studentId } = req.body; const { username, studentId } = req.body;
if (!username || !studentId) { 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 // Start the server
app.listen(port, () => { app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`); console.log(`Server running at http://localhost:${port}`);

View File

@ -3,6 +3,13 @@ const fs = require('fs');
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const path = require('path'); const path = require('path');
const mccContractor = {
name: "Contracting Company",
email: "helpme@contractor.com",
phone: "1 (800) 234-5678",
site: "www.google.com"
};
module.exports = { module.exports = {
printCredentials(username, studentId, req, res) { printCredentials(username, studentId, req, res) {
// Create the output text // 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 // Handle any errors during file writing
writeStream.on('error', (err) => { writeStream.on('error', (err) => {
console.error('Error writing PDF file:', err); console.error('Error writing PDF file:', err);

View File

@ -34,13 +34,34 @@
<button type="submit" <button type="submit"
style="background-color: #1F3C70;" 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"> 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 Print Credentials
</button>
<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">
Print MaineCC Contact
</button> </button>
</div> </div>
</form> </form>
</div> </div>
<script> <script>
document.getElementById('print-mcc').addEventListener('click', (event) => {
// Send a POST request with the form data to the server
fetch('/print-mcc', {
method: 'POST'
})
.then(response => {
if (response.ok) {
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('printForm').addEventListener('submit', (event) => { document.getElementById('printForm').addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting normally event.preventDefault(); // Prevent the form from submitting normally
@ -50,7 +71,7 @@
const studentId = formData.get('studentId'); const studentId = formData.get('studentId');
// Send a POST request with the form data to the server // Send a POST request with the form data to the server
fetch('/print', { fetch('/print-credentials', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',