.
This commit is contained in:
parent
1667630c44
commit
ffd595f590
@ -19,37 +19,50 @@ 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
|
// Create a new PDF document in-memory
|
||||||
const doc = new PDFDocument();
|
const doc = new PDFDocument({
|
||||||
|
size: [204, 1000],
|
||||||
|
margin: 0
|
||||||
|
});
|
||||||
|
|
||||||
// Create a print process (e.g., using `lp` to print the PDF directly)
|
// Store the PDF in a buffer
|
||||||
const printer = spawn('lp', ['-d', 'ITThermal']); // Replace 'printer_name' with your actual printer
|
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);
|
||||||
|
|
||||||
// Pipe the PDF document output to the printing process
|
// Set the response headers to serve the PDF file
|
||||||
doc.pipe(printer.stdin);
|
res.writeHead(200, {
|
||||||
|
'Content-Type': 'application/pdf',
|
||||||
|
'Content-Disposition': 'inline; filename="output.pdf"', // Or 'attachment' for download
|
||||||
|
'Content-Length': pdfData.length,
|
||||||
|
});
|
||||||
|
|
||||||
// Add image and text to the PDF
|
// Send the PDF file as the response
|
||||||
|
res.end(pdfData);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add content (image, text) to the PDF
|
||||||
doc.image('./public/itslogo.jpg', {
|
doc.image('./public/itslogo.jpg', {
|
||||||
fit: [576, 261],
|
fit: [576, 261],
|
||||||
align: 'center',
|
align: 'center',
|
||||||
valign: 'top'
|
valign: 'top',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
doc.moveDown();
|
||||||
|
doc.moveDown();
|
||||||
|
doc.moveDown();
|
||||||
|
doc.moveDown();
|
||||||
doc.moveDown();
|
doc.moveDown();
|
||||||
doc.fontSize(16).text(infoString, {
|
doc.fontSize(16).text(infoString, {
|
||||||
align: 'left'
|
align: 'left',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Finalize the PDF and close the stream
|
// Finalize the PDF
|
||||||
doc.end();
|
doc.end();
|
||||||
|
},
|
||||||
|
generatePdf() {
|
||||||
|
|
||||||
// Listen for the print process to finish
|
|
||||||
printer.on('close', (code) => {
|
|
||||||
if (code === 0) {
|
|
||||||
console.log('Printed successfully');
|
|
||||||
} else {
|
|
||||||
console.error('Failed to print');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user