Rewrite testing
This commit is contained in:
parent
0c39826505
commit
73db7c0e65
@ -12,43 +12,92 @@ app.use(express.json());
|
||||
|
||||
// Print credentials
|
||||
app.post('/print-credentials', async (req, res) => {
|
||||
try {
|
||||
const { username, studentId } = req.body;
|
||||
if (!username || !studentId) {
|
||||
return res.status(400).json({ message: 'Username and Student ID are required.' });
|
||||
}
|
||||
await Thermal.printCredentials(username, studentId, req, res);
|
||||
} catch (error) {
|
||||
console.error('Print Error:', error);
|
||||
res.status(500).json({ message: 'Print Error', error: error.message });
|
||||
}
|
||||
try {
|
||||
const { username, studentId } = req.body;
|
||||
if (!username || !studentId) {
|
||||
return res.status(400).json({ message: 'Username and Student ID are required.' });
|
||||
}
|
||||
// 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) {
|
||||
console.error('Print Error:', error);
|
||||
res.status(500).json({ message: 'Print Error', error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Print temp password
|
||||
app.post('/print-temp-pw', async (req, res) => {
|
||||
try {
|
||||
const { password } = req.body;
|
||||
if (!password) {
|
||||
return res.status(400).json({ message: 'Password is required.' });
|
||||
}
|
||||
await Thermal.printTempPassword(password, req, res);
|
||||
} catch (error) {
|
||||
console.error('Print Error:', error);
|
||||
res.status(500).json({ message: 'Print Error', error: error.message });
|
||||
}
|
||||
try {
|
||||
// Grab the password from the request body
|
||||
const { password } = req.body;
|
||||
|
||||
// Check that the password is actually there
|
||||
if (!password) {
|
||||
return res.status(400).json({ message: 'Password is required.' });
|
||||
}
|
||||
|
||||
// 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) {
|
||||
console.error('Print Error:', error);
|
||||
res.status(500).json({ message: 'Print Error', error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 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 });
|
||||
}
|
||||
try {
|
||||
// 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) {
|
||||
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}`);
|
||||
console.log(`Server running at http://localhost:${port}`);
|
||||
});
|
||||
|
@ -19,222 +19,113 @@ const mccContractor = {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
printCredentials(username, studentId, req, res) {
|
||||
// Create the output text
|
||||
const infoArray = [
|
||||
`Username: ${username}`,
|
||||
'',
|
||||
'Email:',
|
||||
`${username}@kvcc.me.edu`,
|
||||
'',
|
||||
`Student ID: ${studentId}`,
|
||||
'',
|
||||
'Note: the MyKV Portal uses your username to sign in, not email.',
|
||||
receiptFooter
|
||||
];
|
||||
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);
|
||||
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
|
||||
});
|
||||
generateInfo: {
|
||||
credentials(username, studentId) {
|
||||
// Create the output text
|
||||
const infoArray = [
|
||||
`Username: ${username}`,
|
||||
'',
|
||||
'Email:',
|
||||
`${username}@kvcc.me.edu`,
|
||||
'',
|
||||
`Student ID: ${studentId}`,
|
||||
'',
|
||||
'Note: the MyKV Portal uses your username to sign in, not email.',
|
||||
receiptFooter
|
||||
];
|
||||
return infoArray.join('\n');
|
||||
},
|
||||
maineCC() {
|
||||
// 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:\n${mccContractor.name}`,
|
||||
`${mccContractor.phone}`,
|
||||
`${mccContractor.email}`,
|
||||
`${mccContractor.site}\n`,
|
||||
receiptFooter
|
||||
];
|
||||
return infoArray.join('\n');
|
||||
},
|
||||
tempPassword(password) {
|
||||
// Create the output text
|
||||
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',
|
||||
'Password requirements:\n8+ characters\n1+ Uppercase\n1+ Lowercase\n1+ Special (!/_=.,?)\n',
|
||||
'Temporary Password:',
|
||||
`${password}\n`,
|
||||
receiptFooter
|
||||
];
|
||||
return infoArray.join('\n');
|
||||
}
|
||||
},
|
||||
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:\n${mccContractor.name}`,
|
||||
`${mccContractor.phone}`,
|
||||
`${mccContractor.email}`,
|
||||
`${mccContractor.site}\n`,
|
||||
receiptFooter
|
||||
];
|
||||
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
|
||||
generatePdf(infoString) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 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',
|
||||
});
|
||||
|
||||
// Move down below the Logo/Banner image
|
||||
doc.moveDown();
|
||||
doc.moveDown();
|
||||
doc.moveDown();
|
||||
doc.moveDown();
|
||||
doc.moveDown();
|
||||
doc.moveDown();
|
||||
doc.moveDown();
|
||||
|
||||
// Write the text from infoString
|
||||
doc.fontSize(12).text(infoString, {
|
||||
align: 'left',
|
||||
});
|
||||
|
||||
// Finalize the PDF
|
||||
doc.end();
|
||||
|
||||
writeStream.on('finish', () => {
|
||||
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]);
|
||||
|
||||
// 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 });
|
||||
reject(err);
|
||||
});
|
||||
|
||||
// Handle the process exit event
|
||||
printer.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
res.send('PDF sent to printer successfully.');
|
||||
resolve()
|
||||
} 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 });
|
||||
});
|
||||
},
|
||||
printTempPassword(password, req, res) {
|
||||
// Create the output text
|
||||
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',
|
||||
'Password requirements:\n8+ characters\n1+ Uppercase\n1+ Lowercase\n1+ Special (!/_=.,?)\n',
|
||||
'Temporary Password:',
|
||||
`${password}\n`,
|
||||
receiptFooter
|
||||
];
|
||||
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]);
|
||||
|
||||
// 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 });
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user