PHP code example of bitdreamit / qz-tray-enterprise
1. Go to this page and download the library: Download bitdreamit/qz-tray-enterprise library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bitdreamit / qz-tray-enterprise example snippets
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use BitDreamIT\QzTray\Facades\QzTray;
class OrderController extends Controller
{
/**
* Print receipt for order
*/
public function printReceipt($orderId)
{
$order = Order::with('items', 'customer')->findOrFail($orderId);
// Generate receipt HTML
$receipt = view('receipts.order', compact('order'))->render();
// Print using ESC/POS format for thermal printer
QzTray::printEscpos(
'EPSON TM-T88V',
$this->generateEscposReceipt($order),
[
'copies' => 2,
'paperSize' => 'Receipt'
]
);
return response()->json([
'success' => true,
'message' => 'Receipt sent to printer'
]);
}
/**
* Print shipping label
*/
public function printShippingLabel($orderId)
{
$order = Order::findOrFail($orderId);
// Generate ZPL label
$zpl = $this->generateShippingLabelZpl($order);
// Print using Zebra label printer
QzTray::printZpl('Zebra LP 2844', $zpl, [
'label_width' => 100,
'label_height' => 150,
'copies' => 1
]);
return response()->json([
'success' => true,
'message' => 'Shipping label printed'
]);
}
/**
* Print barcode for product
*/
public function printProductBarcode($productId)
{
$product = Product::findOrFail($productId);
// Print barcode label
QzTray::printBarcode(
'Zebra LP 2844',
$product->sku,
'CODE128',
[
'width' => 2,
'height' => 100,
'human_readable' => true,
'copies' => 3
]
);
return response()->json([
'success' => true,
'message' => 'Barcode labels printed'
]);
}
/**
* Print PDF document
*/
public function printInvoice($orderId)
{
$order = Order::findOrFail($orderId);
// Generate PDF
$pdfUrl = route('orders.invoice.pdf', $order->id);
// Print PDF
QzTray::printPdf('HP LaserJet', $pdfUrl, [
'copies' => 1,
'paperSize' => 'A4',
'orientation' => 'portrait',
'pageRange' => '1'
]);
return response()->json([
'success' => true,
'message' => 'Invoice sent to printer'
]);
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use BitDreamIT\QzTray\Facades\QzTray;
class PrintJobController extends Controller
{
/**
* Get print queue status
*/
public function queueStatus()
{
$status = QzTray::getQueueStatus();
return response()->json([
'success' => true,
'data' => $status
]);
}
/**
* Get print job history
*/
public function jobHistory(Request $request)
{
$limit = $request->input('limit', 50);
$userId = $request->input('user_id');
$jobs = QzTray::getJobHistory($limit, $userId);
return response()->json([
'success' => true,
'data' => $jobs,
'count' => $jobs->count()
]);
}
/**
* Cancel print job
*/
public function cancelJob($jobId)
{
$job = PrintJob::findOrFail($jobId);
// Check if job can be cancelled
if (!$job->isPending()) {
return response()->json([
'success' => false,
'error' => 'Job cannot be cancelled'
], 400);
}
$job->markAsCancelled();
return response()->json([
'success' => true,
'message' => 'Job cancelled successfully'
]);
}
/**
* Clear printer queue
*/
public function clearQueue(Request $request)
{
$request->validate([
'printer' => '
// In your application service provider
public function boot()
{
// Set default printers for different modules
QzTray::saveUserPreference(auth()->id(), 'receipts', 'EPSON TM-T88V');
QzTray::saveUserPreference(auth()->id(), 'labels', 'Zebra LP 2844');
QzTray::saveUserPreference(auth()->id(), 'documents', 'HP LaserJet');
// Or in controller actions
QzTray::saveUserPreference(
auth()->id(),
'shipping_labels',
$request->input('printer')
);
}
// Retrieve module-specific printer
$labelPrinter = QzTray::getUserPreference(auth()->id(), 'labels');
// Create custom print templates
class ReceiptTemplate
{
public static function generate(array $orderData): string
{
return "
^XA
^FO50,50^A0N,50,50^FDORDER RECEIPT^FS
^FO50,120^A0N,30,30^FDOrder #: {$orderData['number']}^FS
^FO50,160^A0N,30,30^FDDate: " . date('Y-m-d H:i:s') . "^FS
^FO50,200^A0N,30,30^FDTotal: \${$orderData['total']}^FS
^FO50,250^GB700,3,3^FS
^FO50,270^A0N,25,25^FDThank you for your order!^FS
^XZ
";
}
}
// Usage
$zpl = ReceiptTemplate::generate($order->toArray());
QzTray::printZpl('Zebra LP 2844', $zpl);
// app/Providers/EventServiceProvider.php
protected $listen = [
\BitDreamIT\QzTray\Events\PrintJobQueued::class => [
\App\Listeners\LogPrintJob::class,
\App\Listeners\SendPrintNotification::class,
],
\BitDreamIT\QzTray\Events\PrinterConnected::class => [
\App\Listeners\UpdatePrinterStatus::class,
],
];
// Custom listener example
namespace App\Listeners;
use BitDreamIT\QzTray\Events\PrintJobQueued;
class SendPrintNotification
{
public function handle(PrintJobQueued $event)
{
$job = $event->printJob;
// Send notification to user
$job->user->notify(new PrintJobNotification($job));
// Log to external system
ExternalLogService::logPrintJob($job);
}
}
// Simple text printing
QzTray::printRaw('EPSON TM-T88V', "Hello World\nThis is a test print");
// With ESC/POS commands
$escpos = "\x1B\x40" . // Initialize printer
"\x1B\x61\x01" . // Center align
"RECEIPT\n" .
"\x1B\x61\x00" . // Left align
"Item 1: $10.00\n" .
"Item 2: $15.00\n" .
"\x1D\x56\x41\x10"; // Cut paper
QzTray::printRaw('EPSON TM-T88V', $escpos);
bash
# Run full diagnostics
php artisan tinker
>>> QzTray::getSystemStatus();
# Check inker
>>> QzTray::getAllPrinters();
# Test print
php artisan tinker
>>> QzTray::testPrint('Printer Name');
bash
# Run all tests
php artisan test
# Run specific test suite
php artisan test --testsuite=Unit
php artisan test --testsuite=Feature
# Generate test coverage
vendor/bin/phpunit --coverage-html coverage
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.