You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中无法为Brother QL-700标签打印机设置正确纸张尺寸

解决Brother QL-700在Java打印中设置62mm×29mm标签尺寸的问题

核心问题分析

你遇到的问题主要来自两个关键点:

  1. 单位错误:Java Paper类的setSize方法使用的是1/72英寸作为单位,而非毫米,你之前直接传入620、290(误以为是毫米×10)完全不符合要求。
  2. Java打印API与打印机驱动的映射问题:QL-700的DK-11209标签尺寸属于Brother自定义介质,Java默认的MediaSizeName列表可能无法直接识别,需要手动指定并配合打印属性集完成配置。

解决方案步骤

1. 修正尺寸单位转换

先把62mm×29mm转换成Java打印API要求的1/72英寸单位:

PageFormat pageFormat = job.defaultPage();
Paper paper = pageFormat.getPaper();

// 毫米转1/72英寸:1英寸=25.4毫米,1单位=1/72英寸
double mmToPrintUnit = 72.0 / 25.4;
double width = 62 * mmToPrintUnit;   // 约175.6单位
double height = 29 * mmToPrintUnit;  // 约82.1单位
double margin = 0;

paper.setSize(width, height);
// 注意:imageableArea的宽高要减去左右/上下边距的总和
paper.setImageableArea(margin, margin, width - margin * 2, height - margin * 2);
pageFormat.setPaper(paper);

2. 配合PrintRequestAttributeSet指定介质

仅修改PageFormat可能不足以让打印机识别,需要在打印请求中明确指定介质属性:

PrintRequestAttributeSet printAttrs = new HashPrintRequestAttributeSet();

// 创建自定义介质尺寸并添加到属性集
MediaSize customSize = new MediaSize(MediaSizeName.CUSTOM, width, height);
printAttrs.add(customSize);

// 额外:尝试指定打印机的DK标签类型(部分Brother驱动需要)
// 遍历打印机支持的介质,查找DK-11209对应的属性
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media[] supportedMedias = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
for (Media media : supportedMedias) {
    if (media.toString().contains("DK-11209") || media.toString().contains("62x29")) {
        printAttrs.add(media);
        break;
    }
}

// 执行打印时传入属性集
job.print(printAttrs);

3. 额外配置检查

  • 确保Brother打印机驱动为最新版本,旧驱动可能无法正确向Java暴露自定义介质尺寸。
  • 在系统打印机设置中,确认已将默认介质设置为DK-11209,避免打印机 fallback 到默认的100mm卷纸尺寸。
  • 若上述方法仍无效,可尝试直接使用Brother提供的SDK(如Brother Print SDK for Java),该SDK专门针对Brother打印机做了适配,能更精准地控制介质尺寸。

内容的提问来源于stack exchange,提问作者Trung Kien Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 10:28:12