From 265c962c00e75932ca66d999287a0e8c4fb69070 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Mon, 9 Sep 2024 13:33:26 -0400 Subject: [PATCH] . --- mccs-it/thermal/modules/Thermal.js | 76 +++++++++++++++++------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/mccs-it/thermal/modules/Thermal.js b/mccs-it/thermal/modules/Thermal.js index ffd9e2c..2dffd26 100644 --- a/mccs-it/thermal/modules/Thermal.js +++ b/mccs-it/thermal/modules/Thermal.js @@ -1,6 +1,7 @@ const PDFDocument = require('pdfkit'); +const fs = require('fs'); const { spawn } = require('child_process'); - +const path = require('path'); module.exports = { generateFile(username, studentId, req, res) { @@ -20,41 +21,19 @@ module.exports = { 'For further assistance contact ithelp@mainecc.edu' ]; const infoString = infoArray.join('\n'); - // Create a new PDF document in-memory + + // 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 }); - // 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); - }); - - // Pipe the PDF data to the printer - printer.stdin.write(pdfData); - printer.stdin.end(); - }); + // 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', { @@ -76,8 +55,39 @@ module.exports = { // Finalize the PDF doc.end(); - }, - generatePdf() { + 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) { + // Delete the file after printing to clean up + fs.unlink(pdfFilePath, (err) => { + if (err) { + console.error('Error deleting file:', err); + res.status(500).json({ message: 'Failed to clean up after printing', error: err.message }); + return; + } + 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 }); + }); } } \ No newline at end of file