From ffd595f5907389abee7f453716881699a416daca Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Mon, 9 Sep 2024 13:03:23 -0400 Subject: [PATCH] . --- mccs-it/thermal/modules/Thermal.js | 111 ++++++++++++++++------------- 1 file changed, 62 insertions(+), 49 deletions(-) diff --git a/mccs-it/thermal/modules/Thermal.js b/mccs-it/thermal/modules/Thermal.js index 3567838..4d456f2 100644 --- a/mccs-it/thermal/modules/Thermal.js +++ b/mccs-it/thermal/modules/Thermal.js @@ -3,53 +3,66 @@ const { spawn } = require('child_process'); module.exports = { - generateFile(username, studentId) { - // 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 - const doc = new PDFDocument(); - - // Create a print process (e.g., using `lp` to print the PDF directly) - const printer = spawn('lp', ['-d', 'ITThermal']); // Replace 'printer_name' with your actual printer - - // Pipe the PDF document output to the printing process - doc.pipe(printer.stdin); - - // Add image and text to the PDF - doc.image('./public/itslogo.jpg', { - fit: [576, 261], - align: 'center', - valign: 'top' - }); - - doc.moveDown(); - doc.fontSize(16).text(infoString, { - align: 'left' - }); - - // Finalize the PDF and close the stream - doc.end(); - - // Listen for the print process to finish - printer.on('close', (code) => { - if (code === 0) { - console.log('Printed successfully'); - } else { - console.error('Failed to print'); - } - }); - } + generateFile(username, studentId) { + // 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, 1000], + margin: 0 + }); + + // 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); + }); + + // Add content (image, text) to the PDF + doc.image('./public/itslogo.jpg', { + fit: [576, 261], + align: 'center', + valign: 'top', + }); + + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.moveDown(); + doc.fontSize(16).text(infoString, { + align: 'left', + }); + + // Finalize the PDF + doc.end(); + }, + generatePdf() { + + } } \ No newline at end of file