mike42 / escpos-php

PHP library for printing to ESC/POS-compatible thermal and impact printers
Other
2.57k stars 862 forks source link

NetworkPrintConnector: Connection refused - Ethernet Printer #447

Closed juanxodj closed 7 years ago

juanxodj commented 7 years ago

I have an EPSON TM-U220 printer that works perfectly locally. My PC has IP (192.168.1.30) and the printer has IP (192.168.1.20). My problem arises when I upload my application to a server or hosting www.mywebpage.com .... When I want to print I throw this error: NetworkPrintConnector: Connection refused.

My autoload.php file looks like this:

<?php
defined('PRINTER_IP') or define('PRINTER_IP', "192.168.1.20");
defined('PRINTER_PORT') or define('PRINTER_PORT', 9100);
spl_autoload_register ( function ($class) {

    $prefix = "Mike42\\";
    $base_dir = __DIR__ . "/src/Mike42/";

    /* Only continue for classes in this namespace */
    $len = strlen ( $prefix );
    if (strncmp ( $prefix, $class, $len ) !== 0) {
        return;
    }

    /* Require the file if it exists */
    $relative_class = substr ( $class, $len );
    $file = $base_dir . str_replace ( '\\', '/', $relative_class ) . '.php';
    if (file_exists ( $file )) {
        require $file;
    }
} );

What could be the problem? As I say, locally it works perfectly.

mike42 commented 7 years ago

PHP is running server-side, so most likely the server at your hosting provider can't see your printer at 192.168.1.20. See FAQ entry: Can I print from my server on the Internet?.

I am also a maintainer at receipt-print-hq/chrome-raw-print, which solves the same issue for USB printers, and could be be improved for this use case. If you are interested in contributing, please take a look at receipt-print-hq/chrome-raw-print#3.