const PDFDocument = require('pdfkit'); const fs = require('fs'); const { spawn } = require('child_process'); const path = require('path'); const info = { deptName: "IT Support", supportEmail: "ITHelp@MaineCC.edu", supportPhone: "(207) 453-5079" } const receiptFooter = `For other technical assistance, contact ${info.deptName}:\n${info.supportEmail}\nor ${info.supportPhone}.`; const mccContractor = { name: "Contracting Company", email: "helpme@contractor.com", phone: "1 (800) 234-5678", site: "www.google.com" }; module.exports = { generateInfo: { credentials(username, studentId) { // Create the output text return [ { 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 }, ] }, maineCC() { // Create the output text return [ { 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 } ]; }, tempPassword(password) { // Create the output text return [ { 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 }, { font: 'public/RobotoMono-SemiBold.ttf', text: `${password}\n`, moveDownAfter: true }, { font: 'public/Outfit-Regular.ttf', text: 'Password requirements:\n8+ characters\n1+ Uppercase\n1+ Lowercase\n1+ Special (!/_=.,?)\n', moveDownAfter: true } ] } }, generatePdf(infoArray) { return new Promise((resolve, reject) => { // Define the path for the PDF file const pdfFilePath = path.join(__dirname, '../public/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) // doc.font('public/Outfit-Regular.ttf').text(infoString, { // align: 'left', // }); for (row of infoArray) { doc.font(row.font).text(row.text, { align: 'left', }); if (row.moveDownAfter) { doc.moveDown(); } } doc.font('public/Outfit-Regular.ttf').text(receiptFooter, { 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) => { // Define the path for the PDF file const pdfFilePath = path.join(__dirname, '../public/output.pdf'); // Start the print command const printer = spawn('lp', ['-d', 'ITThermal', pdfFilePath]); // Handle error if the printing process fails printer.on('error', (err) => { reject(err); }); // Handle the process exit event printer.on('close', (code) => { if (code === 0) { resolve() } else { reject("lp process exited with non-zero result."); } }); }) } }