Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active January 12, 2023 02:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresf/6d219f21c66bfe87cd279a6044b7209a to your computer and use it in GitHub Desktop.
Save tresf/6d219f21c66bfe87cd279a6044b7209a to your computer and use it in GitHub Desktop.
QZ Tray PHP Hello World
<html><head><meta charset="UTF-8"></head>
<body>
<!-- USAGE: index.php?var1=some_value_to_print -->
<?php
if (isset($_GET["var1"])) {
echo '<!-- FROM PHP --><script>var var1 = "' . htmlspecialchars($_GET["var1"]) . '";</script>';
} else {?>
<form action="index.php" method="get">Some value: <input type="text" name="var1"><br>
<input type="submit" value="Print"></form></body></html>
<?php die();
}
?>
<!-- FOR QZ TRAY -->
<script type="text/javascript" src="https://rawgit.com/qzind/tray/2.0/js/dependencies/rsvp-3.1.0.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/qzind/tray/2.0/js/dependencies/sha-256.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/qzind/tray/2.0/js/qz-tray.js"></script>
<script>
var printer = "Epson";
qz.websocket.connect().then(function() {
return qz.printers.find(printer);
}).then(function(found) {
var config = qz.configs.create(found);
var data = "\n\n\n\n\n\n\n\n\n\n" +
var1 +// echoed above via php
"\n\n\n\n\n\n\n\n\n\n";
return qz.print(config, [data]);
}).then(qz.websocket.disconnect).then(function() {
document.getElementById('status').innerHTML = 'Done';
}).catch(function(err) {
document.getElementById('status').innerHTML = err + '<br><a href="qz:launch">Launch QZ</a>';
throw err;
});
</script>
<h1 id="status">Attempting to print...</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment