This commit is contained in:
Skylar Grant 2024-09-09 13:03:23 -04:00
parent 1667630c44
commit ffd595f590

View File

@ -3,53 +3,66 @@ const { spawn } = require('child_process');
module.exports = { module.exports = {
generateFile(username, studentId) { generateFile(username, studentId) {
// Create the output text // Create the output text
const infoArray = [ const infoArray = [
'Username:', 'Username:',
`${username}`, `${username}`,
'', '',
'Email:', 'Email:',
`${username}@kvcc.me.edu`, `${username}@kvcc.me.edu`,
'', '',
'Student ID:', 'Student ID:',
`${studentId}`, `${studentId}`,
'', '',
'Note: When signing into the MyKV Portal, use your Username not your Email.', 'Note: When signing into the MyKV Portal, use your Username not your Email.',
'For further assistance contact ithelp@mainecc.edu' 'For further assistance contact ithelp@mainecc.edu'
]; ];
const infoString = infoArray.join('\n'); const infoString = infoArray.join('\n');
// Create a new PDF document // Create a new PDF document in-memory
const doc = new PDFDocument(); const doc = new PDFDocument({
size: [204, 1000],
// Create a print process (e.g., using `lp` to print the PDF directly) margin: 0
const printer = spawn('lp', ['-d', 'ITThermal']); // Replace 'printer_name' with your actual printer });
// Pipe the PDF document output to the printing process // Store the PDF in a buffer
doc.pipe(printer.stdin); let buffers = [];
doc.on('data', buffers.push.bind(buffers));
// Add image and text to the PDF doc.on('end', () => {
doc.image('./public/itslogo.jpg', { // Combine all the chunks into a single buffer
fit: [576, 261], const pdfData = Buffer.concat(buffers);
align: 'center',
valign: 'top' // Set the response headers to serve the PDF file
}); res.writeHead(200, {
'Content-Type': 'application/pdf',
doc.moveDown(); 'Content-Disposition': 'inline; filename="output.pdf"', // Or 'attachment' for download
doc.fontSize(16).text(infoString, { 'Content-Length': pdfData.length,
align: 'left' });
});
// Send the PDF file as the response
// Finalize the PDF and close the stream res.end(pdfData);
doc.end(); });
// Listen for the print process to finish // Add content (image, text) to the PDF
printer.on('close', (code) => { doc.image('./public/itslogo.jpg', {
if (code === 0) { fit: [576, 261],
console.log('Printed successfully'); align: 'center',
} else { valign: 'top',
console.error('Failed to print'); });
}
}); doc.moveDown();
} doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.moveDown();
doc.fontSize(16).text(infoString, {
align: 'left',
});
// Finalize the PDF
doc.end();
},
generatePdf() {
}
} }