?
This commit is contained in:
parent
cfddee5a95
commit
facfff174f
@ -7,10 +7,17 @@ const port = 3000;
|
|||||||
// Middleware to serve static files (HTML)
|
// Middleware to serve static files (HTML)
|
||||||
app.use(express.static('public'));
|
app.use(express.static('public'));
|
||||||
|
|
||||||
|
// Middleware to parse JSON request bodies
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
// Test print function
|
// Test print function
|
||||||
app.post('/test-print', async (req, res) => {
|
app.post('/test-print', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
Thermal.generateFile('Grant.Aiden2', '5012345', req, res);
|
const { username, studentId } = req.body;
|
||||||
|
if (!username || !studentId) {
|
||||||
|
return res.status(400).json({ message: 'Username and Student ID are required.' });
|
||||||
|
}
|
||||||
|
await Thermal.generateFile(username, studentId, req, res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Print Error:', error);
|
console.error('Print Error:', error);
|
||||||
res.status(500).json({ message: 'Print Error', error: error.message });
|
res.status(500).json({ message: 'Print Error', error: error.message });
|
||||||
|
@ -3,31 +3,49 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Test Print</title>
|
<title>Thermal Printer Test</title>
|
||||||
<style>
|
|
||||||
#pdfViewer {
|
|
||||||
width: 100%;
|
|
||||||
height: 600px; /* Adjust as needed */
|
|
||||||
border: none;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Thermal Printer Test</h1>
|
<h1>Thermal Printer Test</h1>
|
||||||
<button id="testPrintBtn">Test Print</button>
|
<form id="printForm">
|
||||||
<iframe id="pdfViewer" style="display:none;"></iframe>
|
<label for="username">Username:</label>
|
||||||
|
<input type="text" id="username" name="username" required><br><br>
|
||||||
|
|
||||||
|
<label for="studentId">Student ID:</label>
|
||||||
|
<input type="text" id="studentId" name="studentId" required><br><br>
|
||||||
|
|
||||||
|
<button type="submit">Test Print</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.getElementById('testPrintBtn').addEventListener('click', () => {
|
document.getElementById('printForm').addEventListener('submit', (event) => {
|
||||||
fetch('/test-print', { method: 'POST' })
|
event.preventDefault(); // Prevent the form from submitting normally
|
||||||
.then(response => response.blob())
|
|
||||||
.then(blob => {
|
// Get the form data
|
||||||
const url = URL.createObjectURL(blob);
|
const formData = new FormData(event.target);
|
||||||
const pdfViewer = document.getElementById('pdfViewer');
|
const username = formData.get('username');
|
||||||
pdfViewer.src = url;
|
const studentId = formData.get('studentId');
|
||||||
pdfViewer.style.display = 'block'; // Show the iframe
|
|
||||||
})
|
// Send a POST request with the form data to the server
|
||||||
.catch(error => alert('Error: ' + error));
|
fetch('/test-print', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
username: username,
|
||||||
|
studentId: studentId,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
return response.text(); // or .json() if your server returns JSON
|
||||||
|
} else {
|
||||||
|
throw new Error('Failed to send print request');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(message => alert(message))
|
||||||
|
.catch(error => alert('Error: ' + error));
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
Reference in New Issue
Block a user