php 打印
php 打印功能需要printer.dll文件
扩展下载地址 http://downloads.php.net/pierre/
这里有很多PHP的扩展
注意要找对版本,然后修改PHP.INI 开启扩展就可以使用了
个人封装了一个小类
类文件
<?php
class printer {
public $pname = ''; // 打印机名字
private $phandle = '';
protected $message = '';
public $tableHeader = array('名称','状态','做法','数量');//设置的表头
public $widthArray = array(0,260,390,510);//针对此种打印机设置的每列的宽度
public $font = 'SimSun'; //设置字体 默认为宋体
public $fontWidth = '15'; //设置字体的宽度
public $fontHeight = '25';//设置字体的高度
protected $y = 0; //坐标的y
function __construct($pname = '') {
$this->pname = $pname;
if (!$this->pconnect()) {
echo $this->message;
exit();
}
}
//连接
private function pconnect() {
if(!empty($this->pname)) {
if (!$this->phandle=@printer_open($this->pname)) {
$this->message = "could't connect printer whit name '".$this->pname."' ";
return false;
} else {
return true;
}
}
}
// 打印
public function pprint($sortName,$tableName,$tableTime,$dishData) {
//开始
printer_start_doc($this->phandle, "壹生活餐饮系统");
printer_start_page($this->phandle);
//创建字体
if (!$this->pcreateFont()) {
echo $this->message;
exit();
}
//打印首部
$this->pfrist($sortName,$tableName,$tableTime);
//打印头部
$this->pheader();
//打印内容
$this->pbody($dishData);
//打印一条线
$this->pline();
//删除字体
printer_delete_font($this->font);
//结束
printer_end_page($this->phandle);
printer_end_doc($this->phandle);
printer_close($this->phandle);
}
protected function pline() {
$pen = printer_create_pen(PRINTER_PEN_SOLID, 1, "000000"); //创建一个笔
printer_select_pen($this->phandle, $pen); // 选择笔
printer_draw_line($this->phandle, 1, $this->y+100, 800, $this->y+100); //划一条线
printer_delete_pen($pen); //删除笔
}
protected function pbody($dishData) {
foreach ($dishData as $k => $v ) {
$this->y += 50;
for ($i=0;$i<count($v);$i++) {
$this->pdrawText($v[$i],$this->widthArray[$i],$this->y);
}
}
}
//打印头部
protected function pheader() {
for ($i=0;$i<count($this->tableHeader);$i++) {
$this->pdrawText($this->tableHeader[$i],$this->widthArray[$i],$this->y);
}
}
protected function pfrist($sortName,$tableName,$tableTime) {
//y坐标自加
$this->pdrawText('菜品分类:'.$sortName,0,$this->y);
$this->y += 40;
$this->pdrawText('桌台名称:'.$tableName,0,$this->y);
$this->y += 40;
$this->pdrawText('开桌时间:'.$tableTime,0,$this->y);
$this->y += 50;
}
//画文字
protected function pdrawText($text,$x=0,$y=0) {
printer_draw_text($this->phandle, $text,$x,$y);
}
//创建字体并选择
protected function pcreateFont($fontWeight = PRINTER_FW_NORMAL) {
if (!$this->font = @printer_create_font($this->font, $this->fontHeight, $this->fontWidth, $fontWeight, false, false, false, 0)) {
$this->message = 'create font failed!';
return false;
} else {
// 选择字体
printer_select_font($this->phandle, $this->font);
return true;
}
}
}
调用方法
<?php
include_once('printer.php');
$printer = new printer('GP-U80300 Series'); //传入打印机名称 一定要现在服务端配置好打印机
$body = array(
0 => array('红烧西红柿','已落单','微辣',1),
1 => array('凉拌蔬菜','已落单','很辣',2),
2 => array('红烧字体长点','已落单','很辣',1),
3 => array('红烧字体长点','已落单','很辣',2),
4 => array('红烧字体长点蔬菜','已落单','很辣',1),
5 => array('红烧字体长点','已落单','很辣',2),
6 => array('红烧字体长点','已落单','很辣',1)
);
$printer->pprint('热菜','包厢001','2013-02-14 15:33:22',$body);
注意调用字体要用能支持中文的字体
发一个字体对照表,调用的时候用后面的英文名
字体:
黑体:SimHei
宋体:SimSun
新宋体:NSimSun
仿宋:FangSong
楷体:KaiTi
仿宋_GB2312:FangSong_GB2312
楷体_GB2312:KaiTi_GB2312
微软雅黑体:Microsoft YaHei
装Office会生出来的一些:
隶书:LiSu
幼圆:YouYuan
华文细黑:STXihei
华文楷体:STKaiti
华文宋体:STSong
华文中宋:STZhongsong
华文仿宋:STFangsong
方正舒体:FZShuTi
方正姚体:FZYaoti
华文彩云:STCaiyun
华文琥珀:STHupo
华文隶书:STLiti
华文行楷:STXingkai
华文新魏:STXinwei
php 打印功能只能再服务端打印,一般用不到,但是如果是局域网内的系统还是可以使用的
浙公网安备 33010602011771号