1.http://www.qinms.com/webapp/barcode/ 免费生成条码.
下面介绍:JsBarcode.all.js
条码支持的有:
CODE128 CODE128 (自动模式切换)
CODE128 A/B/C (强制模式)
EAN EAN-13
EAN-8
EAN-5
EAN-2
UPC (A)
CODE39
ITF-14
MSI MSI10
MSI11
MSI1010
MSI1110
Pharmacode
Codabar
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no" />
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="js/JsBarcode.all.js"></script>
</head>
<body>
<svg id="svgcode"></svg>
<!-- or -->
<canvas id="canvascode"></canvas>
<!-- or -->
<img id="imgcode" />
<script>
$("#svgcode").JsBarcode('Hi!');//or JsBarcode("#svgcode", "Hi!");
$("#canvascode").JsBarcode('Hello world!');//or JsBarcode("#canvascode", "Hello world!");
$("#imgcode").JsBarcode("I'm huangenai!");//or JsBarcode("#imgcode", "I'm bwju!");
</script>
</body>
</html>
JsBarcode 我们还可以设置条码的一些属性(可设置属性详细介绍 链接:https://github.com/lindell/JsBarcode/wiki/Options)
<img />
<script>
JsBarcode("#imgcode", "123", {
format: "CODE39",//选择要使用的条形码类型
width:3,//设置条之间的宽度
height:100,//高度
displayValue:true,//是否在条形码下方显示文字
text:"456",//覆盖显示的文本
fontOptions:"bold italic",//使文字加粗体或变斜体
font:"fantasy",//设置文本的字体
textAlign:"left",//设置文本的水平对齐方式
textPosition:"top",//设置文本的垂直位置
textMargin:5,//设置条形码和文本之间的间距
fontSize:15,//设置文本的大小
background:"#eee",//设置条形码的背景
lineColor:"#2196f3",//设置条和文本的颜色。
margin:15//设置条形码周围的空白边距
});
</script>
http://www.bootcdn.cn/jsbarcode/readme/ 官网.
同事写的代码:
<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<script src="../../../../static/js/JsBarcode.all.js"></script>
<script src="${ctx}/static/plugin/jquery-2.2.3/jquery.js"></script>
<link rel="shortcut icon" type="image/x-icon"
href="${ctx}/static/favicon.ico" />
<link rel="stylesheet"
href="${ctx}/static/plugin/bootstrap-3.3.7/css/bootstrap.min.css">
<link rel="stylesheet"
href="${ctx}/static/plugin/font-awesome-4.5.0/css/font-awesome.min.css">
<link rel="stylesheet"
href="${ctx}/static/plugin/bootstrap-datepicker-1.6.4/css/bootstrap-datepicker3.css">
<link rel="stylesheet"
href="${ctx}/static/plugin/bootstrapvalidator-0.5.3/css/bootstrapValidator.min.css">
<link rel="stylesheet"
href="${ctx}/static/plugin/select2-4.0.3/css/select2.css">
<link rel="stylesheet"
href="${ctx}/static/plugin/zTree-3.5.26/css/metroStyle.css">
<link rel="stylesheet"
href="${ctx}/static/plugin/adminlte-2.3.7/css/AdminLTE.min.css">
<link rel="stylesheet"
href="${ctx}/static/plugin/adminlte-2.3.7/css/skins/skin-blue.min.css">
<link rel="stylesheet" href="${ctx}/static/css/admin.css">
<style>
table{
font-family: 微软雅黑, 宋体;
font-weight: normal;
margin: 0px;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
font-size: 10px;
}
@font-face {
font-family: code128;
src: url(../../../../static/css/code128.ttf);
}
</style>
<a class="btn btn-primary" href="#" onclick="printbarcode()">打印</a>
<c:forEach var="listItem" items="${list}">
<div style="height: 100%;">
<div style="display:">
<div style="font-size: 100px;text-align:center;">${listItem.code }</div>
<div style="text-align:center;">
<img id="barcode" name="barcode" style="width: 500px;height: 100px;"/>
<input type="hidden" name="receiptNo" value="${listItem.code }">
</div>
</div>
</div>
</c:forEach>
<script>
var receiptNos = document.getElementsByName("receiptNo");
var barcodes = document.getElementsByName("barcode");
for (var int = 0; int < barcodes.length; int++) {
var barcode = barcodes[int], str = receiptNos[int].value, options = {
format : "CODE128",
displayValue : true,
fontSize : 20,
height : 80
};
JsBarcode(barcode, str, options);//原生
}
function printbarcode() {
$("a").hide();
window.print();
}
</script>
下方的JS是关键.
GitHub:https://github.com/lindell/JsBarcode
条码生成器:http://lindell.me/JsBarcode/generator/
JsBarcode示例与设置:http://codepen.io/lindell/pen/mPvLXx?editors=1010
简单的JsBarcode演示:http://codepen.io/lindell/pen/eZKBdO?editors=1010