protected function finalizeWin($data)
{
/* Windows-friendly printing of all sorts */
if (!$this->isLocal) {
/* Networked printing */
$device = "\\\\" . $this->hostname . "\\" . $this->printerName;
if ($this->userName !== null) {
/* Log in */
$user = "/user:" . ($this->workgroup != null ? $this->workgroup . "\\" : "") . $this->userName;
if ($this->userPassword == null) {
$command = sprintf("net use %s %s", escapeshellarg($device), escapeshellarg($user));
$redactedCommand = $command;
} else {
$command = sprintf("net use %s %s %s", escapeshellarg($device), escapeshellarg($user), escapeshellarg($this->userPassword));
$redactedCommand = sprintf("net use %s %s %s", escapeshellarg($device), escapeshellarg($user), escapeshellarg("*****"));
}
$retval = $this->runCommand($command, $outputStr, $errorStr);
if ($retval != 0) {
throw new Exception("Failed to print. Command \"{$redactedCommand}\" " . "failed with exit code {$retval}: " . trim($errorStr));
}
}
/* Final print-out */
$filename = tempnam(sys_get_temp_dir(), "escpos");
file_put_contents($filename, $data);
if (!$this->runCopy($filename, $device)) {
throw new Exception("Failed to copy file to printer");
}
unlink($filename);
} else {
/* Drop data straight on the printer */
if (!$this->runWrite($data, $this->printerName)) {
throw new Exception("Failed to write file to printer at " . $this->printerName);
}
}
}