PHP printing example

3,123 views
Skip to first unread message

nl2ttl

unread,
Apr 10, 2012, 11:26:59 AM4/10/12
to jZebra users
Maybe not the most beautiful solution only this is the way I do it.

First I create a file using php with a unique code.

<php code>

$myFile = "./barcodes/" . $barcode . ".txt";
//$myFile = "./barcodes/1.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "A250,5,0,3,1,1,N,\"".$firstname. " ".
$lastname."\"\n";
fwrite($fh, $stringData);
$stringData = "A250,30,0,3,1,1,N,\"".$company."\"\n";
fwrite($fh, $stringData);
$stringData = "B250,60,0,1,3,7,100,B,\"".$barcode."\"\n";
fwrite($fh, $stringData);
$stringData = "A250,195,0,3,1,1,N,\"Room number: " .$room_nr .
"\"\n";
fwrite($fh, $stringData);
$stringData = "A250,215,0,3,1,1,N,\"Valid until: " .$end_date .
"\"\n";
fwrite($fh, $stringData);
$stringData = "P1\n";
fwrite($fh, $stringData);
$stringData = "N\n";
fwrite($fh, $stringData);

fclose($fh);

<end php code?>

<html>
<tr class="text_12">
<td width="960" colspan="3" align="center">
<FORM NAME="test">
<H2>
<INPUT TYPE="TEXT" NAME="barcode" value="<?php echo $barcode; ?>"
style="width:960px; height:25px"><BR>
<br />
<INPUT TYPE="Button" Value="PRINT BARCODE"
onClick="printbarcode(this.form)" style="width:960px; height:50px;"">
</H2>
</FORM>

<applet name="jZebra" code="jzebra.RawPrintApplet.class"
archive="jzebra.jar" width="0" height="0">
<param name="printer" value="zebra">
<!-- <param name="sleep" value="200"> -->
</applet>
</td>
</tr>

</html>

In the script part:

function printbarcode(frm) {
var applet = document.jZebra;
if (applet != null) {
applet.appendFile("http://xx-xx.com/ticket/admin/barcodes/" +
frm.barcode.value + ".txt");
applet.print();
}
window.location.replace(window.location.pathname);

// if (frm.barcode.value == "")
// alert("Lege barcode geprint")
// else
// alert("Barcode geprint")
}

Tres Finocchiaro

unread,
Apr 10, 2012, 8:33:40 PM4/10/12
to jzebra...@googlegroups.com
Thanks for this contribution.  You can also achieve this by skipping the file write (do an echo of your print commands in a separate php file) and providing the url of the php file directly in the appendFile() command.  As long as there's no cookies needed, jZebra will load the php output as if it's a text file.

If you do need cookies, you can load the URL with AJAX (jQuery for example) and do an append using something like JSON once the contents come back.

-Tres


--
To unsubscribe from this group, send email to jzebra-users...@googlegroups.com

http://code.google.com/p/jzebra



--
- Tres.Fin...@gmail.com

Erik Lukiman

unread,
Apr 10, 2012, 10:14:54 PM4/10/12
to jzebra...@googlegroups.com
dear nl2ttl & Tres,

this might be a clearer example for it with ajax. in this example I'm using a barcode printer PF8t and name it PF8t.
--------
php file on server : server.php
<?php
$barcodePattern = '
ON,D
Q407,16
q831
S2
D10
ZT
JF
oR

N
A33,13,0,1,4,5,N,"%s"
B417,150,0,1E,1,0,105,N,"%s"
A38,68,0,1,2,2,N,"%s"
A509,306,0,1,3,4,N,"%s"
A40,288,0,4,4,4,N,"%s"
A559,356,0,1,1,1,N,"Generated by Erik"
P1

';
$name = 'Erik';
$barcode = '1234567890';
$refNo = '992/04/2012';
$city = 'Jakarta';
$boxNo = !empty($_POST['boxId']) ? $_POST['boxId'] : 'Box No';
$result = sprintf($barcodePattern, $name, $barcode, $refNo, $city, $boxNo);
echo json_encode(array(
'id' => $boxNo, //to verify that the result is valid
'barcode' => $result
));
?>

---------
html file on client : client.html
<html>
<head>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script src="directPrint.js" type="text/javascript"></script>
</head>
<body>
<div id='tmp'></div>
<input type='button' id='print' value='Print Barcode' onClick='doClick();' />
<script>
function doClick () {
$.post('http://localhost/barcode' + '/server.php', { 'boxId' :'456' }, function (response) {  //URL should be change
$('#tmp').text(response.barcode);
directPrint.setPrinterName('PF8t'); //set printer name
directPrint.init('#tmp');
directPrint.print();
alert('Printing Box No. ' + response.id);
} else {
alert(response.message || 'Error has been detected.. please try again...');
}
}, 'json')
.error(function() {
alert("Error has been detected.. please try again...");
});
}
</script>
</body>
</html>

-------
the above code is a modified sample from my real application code. sorry if there's a coding error because I haven't had time to test it yet. But it should give you a clear concept of the sample code.

Thx
Erik
Message has been deleted

Erik Lukiman

unread,
Mar 8, 2013, 11:56:17 PM3/8/13
to jzebra...@googlegroups.com
source for directPrint https://code.google.com/p/jzebra/downloads/detail?name=directPrint.0.9.0.zip&can=2&q=

and yes, i forgot to include the call for the applet. thx for the correction

Thx
Erik


On Wed, Mar 6, 2013 at 9:55 PM, <ti...@noveweb.com.br> wrote:
Erik,

where is the directPrint.js file content and the call for the applet?

hugs!
--
--
To unsubscribe from this group, send email to jzebra-users...@googlegroups.com
 
http://code.google.com/p/jzebra
 
---
You received this message because you are subscribed to the Google Groups "jZebra users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jzebra-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages