document = $document; $this->printer = $printer; $this->server = $server; } /** * * * @return PrintJob */ public function setPrinter($printer) { $this->printer = $printer; return $this; } /** * * * @return PrintJob */ public function setMedia($media) { $this->media = $media; return $this; } /** * * * @param string $name The print job name to use * @return PrintJob */ public function setJobName($name) { $this->jobName = $name; return $this; } /** * * * @return PrintJob */ public function setServer($server, $port) { $this->server = $server . (!!$port?":{$port}":""); return $this; } /** * * * @return PrintJob */ public function setOption($option, $value) { $this->options[$option] = $value; return $this; } /** * Attempt to submit the job using the lpr command. * * */ public function submit() { $options = []; if ($this->server !== null) { $options[] = "-H"; $options[] = $this->server; } if ($this->printer !== null) { $options[] = "-P"; $options[] = $this->printer; } if ($this->jobName !== null) { $options[] = "-T"; $options[] = $this->jobName; } foreach ($this->options as $option=>$value) { $options[] = "-o"; if ($value === true) { $options[] = $option; } else { $options[] = $option."=".$value; } } $optionString = join(" ", array_map("escapeshellarg", $options)); $cmdLine = sprintf("lpr %s %s", $optionString, escapeshellarg($this->document)); $ds = [ 1 => [ 'pipe', 'w' ], 2 => [ 'pipe', 'w' ] ]; $proc = proc_open($cmdLine, $ds, $pipes); do { usleep(10000); $status = proc_get_status($proc); } while ($status['running']); if ($status['exitcode'] > 0) { $stderr = stream_get_contents($pipes[2]); throw new LprException( sprintf("lpr exited with code %d: %s", $status['exitcode'], $stderr) ); } proc_close($proc); } }