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

如何用lpr命令在Peripage A4连续纸打印机避免纸张浪费?

Peripage A4热敏打印机Mac下实现按需打印(避免空白页浪费)

问题说明

我用USB-C连接Peripage A4连续纸热敏打印机到MacBook(系统版本13.3.1),应用内打印和lpr命令打印均正常,比如执行以下命令可打印20行随机文本:

lorem --faust --lines 20 --randomize | lpr -P "PeriPage_A4" -o orientation-requested=1

但当前打印会用空白填充整页A4纸,我需要实现仅打印含文本的行后停止,或逐行打印不浪费纸张(每次打印的文本量不固定)。

现有效果 vs 期望效果:

Have:                   Want:

┌──────────────┐        ┌──────────────┐
│lorem ipsum   │        │lorem ipsum   │
│sit dolor et  │        │sit dolor et  │
│eniam unque.  │        │eniam unque.  │
│              │        └──────────────┘
│              │               ▲
│              │               │
│              │               │
│              │               │
│              │               │
│              │        printing stopped.
│              │
│              │
│              │     lots of blank paper
│              │           │
│              │ ◄─────────┘
│              │
└──────────────┘

我尝试过Python ESC/POS脚本,但打印机无法识别指令:

#!/usr/bin/env python3
import sys
import time
from escpos.printer import Usb

# Configure the printer
printer = Usb(0x09c5, 0x0200, in_ep=0x81, out_ep=0x02)

# Reset the printer
printer.hw('reset')

# Read the input text from stdin
input_text = sys.stdin.read()

# Print the input text
printer.text(input_text)

# Add a small delay
time.sleep(1)

# Cut the paper
#printer.cut()

# Close the printer connection
printer.close()

解决方法

方法1:自定义纸张尺寸配合lpr命令

CUPS默认按标准A4页处理打印任务,需自定义适配内容长度的纸张:

  1. 打开CUPS管理页:浏览器输入http://localhost:631(认证时用Mac用户名密码)
  2. 找到PeriPage_A4打印机,点击Administration > Set Default Options
  3. 在Media Size中选择Custom,先设置最小高度(比如1英寸),后续打印时动态调整
  4. 打印前统计文本行数,用-o media=Custom.WIDTHxHEIGHT指定纸张高度(1英寸=72点,每行约占7点):
    # 获取待打印文本
    PRINT_TEXT=$(lorem --faust --lines 20 --randomize)
    # 统计行数
    LINE_NUM=$(echo "$PRINT_TEXT" | wc -l)
    # 计算页面高度
    PAGE_HEIGHT=$((LINE_NUM * 7))
    # 执行打印(595为A4宽度的点数值,可根据打印机分辨率微调)
    echo "$PRINT_TEXT" | lpr -P "PeriPage_A4" -o orientation-requested=1 -o media=Custom.595x$PAGE_HEIGHT
    
    此方法让打印机仅打印容纳内容的区域,不会空走纸到整页A4。

方法2:切换CUPS到Raw模式,直接发送ESC/POS指令

之前的Python脚本无效,是因为CUPS默认会把打印内容转换成PostScript/PDF,过滤了原始ESC/POS指令,切换到Raw模式即可解决:

  1. 在CUPS管理页找到PeriPage_A4打印机,点击Administration > Modify Printer
  2. 一路下一步到Choose a Make and Model,选择Raw分类下的Raw Queue完成设置
  3. 修改Python脚本,用CUPS Raw模式发送打印数据,并添加适配Peripage的走纸指令(标准ESC/POS走纸指令为\x1BJ\x05,表示走5行,可按需调整):
    #!/usr/bin/env python3
    import sys
    import cups
    
    # 连接CUPS服务
    cups_conn = cups.Connection()
    PRINTER_NAME = "PeriPage_A4"
    
    # 读取输入文本
    input_content = sys.stdin.read()
    # 转成字节并添加走纸指令
    print_data = input_content.encode('utf-8') + b'\x1BJ\x05'
    
    # 发送Raw打印任务
    cups_conn.printData(PRINTER_NAME, print_data, {"print-quality": "raw"})
    
    若Peripage有自定义ESC/POS指令,可查阅官方手册替换走纸或切纸命令。

方法3:调整lpr页边距参数快速测试

若不想修改纸张尺寸,可先尝试禁用CUPS的页边距填充,部分打印机可直接生效:

lorem --faust --lines 20 --randomize | lpr -P "PeriPage_A4" -o orientation-requested=1 -o fit-to-page -o page-top=0 -o page-bottom=0 -o page-left=0 -o page-right=0

内容的提问来源于stack exchange,提问作者Christian

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 03:58:12