生成18mmx100mm 条码打印机的条码信息

brother 打印机 使用18mm 打印条码

使用:JsBarcode.all.min.js 生成条码

12mm 条码 80mm长度:12 2 fontsize3
字体 大小 4 ;字体到底部 1mm

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script src="JsBarcode.all.min.js"></script>
</head>
<body>
    
</body>
<script>
function createPrintPage(ht_code) {
  // 创建一个 iframe 用于打印
  const iframe = document.createElement('iframe');
  iframe.style.display = 'none';
  document.body.appendChild(iframe);

  // 设置 iframe 的内容
  const iframeContent = iframe.contentWindow.document;
  iframeContent.open();
  iframeContent.write(`
  <html>
    <head>
      <style>
        @media print {
          @page {
            size: 100mm 18mm;
            margin: 0;
          }
          body {
            margin: 0;
            padding: 0;
            width: 100mm;
            height: 18mm;
          }
          .barcode-container {
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
          }
          .barcode {
            width: 80mm;
            height: 12mm;
          }
          .text {
            position: absolute;
            font-size: 4mm;
            text-align: center;
            width: 100%;
            bottom: 1mm;
            color: black;
          }
        }
      </style>
    </head>
    <body>
      <div class="barcode-container">
        <canvas id="barcode" class="barcode"></canvas>
        <div class="text" id="barcode-text">Sample Barcode</div>
      </div>
    </body>
    </html>
  `);
  iframeContent.close();
  // 使用 JsBarcode 生成条形码
  const barcodeData = ht_code; // 条形码数据
  const barcodeElement = iframeContent.getElementById('barcode');
  const barcodeTextElement = iframeContent.getElementById('barcode-text');
  // 生成条形码
  JsBarcode(barcodeElement, barcodeData, {
    format: "CODE128", // 使用 CODE128 格式
    fontSize: 3,
    margin: 0,
    height: 12,
    width: 2,
    displayValue: false // 不显示条形码下方的文本
  });
  // 设置条形码文本
  barcodeTextElement.textContent = barcodeData;
  // 打印
  iframe.contentWindow.print();
}

// 调用函数生成并打印页面
createPrintPage("HTA101WDCX0320250024");
</script>
</html>

发表回复