如何在php中格式化打印输出 [英] how to format printing output in php
问题描述
我是php的新手,并编写了一个代码,该代码从数据库中获取一些信息并将其转换为code128条码.将其回显到页面上没问题:
I'm new to php and made a code that takes some information from a database and converts it into code128 barcode. Echoing it to the page is no problem:
$finalvar = '<p>'.bar128(stripcslashes($row['Flowers'])).'</p>';
echo $finalvar;
到目前为止,一切都很好.我现在想在网络打印机上打印该条形码,并使用它:
So far so good. I now want to print this barcode on a network printer and use this:
$port = "9100";
$host = "10.64.33.33";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed, reason: " . socket_strerror(socket_last_error ()) . "\n";
} else {
echo "OK.\n";
}
$result = socket_connect($socket, $host, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror (socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
socket_write($socket, $finalvar);
socket_close($socket);
打印效果很好,但是纸张不是像图片中那样打印条形码,而是像这样:
The printing works perfectly but instead of printing the barcode like in the picture, the paper looks like this:
<p><table cellpadding=0 cellspacing=0><tr><td><div class="128"
style"border-"
我认为这可能更多是格式化问题,但是我没有找到任何有效的方法.
如何像上图所示打印$finalvar
?
I think this is probably more of a formatting thing, but I didn't find anything that worked.
How can I print $finalvar
like in the picture shown above?
推荐答案
对于任何想知道我是如何做到的人:
For anyone who wonders how I made it:
大多数标签打印机支持它们自己的语言,例如ZPL(斑马编程语言)或DPL(Datamax编程语言).您只需发送命令即可转换并打印变量.您甚至不需要将其转换为条形码,打印机即可为您完成. 您将需要向打印机发送1行ZPL. ZPL非常简单,您可以在此处亲眼看到. 如您在我提供的链接中所见,您可以仅使用以下命令创建条形码:
Most label printers support their own languages like ZPL (Zebra Programming language) or DPL (Datamax Programming language). You can just send a command to convert and print your variable. You don't even need to convert it into barcode, the printer does that for you. You will need to send 1 line of ZPL to your printer. ZPL is very easy and you can see that for yourself here. As you can see in the link I provided, you can create a barcode with only this command:
^XA^BY5,2,270^FO100,550^BC^FD$yourvariable^FS^XZ
就我而言,我使用上面的确切代码来打印简单的条形码.这是您在PHP中实现此方法并将其发送到打印机的方式:
In my case, I used this exact code above to print a simple barcode. This is how you implement this in PHP and send it to the printer:
$variable = "ABC123"; //the variable you want to convert to barcode and print
$print_data = ^XA^BY5,2,270^FO100,550^BC^FD$variable^FS^XZ; //this is the ZPL Code
// Open a telnet connection to the printer, then push all the data into it.
try
{
$fp=pfsockopen("10.64.57.51",9100); //IP of your printer and port
fputs($fp,$print_data);
echo "Job sent";
fclose($fp);
}
catch (Exception $e)
{
echo "Job Failed";}
注意: 我提供的ZPL代码是根据标签尺寸制作的.您可能需要更改一些X和Y数字以适合您的数字.您可以在我提供的网站上进行测试.
Note: The ZPL Code I provided is made for the size of my labels. You might need to change some X and Y numbers to fit yours. You can test this on the Website I provided.
这篇关于如何在php中格式化打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!