This commit is contained in:
Matthew Frost 2023-11-18 19:43:05 +01:00
parent 4988c740a5
commit 45a0274f5c
2 changed files with 66 additions and 0 deletions

View file

@ -63,6 +63,14 @@
<input type="submit" value="Create Label">
</form>
<h1>Create Generic Label</h1>
<form id="genericLabelForm" action="http://10.209.10.3:8080/create_generic_label" method="POST">
<label for="text">Text:</label>
<input type="text" id="text" name="text" required>
<br><br>
<input type="submit" value="Create Label">
</form>
<script>
document.getElementById("labelForm").addEventListener("submit", function(event) {
event.preventDefault();
@ -85,6 +93,28 @@
console.log(error);
});
});
document.getElementById("genericLabelForm").addEventListener("submit", function(event) {
event.preventDefault();
var form = event.target;
var formData = new FormData(form);
fetch(form.action, {
method: form.method,
body: formData
})
.then(function(response) {
if (response.ok) {
alert("Generic label created successfully!");
} else {
console.log(response);
alert("Failed to create generic label. Please try again.");
}
})
.catch(function(error) {
console.log(error);
});
});
</script>
</body>
</html>

36
main.go
View file

@ -76,6 +76,10 @@ func main() {
createDnhLabelHandler(w, r, ep)
})
http.HandleFunc("/create_generic_label", func(w http.ResponseWriter, r *http.Request) {
createGenericLabelHandler(w, r, ep)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
@ -127,4 +131,36 @@ func createDnhLabelHandler(w http.ResponseWriter, r *http.Request, ep *escpos.Es
ep.Cut()
ep.End()
w.WriteHeader(http.StatusOK)
}
func createGenericLabelHandler(w http.ResponseWriter, r *http.Request, ep *escpos.Escpos) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
text := r.FormValue("text")
// Check if name is blank
if text == "" {
http.Error(w, "Text cannot be blank", http.StatusBadRequest)
return
}
// Here you can add the logic for creating a label using the POST data
ep.Init()
ep.WriteRaw(pageMode())
ep.WriteRaw(pageDirection(1))
ep.WriteRaw(pagePrintArea(0, 0, int(math.Round((29.0/80.0)*512)), 850))
ep.WriteRaw(offsetY(30))
ep.SetFontSize(1, 1)
ep.Write("Technologia Incognita\n")
ep.WriteRaw(offsetY(35))
ep.SetFontSize(1, 2)
ep.Write(text)
ep.WriteRaw([]byte{12}) // FF: in Page mode, prints all the data in the print buffer collectively and switches from Page mode to Standard mode.
ep.Cut()
ep.End()
w.WriteHeader(http.StatusOK)
}