前言
最近項目中用到了打印機,最開始的完全不懂,現在弄好了,所以做了總結,該篇包括后臺的調用打印(兩種方式)跟前端的js的打印,但是只有ie現在支持打印,而且如果想遠程連接打印機,二維碼的生成和直接由打印機的命令進行操作,就要把修改瀏覽器的安全配置,下面再做詳細的介紹
第一種后臺打印:
使用javax中的printservicelookup類進行打印,可以直接調用默認的打印機,也可以使用下列的方法進行篩選打印:
1
|
printservicelookup.lookupmultidocprintservices(flavors, attributes); |
可執行代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
public static void main(string[] args) { fileinputstream textstream = null ; try { textstream = new fileinputstream( "地址" ); } catch (filenotfoundexception e) { e.printstacktrace(); } if (textstream != null ) // 當打印內容不為空時 { // 指定打印輸出格式 docflavor flavor = docflavor.input_stream.pdf; //service_formatted.printable // 定位默認的打印服務 printservice printservice = printservicelookup.lookupdefaultprintservice(); // 創建打印作業 docprintjob job = printservice.createprintjob(); // 設置打印屬性 printrequestattributeset pras = new hashprintrequestattributeset(); // 設置紙張大小,也可以新建mediasize類來自定義大小 pras.add(mediasizename.iso_a4); docattributeset das = new hashdocattributeset(); // 指定打印內容 doc doc = new simpledoc(textstream, flavor, das); // 不顯示打印對話框,直接進行打印工作 try { job.print(doc, pras); // 進行每一頁的具體打印操作 } catch (printexception pe) { pe.printstacktrace(); } } else { // 如果打印內容為空時,提示用戶打印將取消 joptionpane.showconfirmdialog( null , "sorry, printer job is empty, print cancelled!" , "empty" , joptionpane.default_option, joptionpane.warning_message); } } |
第二種后臺打印:
注意:第二種跟第三種打印使用的是打印機的命令進行操作,這里需要jna的jar包,還有jdk要求是32位的,并且要要注冊對應的dll,對應不同的系統要在不同的windows下進行注冊dll,注冊成功之后需要win+r ,調用并運行 regsvr32.exe tscactivex.dll 指令
可執行代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class javademo { public interface tsclibdll extends library { tsclibdll instance = (tsclibdll) native .loadlibrary( "tsclib" , tsclibdll. class ); int about(); int openport(string pirntername); int closeport(); int sendcommand(string printercommand); int setup(string width, string height, string speed, string density, string sensor, string vertical, string offset); int downloadpcx(string filename, string image_name); int barcode(string x, string y, string type, string height, string readable, string rotation, string narrow, string wide, string code); int printerfont(string x, string y, string fonttype, string rotation, string xmul, string ymul, string text); int clearbuffer(); int printlabel(string set, string copy); int formfeed(); int nobackfeed(); int windowsfont( int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, string szfacename, string content); } public static void main(string[] args) { system.setproperty( "jna.encoding" , "gbk" ); // 支持中文 simpledateformat df = new simpledateformat( "yyyy-mm-dd hh:mm:ss" ); string time = df.format( new date()); string qrcode = "pd102011" ; tsclibdll.instance.openport( "tsc ttp-244 pro" ); //打印機型號 tsclibdll.instance.setup( "70" , "40" , "5" , "8" , "0" , "2" , "0" ); tsclibdll.instance.clearbuffer(); string command = "qrcode 120,90,q,8,a,0,m1,s7,\"" + qrcode+ "\"" ; //打印二維碼的參數和內容 tsclibdll.instance.sendcommand(command); //傳送指令 tsclibdll.instance.sendcommand( "text 300 70 36 0 0 0 arial" + "辦公耗材-標簽紙" ); //tsclibdll.instance.windowsfont(300, 70, 36, 0, 0, 0, "arial", "辦公耗材-標簽紙"); tsclibdll.instance.printlabel( "1" , "1" ); tsclibdll.instance.closeport(); } } |
注意:這種打印方式,需要usb連接(ttp-244 pro),如果想在線生成,遠程連接,需要使用第三種方式
第三種js打印
在使用js進行打印的時候要主要瀏覽器的限制,目前只有ie瀏覽器進行支持,在進行打印時,控制臺出現automation 服務器不能創建對象的,請調節瀏覽器的安全中的自定義級別中的activex中相關的設置為啟用,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<script type= "text/javascript" > function printqrcode(){ var tscobj = new activexobject( "tscactivex.tsclib" ); //打印機使用的dll //tscobj.activexabout(); // 打印機信息 tscobj.activexopenport ( "tsc ttp-244 pro" ); //打印機名稱,不能寫錯 //tscobj.activexdownloadpcx ("d:/myeclipseproject/filesmanager/trunk/doc/02-參考資料/03-二維碼打印機/01-開發幫助/php調用范例/tscactivex.dll-php-example/","123.pcx"); tscobj.activexsetup( "70" , "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 ( 260 , 60 , 60 , 0 , 2 , 0 , "arial" , "xx專用" ); //var cmd = 'qrcode 條碼x方向起始點,條碼y方向起始點,糾錯級別,二維碼高度,a(a和m),旋轉角度,m2(分為類型1和類型2),s1 (s1-s8,默認s7),\"1231你好2421341325454353\"'; var cmd = 'qrcode 80,80,h,7,a,0,m2,s1,\"' +" 123456789 "+ '\"' ; tscobj.activexsendcommand(cmd); tscobj.activexwindowsfont ( 280 , 150 , 40 , 0 , 0 , 0 , "arial" , "123456789" ); tscobj.activexwindowsfont ( 180 , 260 , 30 , 0 , 0 , 0 , "arial" , "xxxxxxxx" ); tscobj.activexprintlabel ( "1" , "1" ); //(打印份數,每頁打印張數) tscobj.activexclearbuffer(); //清除 tscobj.activexcloseport(); //關閉打印端口 } </script> |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/gdhzdbh/p/9300771.html