如何让HTML页面适配TSC TTP-244 Pro 40x60mm标签打印?
解决TSC TTP-244 Pro标签打印机HTML打印溢出问题
问题背景
使用TSC TTP-244 Pro标签打印机打印40mm×60mm的HTML标签,已在打印机设置中正确配置纸张尺寸,但打印时内容溢出,会跨3个标签,无法适配单个标签。
需求目标
- 内容完美适配40mm×60mm标签尺寸
- 必要时自动调整字体大小和边距以适配标签
- 无多余分页,全部内容打印在单个标签上
- 通过CSS或HTML调整实现解决方案
已尝试代码
<!DOCTYPE html> <html> <head> <title>Label Printing</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: Arial, sans-serif; } @page { size: 40mm 60mm; margin: 0; } body { width: 40mm; height: 60mm; padding: 0.5mm; font-size: 4.5px; line-height: 1; overflow: hidden; } .header { text-align: center; margin-bottom: 0.5mm; font-weight: bold; } .company-info { display: flex; justify-content: space-between; margin-bottom: 0.5mm; } .company-name { font-family: 'Monotype Corsiva', cursive; font-size: 5px; } .company-meta { font-size: 3.5px; line-height: 1.2; } .divider { border-top: 0.5px solid #000; margin: 0.5mm 0; } .details-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.3mm; margin-bottom: 0.5mm; } .detail-pair { display: flex; white-space: nowrap; } .detail-label { font-weight: bold; margin-right: 0.5mm; } .detail-value { white-space: normal; word-break: break-all; max-width: 18mm; } .parts-title { font-weight: bold; text-align: center; margin-bottom: 0.3mm; font-size: 4.5px; } .parts-table { width: 100%; border-collapse: collapse; font-size: 4px; } .parts-table th, .parts-table td { padding: 0.2mm; border: none; } .parts-table td { word-break: break-all; max-width: 18mm; } .page-break { page-break-after: always; } </style> </head> <body> <!-- for loop start here --> <div class="header">OUTWARD DETAILS</div> <div class="company-info"> <div> <div class="company-name">XXXXXXXXXXXXXXXXXXXX<sup style="font-size:3px">®</sup></div> <div class="company-meta">ISO 9001:2015 Certified<br>XXXXXXXXXXXX</div> </div> <div> <div class="detail-pair"> <span class="detail-label">Packing Date:</span> <span>{{ date('d-m-Y H:i', strtotime($boxDetails[0]->package_date)) }}</span> </div> </div> </div> <div class="divider"></div> <div class="details-grid"> <div class="detail-pair"> <span class="detail-label">Party:</span> <span class="detail-value">{{ $salesRegister[0]->customer_name }}</span> </div> <div class="detail-pair"> <span class="detail-label">Inv No:</span> <span class="detail-value">{{ $salesRegister[0]->document_no }}</span> </div> <div class="detail-pair"> <span class="detail-label">Box:</span> <span class="detail-value">{{ $boxNo }}/{{ $totalBoxes }}</span> </div> <div class="detail-pair"> <span class="detail-label">Date:</span> <span class="detail-value">{{ \Carbon\Carbon::parse($salesRegister[0]->posting_date)->format('d-m-Y') }}</span> </div> <div class="detail-pair"> <span class="detail-label">Weight:</span> <span class="detail-value">{{ $boxDetails[0]->weight }}{{ $boxDetails[0]->parts_nlr_measure }}</span> </div> </div> <div class="divider"></div> <div class="parts-title">Parts Summary</div> <table class="parts-table"> <thead> <tr> <th>#</th> <th>INV No.</th> <th>Description</th> <th>Qty</th> </tr> </thead> <tbody> @foreach ($salesRegister as $index => $register) @if ($register->box_no == $boxNo) <tr> <td>*</td> <td>{{ $register->inv_no }}</td> <td>{{ $register->search_description }}</td> <td>{{ $register->qty }}</td> </tr> @endif @endforeach </tbody> </table> @if (!$loop->last) <div class="page-break"></div> @endif @endforeach <script> window.onload = function() { setTimeout(function() { window.print(); setTimeout(function() { window.close(); }, 100); }, 200); }; </script> </body> </html>
已完成操作
- 在打印机设置中设置纸张尺寸为40x60mm
- 手动缩小字体大小
解决方案
一、修正后的CSS配置
替换原有<style>标签内的代码,以下配置确保内容严格限制在40mm×60mm范围内:
* { margin: 0; padding: 0; box-sizing: border-box; font-family: Arial, sans-serif; } /* 强制打印机识别标签尺寸,禁用默认边距 */ @page { size: 40mm 60mm portrait; margin: 0; padding: 0; } /* 确保body严格匹配标签尺寸,超出部分隐藏 */ body { width: 40mm; max-height: 60mm; padding: 0.5mm; font-size: 4.5px; line-height: 1.1; overflow: hidden; position: relative; } .header { text-align: center; margin-bottom: 0.5mm; font-weight: bold; } .company-info { display: flex; justify-content: space-between; margin-bottom: 0.5mm; flex-wrap: nowrap; } .company-name { font-family: Arial, sans-serif; /* 替换特殊字体,避免渲染偏差 */ font-size: 5px; } .company-meta { font-size: 3.5px; line-height: 1.2; } .divider { border-top: 0.5px solid #000; margin: 0.5mm 0; } .details-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.2mm; margin-bottom: 0.5mm; } .detail-pair { display: flex; flex-wrap: wrap; /* 允许标签和值换行,避免横向溢出 */ } .detail-label { font-weight: bold; margin-right: 0.3mm; min-width: 30%; } .detail-value { word-break: break-word; flex: 1; } .parts-title { font-weight: bold; text-align: center; margin-bottom: 0.3mm; font-size: 4.5px; } .parts-table { width: 100%; border-collapse: collapse; font-size: 4px; } .parts-table th, .parts-table td { padding: 0.1mm; border: none; text-align: left; } .parts-table td { word-break: break-word; } /* 打印专属样式,确保浏览器不添加额外元素 */ @media print { body { margin: 0; padding: 0.5mm; width: 40mm; height: 60mm; } .page-break { page-break-after: always; } /* 禁用任何可能导致溢出的元素 */ * { overflow: hidden !important; } }
关键调整点说明:
- 给
@page添加portrait明确方向,避免打印机识别错误 - 将
company-name的特殊字体替换为Arial,防止字体渲染导致尺寸超出 - 修改
detail-pair为flex-wrap: wrap,允许内容换行,避免横向溢出 - 给body添加
max-height,配合overflow:hidden彻底截断超出内容 - 添加
@media print媒体查询,强制打印时的布局规则
二、针对TSC TTP-244 Pro的最佳实践
- 使用绝对单位:全程用mm作为尺寸单位,避免px/em等相对单位带来的缩放偏差
- 禁用默认边距:
@page必须设置margin:0,同时body的margin/paddding尽量最小化 - 简化布局:避免复杂嵌套和浮动,优先用flex/grid的紧凑布局
- 测试打印预览:打印前先在浏览器预览,确认布局无溢出后再发送到打印机
- 避免特殊字体:优先使用打印机内置的Arial、Courier等字体,防止字体替换导致尺寸变化
- 动态内容适配:如果内容长度不确定,可添加JavaScript动态调整字体大小:
function adjustFontSize() { const body = document.body; let fontSize = parseFloat(getComputedStyle(body).fontSize); while (body.scrollHeight > 60 * 3.7795275591) { // 60mm转像素(1mm≈3.78px) fontSize -= 0.1; body.style.fontSize = fontSize + 'px'; } } window.onload = function() { adjustFontSize(); setTimeout(function() { window.print(); setTimeout(window.close, 100); }, 200); };
内容的提问来源于stack exchange,提问作者vinayak
相关产品推荐
相关产品推荐

