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

141 lines
4.6 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
2024-09-13 13:23:25 +00:00
return [
2024-09-13 13:47:06 +00:00
{ font: 'public/Outfit-Regular.ttf', text: `Username:`, moveDownAfter: false },
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${username}`, moveDownAfter: true },
{ font: 'public/Outfit-Regular.ttf', text: 'Email:', moveDownAfter: false },
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${username}@kvcc.me.edu`, moveDownAfter: true },
{ font: 'public/Outfit-Regular.ttf', text: `Student ID:`, moveDownAfter: false },
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${studentId}`, moveDownAfter: true },
{ font: 'public/Outfit-Regular.ttf', text: 'Note: the MyKV Portal uses your username to sign in, not email.', moveDownAfter: true },
2024-09-13 13:09:17 +00:00
]
2024-09-12 17:39:49 +00:00
},
maineCC() {
// Create the output text
2024-09-13 13:38:22 +00:00
return [
2024-09-13 13:47:06 +00:00
{ font: 'public/Outfit-Regular.ttf', text: 'Kennebec Valley Community College is migrating email domains from @kvcc.me.edu to @mainecc.edu.', moveDownAfter: true },
{ font: 'public/Outfit-Regular.ttf', text: `For support with this transition, please contact:`, moveDownAfter: true },
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${mccContractor.name}`, moveDownAfter: false },
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${mccContractor.phone}`, moveDownAfter: false },
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${mccContractor.email}`, moveDownAfter: false },
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${mccContractor.site}`, moveDownAfter: true }
2024-09-12 17:39:49 +00:00
];
},
tempPassword(password) {
// Create the output text
2024-09-13 13:38:22 +00:00
return [
2024-09-13 13:47:06 +00:00
{ font: 'public/Outfit-Regular.ttf', text: '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', moveDownAfter: true },
{ font: 'public/Outfit-Regular.ttf', text: 'Temporary Password:', moveDownAfter: false },
2024-09-13 14:05:07 +00:00
{ font: 'public/RobotoMono-SemiBold.ttf', text: `${password}\n`, moveDownAfter: true },
2024-09-13 13:47:06 +00:00
{ font: 'public/Outfit-Regular.ttf', text: 'Password requirements:\n8+ characters\n1+ Uppercase\n1+ Lowercase\n1+ Special (!/_=.,?)\n', moveDownAfter: true }
2024-09-13 13:38:22 +00:00
]
2024-09-12 17:39:49 +00:00
}
},
2024-09-13 13:09:17 +00:00
generatePdf(infoArray) {
2024-09-12 17:39:49 +00:00
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-13 13:21:52 +00:00
doc.fontSize(12)
2024-09-13 13:24:11 +00:00
// doc.font('public/Outfit-Regular.ttf').text(infoString, {
// align: 'left',
// });
2024-09-13 13:21:52 +00:00
2024-09-13 13:09:17 +00:00
for (row of infoArray) {
doc.font(row.font).text(row.text, {
align: 'left',
});
2024-09-13 13:47:06 +00:00
if (row.moveDownAfter) {
doc.moveDown();
}
2024-09-13 13:09:17 +00:00
}
2024-09-13 13:41:49 +00:00
doc.font('public/Outfit-Regular.ttf').text(receiptFooter, {
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
}