Salve a tutti, ho uno script che una volta lanciato apre un socket con la stampante di rete e stampa direttamente un documento senza far apparire il classico pop-up di stampa, questo è il cuore della classe:
codice:
//Open a new connection to send the control file and data.
$stream = stream_socket_client("tcp://".$this->host.":".$this->port, $this->errNo, $this->errStr, $this->timeout);
if(!$stream){
return $this->errNo." (".$this->errStr.")";
} else {
$job = self::getJobId();//Get a new id for this job
//Set printer to receive file
fwrite($stream, chr(2).$queue."\n");
$this->debug .= "Confirmation of receive cmd:".ord(fread($stream, 1))."\n";
//Send Control file.
(isset($_SERVER['SERVER_NAME'])) ? $server = $_SERVER['SERVER_NAME'] : $server = "me";//Might be CLI and not have _SERVER
$ctrl = "H".$server."\nPphp\nfdfA".$job.$server."\n";
fwrite($stream, chr(2).strlen($ctrl)." cfA".$job.$server."\n");
$this->debug .= "Confirmation of sending of control file cmd:".ord(fread($stream, 1))."\n";
fwrite($stream, $ctrl.chr(0)); //Write null to indicate end of stream
$this->debug .= "Confirmation of sending of control file itself:".ord(fread($stream, 1))."\n";
if (is_readable($this->data)){
//It's a filename, rather than just some ascii text that needs printing. Open and stream.
if (strstr(strtolower($_ENV["OS"]), "windows")){
$this->debug .= "Operating system is Windows\n";
$data = fopen($this->data, "rb");//Force binary in Windows.
} else {
$this->debug .= "Operating system is not Windows\n";
$data = fopen($this->data, "rb");
}
fwrite($stream, chr(3).filesize($this->data)." dfA".$job.$server."\n");
$this->debug .= "Confirmation of sending receive data cmd:".ord(fread($stream, 1))."\n";
fwrite($stream, fread($data, filesize($this->data)));
fwrite($stream, chr(0));//Write null to indicate end of stream
$this->debug .= "Confirmation of sending data:".ord(fread($stream, 1))."\n";
fclose($data);
} else {
//Send data string
fwrite($stream, chr(3).strlen($this->data)." dfA".$job.$server."\n");
$this->debug .= "Confirmation of sending receive data cmd:".ord(fread($stream, 1))."\n";
fwrite($stream, $this->data.chr(0)); //Write null to indicate end of stream
$this->debug .= "Confirmation of sending data:".ord(fread($stream, 1))."\n";
}
}
Ora il problema sorge quando vado a stampare dei pdf, ovviamente la parte che effettua lo stream del documento da stampare è questa:
codice:
//It's a filename, rather than just some ascii text that needs printing. Open and stream.
if (strstr(strtolower($_ENV["OS"]), "windows")){
$this->debug .= "Operating system is Windows\n";
$data = fopen($this->data, "rb");//Force binary in Windows.
} else {
$this->debug .= "Operating system is not Windows\n";
$data = fopen($this->data, "rb");
}
fwrite($stream, chr(3).filesize($this->data)." dfA".$job.$server."\n");
$this->debug .= "Confirmation of sending receive data cmd:".ord(fread($stream, 1))."\n";
fwrite($stream, fread($data, filesize($this->data)));
fwrite($stream, chr(0));//Write null to indicate end of stream
$this->debug .= "Confirmation of sending data:".ord(fread($stream, 1))."\n";
fclose($data);
Che apre il file forzando la lettura in binario e lo invia al socket. Ora però se si tratta di un file di testo è tutto ok, se invece si tratta di un file pdf vengono stampati una serie di caratteri strani comprese le formattazioni proprie del file pdf.
E' possibile evitare questa cosa è inviare via socket un pdf alla stampante?
Grazie per eventuali risposte!