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 } \n or ${ 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-10 17:29:16 +00:00
printCredentials ( username , studentId , req , res ) {
2024-09-09 17:03:23 +00:00
// Create the output text
const infoArray = [
2024-09-10 18:47:34 +00:00
` Username: ${ username } ` ,
2024-09-09 17:03:23 +00:00
'' ,
'Email:' ,
` ${ username } @kvcc.me.edu ` ,
'' ,
2024-09-10 18:47:34 +00:00
` Student ID: ${ studentId } ` ,
2024-09-09 17:03:23 +00:00
'' ,
2024-09-10 18:51:51 +00:00
'Note: the MyKV Portal uses your username to sign in, not email.' ,
2024-09-10 19:53:51 +00:00
receiptFooter
2024-09-09 17:03:23 +00:00
] ;
const infoString = infoArray . join ( '\n' ) ;
2024-09-09 17:33:26 +00:00
// Define the path for the PDF file
const pdfFilePath = path . join ( _ _dirname , 'output.pdf' ) ;
// Create a new PDF document
2024-09-09 17:03:23 +00:00
const doc = new PDFDocument ( {
2024-09-09 17:41:14 +00:00
size : [ 204 , 400 ] ,
2024-09-09 17:15:30 +00:00
margin : 5
2024-09-09 17:03:23 +00:00
} ) ;
2024-09-09 17:33:26 +00:00
// Create a writable stream to save the PDF to a file
const writeStream = fs . createWriteStream ( pdfFilePath ) ;
doc . pipe ( writeStream ) ;
2024-09-09 17:03:23 +00:00
// Add content (image, text) to the PDF
2024-09-09 20:17:52 +00:00
doc . image ( './public/itslogo.jpg' , {
fit : [ 196 , 100 ] ,
align : 'center' ,
valign : 'top' ,
} ) ;
2024-09-09 17:03:23 +00:00
2024-09-09 20:17:52 +00:00
doc . moveDown ( ) ;
doc . moveDown ( ) ;
doc . moveDown ( ) ;
doc . moveDown ( ) ;
doc . moveDown ( ) ;
doc . moveDown ( ) ;
doc . moveDown ( ) ;
2024-09-09 17:16:05 +00:00
doc . fontSize ( 12 ) . text ( infoString , {
2024-09-10 19:42:42 +00:00
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 ) {
// Create the output text
const infoArray = [
'Kennebec Valley Community College is migrating email domains from @kvcc.me.edu to @mainecc.edu.\n' ,
2024-09-10 19:48:42 +00:00
` For support with this transition, please contact: \n ${ mccContractor . name } ` ,
` ${ mccContractor . phone } ` ,
` ${ mccContractor . email } ` ,
` ${ mccContractor . site } \n ` ,
2024-09-10 19:53:51 +00:00
receiptFooter
2024-09-10 19:42:42 +00:00
] ;
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 ( ) ;
2024-09-12 16:24:02 +00:00
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 ) {
// 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: \n 8+ characters \n 1+ Uppercase \n 1+ Lowercase \n 1+ 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 ( ) ;
2024-09-10 19:42:42 +00:00
doc . moveDown ( ) ;
doc . moveDown ( ) ;
doc . fontSize ( 12 ) . text ( infoString , {
2024-09-09 17:03:23 +00:00
align : 'left' ,
} ) ;
// Finalize the PDF
doc . end ( ) ;
2024-09-09 17:33:26 +00:00
writeStream . on ( 'finish' , ( ) => {
// PDF file has been saved, now print it using lp 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 ) => {
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 ) {
2024-09-09 17:41:14 +00:00
res . send ( 'PDF sent to printer successfully.' ) ;
2024-09-09 17:33:26 +00:00
} 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 } ) ;
} ) ;
2024-09-09 17:03:23 +00:00
}
2024-09-09 15:38:53 +00:00
}