i have create the report.txt on serverside, now i want to print that file on client side , without open for user (to avoid alteration/ammenent in original report) i.e send that file to local printer.

plz send the sampal code in php for it

Dani AI

Generated

Short answer: you cannot make server-side PHP push a print job to a printer attached to a remote user's PC. PHP runs on the server and has no access to a visitor's local devices or the browser's print UI; the print action has to run on the client (browser/OS). As already said, the usual flow is: send the report to the browser and let the client print it. (stackoverflow.com)

The "undefined function printer_open()" you saw (your post, ) means the Windows-only PECL "printer" extension isn't available or loaded on that PHP build. That extension is not bundled with PHP and you must install a php_printer DLL that exactly matches your PHP (version, x86/64, TS/NTS and VC runtime). Check phpinfo() or run a quick test to see if the extension is present:

<?php
echo extension_loaded('printer') ? 'printer: loaded' : 'printer: missing';
?>

If it's missing you either install the compatible php_printer DLL on the server (only if the server is Windows) or use a different approach. (pecl.php.net)

Practical, cross-browser approach (recommended): produce a print-ready file on the server (PDF or well-formatted HTML/text), serve it inline so the browser opens it, and let the user print from their browser. Use the HTTP Content-Disposition header to prefer inline display rather than forcing a download, and use a print stylesheet for layout. Example (serve a text report inline):

<?php
$path = '/path/to/report.txt';
if (file_exists($path)) {
    header('Content-Type: text/plain');
    header('Content-Disposition: inline; filename="report.txt"');
    readfile($path);
}
?>

Content-Disposition advises the browser to render inline when possible; then a client-side print action opens the print dialog. (devdoc.net)

If the printer is reachable from the server (same LAN / publicly routable and allowed), you can print from PHP on the server side by sending the correct PDL to the printer (lpr, raw TCP socket to port 9100 for many printers, or vendor APIs). That only works when the server can reach the printer and when the data format matches what the printer expects (PDF/PostScript/ESC-POS etc.). Example (raw socket to port 9100):

<?php
$ip = '192.168.1.50';
$port = 9100;
$fp = fsockopen($ip, $port, $errno, $errstr, 2);
if ($fp) {
    $data = file_get_contents('report.txt');
    fwrite($fp, $data);
    fclose($fp);
} else {
    error_log("Printer connect failed: $errstr ($errno)");
}
?>

Port 9100 (raw/TCP) is a common option for network printing; check your printer docs. For silent client-side printing (no dialog) you need a local helper (native app, signed extension or enterprise-controlled solution). Legacy plugin approaches (ActiveX / Java applets) are essentially obsolete in modern browsers. (support.xerox.com)

Summary: for public web apps, generate a print-ready file on the server, serve it inline, and trigger printing on the client — that is the reliable, cross-browser pattern. If you control the client environment, consider a native helper or server-side printing only when the printer is reachable from the server.

Recommended Answers

All 2 Replies

i have tryed thr folloing code:-

$handle = printer_open();
printer_start_doc($handle);
printer_start_page($handle);

printer_create_dc($handle);
/* do some stuff with the dc */
/*printer_set_option($handle, PRINTER_TEXT_COLOR, "333333");
printer_draw_text($handle, 1, 1, "text");
printer_delete_dc($handle);

/* create another dc */
/*printer_create_dc($handle);
printer_set_option($handle, PRINTER_TEXT_COLOR, "000000");
printer_draw_text($handle, 1, 1, "text");*/
/* do some stuff with the dc */

/*printer_delete_dc($handle);

printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);*/

it's shows error undefine function printer_open() in line 1.

Just to clarify, you want to print the report on the client side? You cannot print from a PHP server to a printer attached to a user's computer directly. If you want to do that you need to send the file to the user's browser with a JavaScript command window.print() to print it from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.