Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与/n相反?-ESC/POS和PHP右/左对齐在同一行上_Php_String_Printing_Epson - Fatal编程技术网

与/n相反?-ESC/POS和PHP右/左对齐在同一行上

与/n相反?-ESC/POS和PHP右/左对齐在同一行上,php,string,printing,epson,Php,String,Printing,Epson,所以我从我们的网络服务器(也在办公室)打印到办公室的联网热敏打印机上,这样客户就可以在网站上下订单,然后在销售部门的办公桌上弹出。这是我使用的代码,它工作得很好。但是,当打印纸条上的项目时,我希望项目文本居中对齐,价格文本向右对齐,但它不允许我这样做(因为我认为是同一行),所以我如何说新行(\n),然后将其反转。我已经试过\033F和\F了,但没有成功。有什么建议吗 $texttoprint = ""; //center,bold,underline - close underline, clo

所以我从我们的网络服务器(也在办公室)打印到办公室的联网热敏打印机上,这样客户就可以在网站上下订单,然后在销售部门的办公桌上弹出。这是我使用的代码,它工作得很好。但是,当打印纸条上的项目时,我希望项目文本居中对齐,价格文本向右对齐,但它不允许我这样做(因为我认为是同一行),所以我如何说新行(\n),然后将其反转。我已经试过\033F和\F了,但没有成功。有什么建议吗

$texttoprint = "";
//center,bold,underline - close underline, close bold
$texttoprint .= "\x1b\x61\x01\x1b\x45\x01\x1b\x2d\x02\x1b\x21\x10\x1b\x21\x20 Company name \x1b\x2d\x00\x1b\x45\x00";
$texttoprint .= "\n";
//normal text
$texttoprint .= "\x1b\x21\x00 Address";
$texttoprint .= "\n"; 
//normal text
$texttoprint .= "\x1b\x21\x00 Adress2";
$texttoprint .= "\n";
//normal text
$texttoprint .= "\x1b\x21\x00 Tel : ...";
$texttoprint .= "\n";
$texttoprint .= "\n";
//normal text
$texttoprint .= "\x1b\x21\x00 Website order";
$texttoprint .= "\n";
$texttoprint .= "\n";
//center,bold,underline - close underline, close bold
$texttoprint .= "\x1b\x61\x01\x1b\x45\x01\x1b\x2d\x02\x1b\x21\x10\x1b\x21\x20 Tax Invoice \x1b\x2d\x00\x1b\x45\x00";
$texttoprint .= "\n";
$texttoprint .= "\n";
//align center, normal text
$texttoprint .= "\x1b\x61\x01\x1b\x21\x00 1x product";
//align right, normal text
$texttoprint .= "\x1b\x61\x02\x1b\x21\x00 $200";
...
正如你在这里看到的,最后两个在同一条线上,我试图证明产品中心和价格是正确的。它们最终都会居中,如果我在两者之间加上a/n,那么它们就在错误的行上正确地对正了

$texttoprint = stripslashes($texttoprint);

$fp = fsockopen("192.168.0.168", 9100, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "\033\100");
$out = $texttoprint . "\r\n";
fwrite($fp, $out);
fwrite($fp, "\012\012\012\012\012\012\012\012\012\033\151\010\004\001");
fclose($fp);
}
$texttoprint=stripslashes($texttoprint);
$fp=fsockopen(“192.168.0.168”,9100,$errno,$errstr,10);
如果(!$fp){
回显“$errstr($errno)
\n”; }否则{ fwrite($fp,“\033\100”); $out=$texttoprint。“\r\n”; fwrite($fp,$out); fwrite($fp,“\012\012\012\012\012\012\012\012\012\033\151\010\004\001”); fclose($fp); }
您可以让打印机完成所有工作,但我认为最好在输出例程中实现一些关于打印机特性的知识。您应该知道每行可以打印多少个字符

然后,您可以使用
sprintf()
格式化整行,产品信息与左侧对齐,字段大小最大,价格与右侧对齐

$texttoprint .= sprintf('%s %f', $article, $price); // very basic start: A string and a float
$texttoprint .= sprintf('%-30.30s %8.2f', $article, $price); 
// 30 chars for the string, will not exceed this length, and 8 chars for the price, with decimal dot and 2 decimal digits.
请注意,不应在最终结果上使用
stripslashes()
。如果您对斜杠存在问题,那么斜杠是由“魔法引号”引入的,应该在开始时消除,而不是在结束时消除


另一种解决方案是将打印机切换到“Windows”模式,使他需要CR/LF进行完整的行打印输出。通过仅打印CR,您可以打印整行,而无需移动纸张,从而将打印头再次移动到行的开头。然后,您可以在已打印行的顶部再次打印,直到打印LF。请注意,如果涉及实际的打印机打印头,这可能会使情况变得更糟,因为它可能会触发额外的打印头移动,仅用于打印一行。打印机通常能够优化一行接一行的打印头移动。

感谢您的回复,我测试了您的方法,它确实有效,因此我将选择它作为我问题的答案,但是我已经实施了一个不太干净的解决方案,基于正常打印头的最大大小为42个字符,所以我只添加产品长度、价格长度和差异,然后添加空格。