-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_cross_platform.php
More file actions
76 lines (63 loc) · 2.07 KB
/
Copy pathexample_cross_platform.php
File metadata and controls
76 lines (63 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Cross-platform printer example
* Works on both Windows and Linux
*/
// Check if the printer extension is loaded
if (!extension_loaded('printer')) {
die("ERROR: Printer extension is not loaded.\n" .
"Please install and enable the printer extension in php.ini\n");
}
echo "PHP Printer Extension - Cross-Platform Example\n";
echo "===============================================\n\n";
// List all available printers
echo "Available printers:\n";
try {
$printers = printer_list(PRINTER_ENUM_LOCAL);
if (empty($printers)) {
echo " No printers found.\n";
echo " Make sure you have at least one printer installed.\n\n";
exit(1);
}
foreach ($printers as $index => $printer) {
echo " [$index] " . $printer['NAME'];
if (isset($printer['IS_DEFAULT']) && $printer['IS_DEFAULT']) {
echo " (default)";
}
echo "\n";
}
echo "\n";
} catch (Exception $e) {
echo "ERROR listing printers: " . $e->getMessage() . "\n";
exit(1);
}
// Open the default printer
echo "Opening default printer...\n";
$handle = printer_open();
if (!$handle) {
die("ERROR: Failed to open printer connection.\n" .
"Make sure a default printer is configured.\n");
}
echo "Printer opened successfully.\n\n";
// Prepare the content to print
$content = "Hello from PHP Printer Extension!\n";
$content .= "=================================\n\n";
$content .= "This is a cross-platform printing test.\n";
$content .= "Date: " . date('Y-m-d H:i:s') . "\n";
$content .= "Platform: " . PHP_OS . "\n";
$content .= "PHP Version: " . PHP_VERSION . "\n\n";
$content .= "This example works on both Windows and Linux systems.\n";
// Print the content
echo "Sending content to printer...\n";
$result = printer_write($handle, $content);
if ($result) {
echo "Content sent successfully!\n";
echo "Check your printer for output.\n";
} else {
echo "ERROR: Failed to send content to printer.\n";
}
// Close the printer connection
echo "\nClosing printer connection...\n";
printer_close($handle);
echo "Done.\n";
?>