要真正的要用b/s架构实现web打印,相了很长时间。总算实现了。但还是有些小问题。跟大家共享一下
首先实现条形码的部分
我首先采用的是java做后台的web project。即将生成条形码的功能在后台实现,后台再将产生的条形码返回到前台显示。在这里我主要用了两个框架
第一种:barbecue-1.5-beta1
整個jar包占得空間很少,下載的zip也只有大概860kb,解壓之後可以看到一個barbecue-1.5-beta1.jar。我們開發只需这一个就可以了。挺轻量的。
将上面的jar包放到webRoot的bin目录下
在web.xml加入下面代码:
<servlet>
<servlet-name>barbecueServlet</servlet-name>
<servlet-class>net.sourceforge.barbecue.BarcodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>barbecueServlet</servlet-name>
<url-pattern>/barbecue</url-pattern>
</servlet-mapping>
在你的jsp页面中加入下面一句
<img src="<%=request.getContextPath()%>/barbecue?data=<%=request.getParameter("code")%>&type=code128" height="50px" width=200px/>
src中前半部分就不用说了(servlet的东西),
后面的参数:
data(必選的,其他的可選):要显示的条形码数值
type:采用什么条形码国际标准。支持一下格式:
- Code128 (默認)
- Code128A
- Code128B
- Code128C
- EAN128
- USPS
- ShipmentIdentificationNumber
- SSCC18
- SCCC14ShippingCode
- GlobalTradeItemNumber
- UCC128
- PDF417
- Code39
- 3of9
- USD3
- Codabar
- USD4
- NW7
- Monarch
- 2of7
-
appId: 只有 UCC128 格式需要.
width: 最小的 bar 寬度 pixels.
height: 輸出的圖檔高度 pixels.
resolution: 輸出的圖檔解析度 dpi.
headless: 預設為 true, 當設為 false 時將不會同時繪出文字, 但是有些 type 即使設為 true 也不會繪出文字. (1.0.6RC1 此處有bug)
drawText: 預設為 false, 當設為 true 時將增加一個檢查碼, 只有 Code39 格式有用.
優點:比較輕量。
缺點:按照上面的解釋,好像當headless=false時,drawText為true時可以在條形碼下顯示對應的值可是沒顯示,不知道原因
下面是英文的说明:
Servlet class: net.sourceforge.barbecue.BarcodeServlet
Required parameters:
--------------------data the data to encode
Optional parameters:
--------------------type the barcode type - see below
width the width of the smallest bar in the barcode in pixels
height the height of the barcode in pixels
resolution the output resolution in DPI
checksum true to include a calculated checksum, false to omit (only used by Code39 and derivatives)
headless true for headless mode (default value), false to force non-headless mode
drawText (only takes effect if headless = false) true to draw text of barcode, false to omit
Types:
------Valid barcode types are (case insensitive):
Code128
Code128A
Code128B
Code128C
EAN128
USPS
ShipmentIdentificationNumber
SSCC18
SCCC14ShippingCode
GlobalTradeItemNumber
UCC128 Note: UCC128 requires an additional parameter: appid
PDF417
Code39
3of9
USD3
Codabar
Std2of5
Int2of5
USD4
NW7
Monarch
2of7
PostNet
UPCA
RandomWeightUPCANote that if the type is omitted then the type will default to Code128 (optimal).
Examples:
---------BarcodeServlet?data=12345
BarcodeServlet?data=12345&width=3&height=100&resolution=100
BarcodeServlet?data=12345&drawText=yes
BarcodeServlet?data=12345&type=Code128B
BarcodeServlet?data=12345&type=UCC128&appid=402
BarcodeServlet?data=12345&type=PDF417
BarcodeServlet?data=12345&headless=false&drawText=trueServlet Container Configuration:
--------------------------------When running the servlet in headless mode (the default) you must also set the Java VM
running the servlet to use headless mode, by setting the following VM parameter:-Djava.awt.headless=true
第二种:barcode4j
下載barcode4j-2.0-bin.zip,這個估計有7M多
需要用到的包有avalon-framework-4.2.0.jar,barcode4j.jar,commons-cli-1.0.jar
在web.xml中添加代碼
<servlet>
<servlet-name>barbecueServlet</servlet-name>
<servlet-class>net.sourceforge.barbecue.BarcodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>barbecueServlet</servlet-name>
<url-pattern>/barbecue</url-pattern>
</servlet-mapping>在jsp頁面中添加
<img src="<%=request.getContextPath()%>/barcode?msg=<%=request.getParameter("code")%>&type=code128&fmt=jpeg" height="50px" width=200px/>
參數說明
msg:打印的數據
type:類型
fmt:打印是以什麽格式打印
還有很多參數,網上可以找到解釋
上面兩種方式都是通過后台执行产生条形码的。在前台产生的可以使用jquery-barcode
第三种:jquery-barcode方式
首先下载jquery-barcode
http://barcode-coder.com/en/barcode-jquery-plugin-201.html
添加
- <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
- <script type="text/javascript" src="jquery-barcode.js"></script>
在body中添加
<div id="barcodeTarget" class="barcodeTarget"></div>
<script>$("#barcodeTarget").barcode("1234567", "ean8");</script>
注意:顺序不能变
具体的使用可以看上面的链接
三种方式比较:
前两种都是以图片方式显示的,所以打印机可以打印出来,后面一种是通过css样式定义出来的,所以不能通过打印机打印(其实可以以bmp方式打印,但是官网上说不能在IE下使用,无语,期待升级)。