This commit is contained in:
Skylar Grant 2024-09-09 13:33:26 -04:00
parent 4ddbab45ea
commit 265c962c00

View File

@ -1,6 +1,7 @@
const PDFDocument = require('pdfkit'); const PDFDocument = require('pdfkit');
const fs = require('fs');
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const path = require('path');
module.exports = { module.exports = {
generateFile(username, studentId, req, res) { generateFile(username, studentId, req, res) {
@ -20,41 +21,19 @@ module.exports = {
'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 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({ const doc = new PDFDocument({
size: [204, 400], size: [204, 400],
margin: 5 margin: 5
}); });
// Store the PDF in a buffer // Create a writable stream to save the PDF to a file
let buffers = []; const writeStream = fs.createWriteStream(pdfFilePath);
doc.on('data', buffers.push.bind(buffers)); doc.pipe(writeStream);
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();
});
// Add content (image, text) to the PDF // Add content (image, text) to the PDF
doc.image('./public/itslogo.jpg', { doc.image('./public/itslogo.jpg', {
@ -76,8 +55,39 @@ module.exports = {
// Finalize the PDF // Finalize the PDF
doc.end(); 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 });
});
} }
} }