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

92 lines
2.2 KiB
JavaScript

const PDFDocument = require('pdfkit');
const { spawn } = require('child_process');
module.exports = {
generateFile(username, studentId, req, res) {
// Create the output text
const infoArray = [
'Username:',
`${username}`,
'',
'Email:',
`${username}@kvcc.me.edu`,
'',
'Student ID:',
`${studentId}`,
'',
'Note: When signing into the MyKV Portal, use your Username not your Email.',
'',
'For further assistance contact ithelp@mainecc.edu'
];
const infoString = infoArray.join('\n');
// Create a new PDF document in-memory
const doc = new PDFDocument({
size: [204, 400],
margin: 5
});
// Store the PDF in a buffer
let buffers = [];
doc.on('data', buffers.push.bind(buffers));
doc.on('end', () => {
// Combine all the chunks into a single buffer
const pdfData = Buffer.concat(buffers);
// Set the response headers to serve the PDF file
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': 'inline; filename="output.pdf"', // Or 'attachment' for download
'Content-Length': pdfData.length,
});
// Send the PDF file as the response
res.end(pdfData);
// Print the PDF using lp command
const printer = spawn('lp', ['-d', 'ITThermal']); // Replace 'printer_name' 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).send('Failed to start printing process');
});
// Pipe the PDF data to the printer
printer.stdin.write(pdfData);
// Handle the process exit event
printer.on('close', (code) => {
if (code === 0) {
res.send('PDF sent to printer successfully.');
} else {
res.status(500).send('Failed to print PDF');
}
});
});
// 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();
},
generatePdf() {
}
}