因为需要调用打印机封装好的DLL文件,只能在IE下使用ActiveX来调用,直接使用tspl指令来打印。只在前台调用,所以后台什么也不需要,但是客户端需要进行配置,首先要安装打印机驱动,然后注册DLL
本案例相关的dll和整理的文档如下
http://download.csdn.net/detail/u010121228/9861057
在利用ActiveX调用之前,需要注册DLL文件,先把DLL文件夹下的两个 .dll 文件复制到 “C:\windows\system\ ”目录下,
32位系统可直接还用快捷键WIN+R调起并运行 Regsvr32.exe TSCActiveX.dll 指令
64位系统需要运行64位的注册,跳转到当前64位目录下
cd C:\Windows\SysWOW64
并运行 Regsvr32.exe TSCActiveX.dll 指令,如果不能打印,把dll文件放到 C:\Windows\SysWOW64 目录下再次注册。
JSP实现代码如下
<HTML>
<HEAD>
</HEAD>
<BODY>
<script type='text/javascript' language='javascript'>
//********** ActiveX Dll Functions Description **********
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXabout()
// Description: Display the DLL version on the screen.
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXopenport(ByVal PrinterName As String)
// Description: Start the Windows printer spool.
// Parameter:
// PrinterName : String, for local printer, please specified the printer driver name.
// For network printer, please specified the UNC path and printer name.
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXdownloadpcx(ByVal Filename As String, ByVal ImageName As String)
// Description: Download mono PCX graphic files to the printer
// Parameter:
// Filename : string; file name (including file retrieval path)
// ImageName : string, names of files that are to be downloaded in the printer memory (Please use capital letters)
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXsetup(ByVal LabelWidth As String, ByVal LabelHeight As String, ByVal Speed As String, ByVal Density As String, ByVal Sensor As String, ByVal Vertical As String, ByVal Offset As String)
// Description: Set up label width, label height, print speed, print density, sensor type, gap/black mark vertical distancegap/black mark shift distance.
// Parameter:
// LabelWidth : string, sets up label width; unit: mm
// LabelHeight : string, sets up label height; unit: mm
// Speed : string, sets up print speed, (selectable print speeds vary on different printer models)
// 1.0: sets print speed at 1.0"/sec
// 1.5: sets print speed at 1.5"/sec
// 2.0: sets print speed at 2.0"/sec
// 3.0: sets print speed at 3.0"/sec
// 4.0: sets print speed at 4.0"/sec
// 6.0: sets print speed at 6.0"/sec
// 8.0: sets print speed at 8.0"/sec
// 10.0: sets print speed at 10.0"/sec
// Density : string, sets up print density
// 0~15the greater the number, the darker the printing
// Sensor : string, sets up the sensor type to be used
// 0: signifies that vertical gap sensor is to be used
// 1: signifies that black mark sensor is to be used
// Vertical : string, sets up vertical gap height of the gap/black mark; unit: mm
// Offset : string, sets up shift distance of the gap/black mark; unit:: mm; in the case of the average label, set this parameter to be 0.
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXformfeed()
// Description: Skip to next page (of label); this function is to be used after setup
// Parameter: None
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXnobackfeed()
// Description: disable the backfeed function
// Parameter: None
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXsendcommand(ByVal command As String)
// Description: Sends built-in commands to the bar code printer
// Parameter: Refer to TSPL for details
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXclearbuffer()
// Description: Clear
// Parameter: None
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXwindowsfont(ByVal X As Long, ByVal Y As Long, ByVal Fontheight As Long, ByVal Rotation As Long, ByVal Fontstyle As Long, ByVal Fontunderline As Long, ByVal FaceName As String, ByVal TextContent As String)
// Description: Use Windows font to print text.
// Parameter:
// X: Integer, the starting point of the text along the X direction, given in points
// Y: Integer, the starting point of the text along the Y direction, given in points
// Fontheight : Integer, the font height, given in points.
// Rotation :Integer, rotation in counter clockwise direction
// 0 -> 0 degree
// 90-> 90 degree
// 180-> 180 degree
// 270-> 270 degree
// Fontstyle : Integer, font style
// 0-> Normal
// 1-> Italic
// 2-> Bold
// 3-> Bold and Italic
// Fontunderline : Integer, font with underline
// 0-> Without underline
// 1-> With underline
// FaceName : String, font type face. Specify the true type font name. For example: Arial, Times new Roman.
// TextContent : String, text to be printed.
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXbarcode(ByVal X As String, ByVal Y As String, ByVal CodeType As String, ByVal Height As String, ByVal Readable As String, ByVal Rotation As String, ByVal Narrow As String, ByVal Wide As String, ByVal Code As String)
// Description: Use built-in bar code formats to print
// Parameter:
// X: string; the starting point of the bar code along the X direction, given in points (of 200 DPI, 1 point=1/8 mm; of 300 DPI, 1point=1/12 mm)
// Y: string; the starting point of the bar code along the Y direction, given in points (of 200 DPI, 1 point=1/8 mm; of 300 DPI, 1 point=1/12 mm)
// CodeType : string
// 128 : Code 128, switching code subset A, B, C automatically
// 128M : Code 128, switching code subset A, B, C manually.
// EAN128 : Code 128, switching code subset A, B, C automatically
// 25 : Interleaved 2 of 5
// 25C : Interleaved 2 of 5 with check digits
// 39 : Code 39
// 39C : Code 39 with check digits
// 93 : Code 93
// EAN13 : EAN 13
// EAN13+2 : EAN 13 with 2 digits add-on
// EAN13+5 : EAN 13 with 5 digits add-on
// EAN8 : EAN 8
// EAN8+2 : EAN 8 with 2 digits add-on
// EAN8+5 : EAN 8 with 5 digits add-on
// CODA : Codabar
// POST : Postnet
// UPCA : UPC-A
// UPCA+2 : UPC-A with 2 digits add-on
// UPCA+5 : UPC-A with 5 digits add-on
// UPCE : UPC-E
// UPCE+2 : UPC-E with 2 digits add-on
// UPCE+5 : UPC-E with 5 digits add-on
// Height : string; sets up bar code height, given in points
// Readable : string, sets up whether to print human recognizable interpretation (text) or not.
// 0: prints no interpretation
// 1: prints interpretation
// Rotation : string; sets up rotation degrees
// 0: rotates 0 degree
// 90: rotates 90 degrees
// 180: rotates180 degrees
// 270: rotates 270 degrees
// Narrow : string; sets up narrow bar ratio, refer to TSPL user//s manual
// Wide : string; sets up narrow bar ratio, refer to TSPL user//s manual
// Code : string; bar code content
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXprinterfont(ByVal X As String, ByVal Y As String, ByVal FontName As String, ByVal Rotation As String, ByVal Xmul As String, ByVal Ymul As String, ByVal Content As String)
// Description: Use printer built-in fonts to print
// Parameter:
// X: string; the starting point of text (character string) along the X direction, given in points (of 200 DPI, 1 point=1/8 mm; of 300 DPI, 1 point=1/12 mm)
// Y: string; the starting point of text (character string) along the Y direction, given in points (of 200 DPI, 1 point=1/8 mm; of 300 DPI, 1 point=1/12 mm)
// FontName : string; built-in font type name, 12 kinds in sum
// 1: 8*/12 dots
// 2: 12*20 dots
// 3: 16*24 dots
// 4: 24*32 dots
// 5: 32*48 dots
// TST24.BF2: Traditional Chinese 24*24
// TST16.BF2: Traditional Chinese 16*16
// TTT24.BF2: Traditional Chinese 24*24 (Telecommunication Code)
// TSS24.BF2: Simplified Chinese 24*24
// TSS16.BF2: Simplified Chinese 16*16
// K: Japan, Korean font 24*24,
// L: Japan Korean font 16*16
// Rotation : string; sets up the rotation degree of the text (character string)
// 0: rotates 0 degree
// 90: rotate 90 degrees
// 180: rotate 180 degrees
// 270: rotate 270 degrees
// Xmul : string; sets up the magnification rate of text (character string) along the X direction, range: 1~8
// Ymul : string; sets up the magnification rate of text (character string) along the Y direction, range: 1~8
// Content : string; prints the content of text (character string)
//
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXprintlabel(ByVal NumberOfSet As String, ByVal NumberOfCopy As String)
// Description: Print label content
// Parameter:
// NumberOfSet : string; sets up the number of label sets
// NumberOfCopy : string, sets up the number of print copies
//--------------------------------------------------------------------------------------------------------------------------------------------------
//TSCActiveX.TSCLIB.ActiveXcloseport()
// Description: Close Windows printer spool.
// Parameter: None
//--------------------------------------------------------------------------------------------------------------------------------------------------
//
//********** JAVASCRIPT Example **********
//
//
function printQRcode(){
var TSCObj;
TSCObj = new ActiveXObject("TSCActiveX.TSCLIB");//打印机使用的dll
//TSCObj.ActiveXabout(); // 打印机信息
TSCObj.ActiveXopenport ("TSC TTP-342 Pro");//打印机名称,不能写错
//TSCObj.ActiveXdownloadpcx ("D:/myeclipseproject/filesManager/trunk/doc/02-参考资料/03-二维码打印机/01-开发帮助/PHP调用范例/TSCActiveX.DLL-PHP-Example/","123.PCX");
TSCObj.ActiveXsetup("55","40","4","15","0","2.5","0");//打印机设置
//TSCObj.ActiveXsetup("标签宽度","标签高度","打印速度","打印浓度(0-15)","感应器类别字串型,0 表示使用垂直間距感測器(gap sensor), 1 表示使用黑標感測器(black mark senso)","Gap/Black mark垂直间距(mm)","Gap/Black mark偏移距离(mm)");
//TSCObj.ActiveXformfeed();
//TSCObj.ActiveXnobackfeed();
TSCObj.ActiveXsendcommand("DIRECTION 1"); //设置标签方向 ,DIRECTION 1 左上角 (x,y)={0,0};DIRECTION 0 右下角 (x,y)={0,0};
TSCObj.ActiveXsendcommand ("SET TEAR ON");
TSCObj.ActiveXclearbuffer();
//TSCObj.ActiveXsendcommand ("PUTPCX 0,0,\"123.PCX\"");
/*
1、ActiveXprinterfont采用机器内置编码通常用来打英文。
2、ActiveXwindowsfont可以输出汉字,但是必须是系统中存在的字体。
TSCObj.ActiveXprinterfont ("a","b","c","d","e","f","g");
a:字符串,文字X方向起始点,以点表示。
b:字符串,文字Y方向起始点,以点表示。
c:內建字型名称,共12种(1: 8*12 dots 2: 12*20 dots 3: 16*24 dots 4: 24*32 dots 5: 32*48 dots TST24.BF2: 繁體中文 24*24 TST16.BF2: 繁體中文 16*16 TTT24.BF2: 繁體中文 24*24 (電信碼) TSS24.BF2: 簡體中文 24*24 TSS16.BF2: 簡體中文 16*16 K: 韓文 24*24 L: 韓文 16*16 )
d:字符串,旋转角度
e:字符串,X方向放大倍率1-8
f:字符串,Y方向放大倍率1-8
g:字符串,打印内容
ActiveXwindowsfont(a,b,c,d,e,f,g,h)
说明:使用Windows TTF字体打印文字。
参数:
a:整数类型,文字X方向起始点,以点表示。
b:整数类型,文字Y方向起始点,以点表示。
c:整数类型,字体高度,以点表示。
d:整数类型,旋转角度,逆时针方向旋转。0-旋转0°,90-旋转90°,180-旋转180°,270-旋转270°。
e:整数类型,字体外形。0:标签;1:斜体;2:粗体;3:粗斜体。
f:整数类型,下划线,0:无下划线;1:加下划线。
g:字符串类型,字体名称。如:Arial,Times new Roman。
h:字符串类型,打印文字内容。
*/
TSCObj.ActiveXwindowsfont (500, 200, 48, 90, 0, 0, "Arial", "\u7f16\u7801");
TSCObj.ActiveXwindowsfont (550, 200, 48, 90, 0, 0, "Arial", "你好啊1");
TSCObj.ActiveXwindowsfont (600, 200, 48, 90, 0, 0, "Arial", "你好啊2");
//var cmd = 'QRCODE 条码X方向起始点,条码Y方向起始点,纠错级别,二维码高度,A(A和M),旋转角度,M2(分为类型1和类型2),S1 (s1-s8,默认s7),\"1231你好2421341325454353\"';
var cmd = 'QRCODE 60,30,H,7,A,0,M2,S1,\"\u7f16\u7801\"';
TSCObj.ActiveXsendcommand(cmd);
TSCObj.ActiveXprintlabel ("1","1");//(打印份数,每页打印张数)
TSCObj.ActiveXclearbuffer();//清除
TSCObj.ActiveXcloseport();//关闭打印端口
}
</SCRIPT>
</BODY>
</HTML>