Rewrite testing

This commit is contained in:
Skylar Grant 2024-09-12 13:39:49 -04:00
parent 0c39826505
commit 73db7c0e65
2 changed files with 171 additions and 231 deletions

View File

@ -17,7 +17,22 @@ app.post('/print-credentials', async (req, res) => {
if (!username || !studentId) { if (!username || !studentId) {
return res.status(400).json({ message: 'Username and Student ID are required.' }); return res.status(400).json({ message: 'Username and Student ID are required.' });
} }
await Thermal.printCredentials(username, studentId, req, res); // Generate the information string
const infoString = Thermal.generateInfo.credentials(username, studentId);
// Generate the PDF, saved to disk
Thermal.generatePdf(infoString).then(res => {
// Print the document
Thermal.print().then(res => {
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(e);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
} catch (error) { } catch (error) {
console.error('Print Error:', error); console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message }); res.status(500).json({ message: 'Print Error', error: error.message });
@ -27,11 +42,30 @@ app.post('/print-credentials', async (req, res) => {
// Print temp password // Print temp password
app.post('/print-temp-pw', async (req, res) => { app.post('/print-temp-pw', async (req, res) => {
try { try {
// Grab the password from the request body
const { password } = req.body; const { password } = req.body;
// Check that the password is actually there
if (!password) { if (!password) {
return res.status(400).json({ message: 'Password is required.' }); return res.status(400).json({ message: 'Password is required.' });
} }
await Thermal.printTempPassword(password, req, res);
// Generate the information string
const infoString = Thermal.generateInfo.tempPassword(password);
// Generate the PDF, saved to disk
Thermal.generatePdf(infoString).then(res => {
// Print the document
Thermal.print().then(res => {
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(e);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
} catch (error) { } catch (error) {
console.error('Print Error:', error); console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message }); res.status(500).json({ message: 'Print Error', error: error.message });
@ -41,7 +75,22 @@ app.post('/print-temp-pw', async (req, res) => {
// Print Student MaineCC Info // Print Student MaineCC Info
app.post('/print-mcc', async (req, res) => { app.post('/print-mcc', async (req, res) => {
try { try {
await Thermal.printStudentMaineCC(req, res); // Generate the information string
const infoString = Thermal.generateInfo.maineCC();
// Generate the PDF, saved to disk
Thermal.generatePdf(infoString).then(res => {
// Print the document
Thermal.print().then(res => {
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(e);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
} catch (error) { } catch (error) {
console.error('Print Error:', error); console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message }); res.status(500).json({ message: 'Print Error', error: error.message });

View File

@ -19,7 +19,8 @@ const mccContractor = {
}; };
module.exports = { module.exports = {
printCredentials(username, studentId, req, res) { generateInfo: {
credentials(username, studentId) {
// Create the output text // Create the output text
const infoArray = [ const infoArray = [
`Username: ${username}`, `Username: ${username}`,
@ -32,69 +33,9 @@ module.exports = {
'Note: the MyKV Portal uses your username to sign in, not email.', 'Note: the MyKV Portal uses your username to sign in, not email.',
receiptFooter receiptFooter
]; ];
const infoString = infoArray.join('\n'); return 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);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
}, },
printStudentMaineCC(req, res) { maineCC() {
// Create the output text // Create the output text
const infoArray = [ const infoArray = [
'Kennebec Valley Community College is migrating email domains from @kvcc.me.edu to @mainecc.edu.\n', 'Kennebec Valley Community College is migrating email domains from @kvcc.me.edu to @mainecc.edu.\n',
@ -104,69 +45,9 @@ module.exports = {
`${mccContractor.site}\n`, `${mccContractor.site}\n`,
receiptFooter receiptFooter
]; ];
const infoString = infoArray.join('\n'); return 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);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
}, },
printTempPassword(password, req, res) { tempPassword(password) {
// Create the output text // Create the output text
const infoArray = [ 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', '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',
@ -175,8 +56,11 @@ module.exports = {
`${password}\n`, `${password}\n`,
receiptFooter receiptFooter
]; ];
const infoString = infoArray.join('\n'); return infoArray.join('\n');
}
},
generatePdf(infoString) {
return new Promise((resolve, reject) => {
// Define the path for the PDF file // Define the path for the PDF file
const pdfFilePath = path.join(__dirname, 'output.pdf'); const pdfFilePath = path.join(__dirname, 'output.pdf');
@ -197,6 +81,7 @@ module.exports = {
valign: 'top', valign: 'top',
}); });
// Move down below the Logo/Banner image
doc.moveDown(); doc.moveDown();
doc.moveDown(); doc.moveDown();
doc.moveDown(); doc.moveDown();
@ -204,6 +89,8 @@ module.exports = {
doc.moveDown(); doc.moveDown();
doc.moveDown(); doc.moveDown();
doc.moveDown(); doc.moveDown();
// Write the text from infoString
doc.fontSize(12).text(infoString, { doc.fontSize(12).text(infoString, {
align: 'left', align: 'left',
}); });
@ -212,29 +99,33 @@ module.exports = {
doc.end(); doc.end();
writeStream.on('finish', () => { writeStream.on('finish', () => {
// PDF file has been saved, now print it using lp command resolve();
});
// Handle any errors during file writing
writeStream.on('error', (err) => {
reject(err);
});
})
},
print() {
return new Promise((resolve, reject) => {
// Start the print command
const printer = spawn('lp', ['-d', 'ITThermal', pdfFilePath]); const printer = spawn('lp', ['-d', 'ITThermal', pdfFilePath]);
// Handle error if the printing process fails // Handle error if the printing process fails
printer.on('error', (err) => { printer.on('error', (err) => {
console.error('Failed to start printing process:', err); reject(err);
res.status(500).json({ message: 'Failed to start printing process', error: err.message });
}); });
// Handle the process exit event // Handle the process exit event
printer.on('close', (code) => { printer.on('close', (code) => {
if (code === 0) { if (code === 0) {
res.send('PDF sent to printer successfully.'); resolve()
} else { } else {
res.status(500).json({ message: 'Failed to print PDF' }); reject("lp process exited with non-zero result.");
} }
}); });
}); })
// 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 });
});
} }
} }