Cristian Cutitei
Cristian Cutitei

Reputation: 150

PHP Printing - How do I print directly from PHP on local server printer?

I would like to print documents to my POS printer from my local server automatically.

I have tried Mike's method https://github.com/mike42/escpos-php but to no avail, this is my code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\CapabilityProfile;

class PrintReceipt extends Controller
{
   public function TestPrint(){
       $profile = CapabilityProfile::load("simple");
       $connector = new WindowsPrintConnector("LPT1");
       $printer = new Printer($connector, $profile);

       $printer -> text("Hello World!\n");

       $printer -> cut();

       $printer -> close();

   } 

but I get this error: file_put_contents(LPT1): failed to open stream: No such file or directory The printer is connected and shared to the LPT1 port. Any ideeas on how to fix my code, or another way of aproaching this? I think normal printing should work as well, it does not matter if the method is for POS or not.

Thank you in advance!

Upvotes: 0

Views: 4136

Answers (1)

Ro Achterberg
Ro Achterberg

Reputation: 2694

The source is commented and tells you the following.

From https://github.com/mike42/escpos-php/blob/master/example/interface/windows-usb.php:

/**
 * Install the printer using USB printing support, and the "Generic / Text Only" driver,
 * then share it (you can use a firewall so that it can only be seen locally).
 *
 * Use a WindowsPrintConnector with the share name to print.
 *
 * Troubleshooting: Fire up a command prompt, and ensure that (if your printer is shared as
 * "Receipt Printer), the following commands work:
 *
 *  echo "Hello World" > testfile
 *  copy testfile "\\%COMPUTERNAME%\Receipt Printer"
 *  del testfile
 */

And from https://github.com/mike42/escpos-php/blob/development/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php:

/**
 * Connector for sending print jobs to
 * - local ports on windows (COM1, LPT1, etc)
 * - shared (SMB) printers from any platform (smb://server/foo)
 * For USB printers or other ports, the trick is to share the printer with a
 * generic text driver, then connect to the shared printer locally.
 */

See these articles on how to share a printer:

Upvotes: 1

Related Questions