Printing data to printer using PHP

后端 未结 2 621
日久生厌
日久生厌 2020-12-25 08:56

I have a question that has been troubling me for at least 3 weeks now. I need to print some data to a printer using php. I have data saved into a $print_output

相关标签:
2条回答
  • 2020-12-25 09:39

    You have several options to print with PHP on Windows:

    1. Extension "php_printer"

    Because PHP 5.2.0 is outdated, it's quite hard to find compiled extension. You might try to drop in the php_printer extension for 5.2.8:

    http://downloads.php.net/pierre/php_printer-cvs-20081215-5.2.8-nts-Win32.zip

    Add to your php.ini:

    extension=php_printer.dll
    

    Recent version might be found at Pierre's download site: http://downloads.php.net/pierre/

    2. Execute the windows CLI command "print" via PHP

    An alternative solution is to use the windows command "print".

    See http://technet.microsoft.com/en-us/library/cc772773%28v=ws.10%29.aspx

    exec("print /d:\\192.168.1.33_4\\Printer_Office c:\accounting\report.txt); 
    

    3. Use Sockets

    $fp=pfsockopen("192.168.1.201",9100);
    fputs($fp,$print_data);
    fclose($fp);
    

    4. Output to HTML, open in browser and trigger window.print()

    0 讨论(0)
  • 2020-12-25 10:00

    For anyone who is having the same trouble, I figured out I can simply push the data using socket programming as follows. The ip address below is my printer's ip address. You can telnet into your printer to make sure the connection works beforehand if you'd like.

    if(isset($_POST['order'])){
    $print_output= $_POST['order'];
    }
    try
    {
        $fp=pfsockopen("192.168.1.33", 9100);
        fputs($fp, $print_output);
        fclose($fp);
    
        echo 'Successfully Printed';
    }
    catch (Exception $e) 
    {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
    
    0 讨论(0)
提交回复
热议问题