custom-scripts/mccs-it/thermal/modules/Thermal.js

134 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-09-09 15:38:53 +00:00
const PDFDocument = require('pdfkit');
2024-09-09 17:33:26 +00:00
const fs = require('fs');
2024-09-09 15:38:53 +00:00
const { spawn } = require('child_process');
2024-09-09 17:33:26 +00:00
const path = require('path');
2024-09-09 15:38:53 +00:00
2024-09-10 19:53:51 +00:00
const info = {
deptName: "IT Support",
supportEmail: "ITHelp@MaineCC.edu",
supportPhone: "(207) 453-5079"
}
2024-09-10 19:56:54 +00:00
const receiptFooter = `For other technical assistance, contact ${info.deptName}:\n${info.supportEmail}\nor ${info.supportPhone}.`;
2024-09-10 19:53:51 +00:00
2024-09-10 19:42:42 +00:00
const mccContractor = {
name: "Contracting Company",
email: "helpme@contractor.com",
phone: "1 (800) 234-5678",
site: "www.google.com"
};
2024-09-09 15:38:53 +00:00
module.exports = {
2024-09-12 17:39:49 +00:00
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',
'Temporary Password:',
`${password}\n`,
2024-09-12 18:24:15 +00:00
'Password requirements:\n8+ characters\n1+ Uppercase\n1+ Lowercase\n1+ Special (!/_=.,?)\n',
2024-09-12 17:39:49 +00:00
receiptFooter
];
return infoArray.join('\n');
}
},
generatePdf(infoString) {
return new Promise((resolve, reject) => {
// Define the path for the PDF file
2024-09-12 19:32:29 +00:00
const pdfFilePath = path.join(__dirname, '../public/output.pdf');
2024-09-10 19:42:42 +00:00
2024-09-12 17:39:49 +00:00
// Create a new PDF document
const doc = new PDFDocument({
size: [204, 400],
margin: 5
2024-09-10 19:42:42 +00:00
});
2024-09-12 17:39:49 +00:00
// 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',
2024-09-10 19:42:42 +00:00
});
2024-09-12 16:24:02 +00:00
2024-09-12 17:39:49 +00:00
// Move down below the Logo/Banner image
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
2024-09-12 18:24:15 +00:00
2024-09-12 17:39:49 +00:00
// Write the text from infoString
2024-09-12 18:24:15 +00:00
doc.fontSize(12)
doc.font('public/Outfit-Regular.ttf').text(infoString, {
2024-09-12 17:39:49 +00:00
align: 'left',
2024-09-12 16:24:02 +00:00
});
2024-09-12 17:39:49 +00:00
// Finalize the PDF
doc.end();
writeStream.on('finish', () => {
resolve();
});
// Handle any errors during file writing
writeStream.on('error', (err) => {
reject(err);
2024-09-12 16:24:02 +00:00
});
2024-09-12 17:39:49 +00:00
})
2024-09-12 16:24:02 +00:00
},
2024-09-12 17:39:49 +00:00
print() {
return new Promise((resolve, reject) => {
2024-09-12 17:43:46 +00:00
// Define the path for the PDF file
2024-09-12 19:32:29 +00:00
const pdfFilePath = path.join(__dirname, '../public/output.pdf');
2024-09-12 17:39:49 +00:00
// Start the print command
2024-09-10 19:45:43 +00:00
const printer = spawn('lp', ['-d', 'ITThermal', pdfFilePath]);
2024-09-09 17:33:26 +00:00
// Handle error if the printing process fails
printer.on('error', (err) => {
2024-09-12 17:39:49 +00:00
reject(err);
2024-09-09 17:33:26 +00:00
});
// Handle the process exit event
printer.on('close', (code) => {
if (code === 0) {
2024-09-12 17:39:49 +00:00
resolve()
2024-09-09 17:33:26 +00:00
} else {
2024-09-12 17:39:49 +00:00
reject("lp process exited with non-zero result.");
2024-09-09 17:33:26 +00:00
}
});
2024-09-12 17:39:49 +00:00
})
2024-09-09 17:03:23 +00:00
}
2024-09-09 15:38:53 +00:00
}