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

如何将Raspberry Pi配置为打印机,接收收银机打印任务并保存文件以供导入数据库?

Got it, let's walk through exactly how to turn your Raspberry Pi into a virtual printer that captures print jobs from your cash register, saves them for later processing, and fully replaces your physical printer—no more wasted paper. Here's a step-by-step guide tailored to your use case:

1. Set Up the Core Printing Service (CUPS)

CUPS is the standard Linux printing framework, and it's perfect for handling network print jobs from your cash register. Let's get it configured:

  • First, update your Pi's system packages:
    sudo apt update && sudo apt upgrade -y
    
  • Install CUPS:
    sudo apt install cups -y
    
  • Add your pi user to the lpadmin group so you can manage printers via the web interface:
    sudo usermod -aG lpadmin pi
    
  • Start and enable the CUPS service to run on boot:
    sudo systemctl enable --now cups
    
  • Allow network access to CUPS (since your cash register needs to connect to it):
    Edit the CUPS config file with sudo nano /etc/cups/cupsd.conf:
    • Change Listen localhost:631 to Listen 0.0.0.0:631 (lets all devices on the network access CUPS)
    • In the <Location />, <Location /admin>, and <Location /admin/conf> blocks, replace Allow localhost with Allow @LOCAL (grants access to your local network)
    • Save and exit, then restart CUPS:
      sudo systemctl restart cups
      
2. Create a Virtual Printer That Saves Files

We'll use the cups-pdf package to convert print jobs into PDF files (you can adjust this to save text if needed later):

  • Install cups-pdf:
    sudo apt install cups-pdf -y
    
  • By default, PDFs save to /home/pi/PDF/. To change this to a more organized directory (like /opt/print-jobs/):
    sudo mkdir -p /opt/print-jobs && sudo chown lp:lp /opt/print-jobs
    
    Then edit sudo nano /etc/cups/cups-pdf.conf and set Out ${HOME}/PDF to Out /opt/print-jobs
  • Now set up the virtual printer via the CUPS web interface:
    Open a browser and go to http://<your-pi-ip>:631, log in with your pi username/password
    • Click Administration > Add Printer
    • Under Local Printers, select CUPS-PDF (Virtual PDF Printer)
    • Name it something clear (like CashRegister_VirtualPrinter), set it as the default printer, and choose the Generic PDF Printer driver when prompted
  • Test it: Send a print job from your cash register, then check your target directory—you should see a new PDF file there. If your cash register uses LPD instead of IPP, enable the LPD service with sudo systemctl enable --now cups-lpd
3. Make It Seamlessly Replace Your Physical Printer

To ensure your cash register doesn't notice the difference:

  • If your old printer was network-connected: Just update the cash register's printer settings to point to http://<your-pi-ip>:631/printers/CashRegister_VirtualPrinter
  • If your old printer was USB-only: You can simulate a USB printer with tools like usbip (more complex, but doable)—but network printing is far simpler if your cash register supports it. Confirm your cash register's network print settings and input your Pi's IP/printer name there.
  • Verify connectivity: Ping your Pi from the cash register to ensure they're on the same network, and check that ports 631 (IPP) and 515 (LPD) are open with:
    sudo ufw allow 631/tcp && sudo ufw allow 515/tcp
    
4. Automate File Processing for Your App

Now we'll set up a system to watch for new print files, extract data, and save it to your database:

  • First, install tools to extract text from PDFs (we'll use pdftotext):
    sudo apt install poppler-utils -y
    
  • Use inotifywait to monitor your print directory for new files (install it first):
    sudo apt install inotify-tools -y
    
  • Create a shell script to handle new files (save as /opt/print-processor/monitor_jobs.sh):
    #!/bin/bash
    MONITOR_DIR="/opt/print-jobs"
    ARCHIVE_DIR="/opt/print-jobs/archived"
    mkdir -p "$ARCHIVE_DIR"
    
    inotifywait -m -e create "$MONITOR_DIR" | while read dir action file; do
      if [[ "$file" == *.pdf ]]; then
        # Convert PDF to text for easier parsing
        pdftotext "$dir$file" "$dir${file%.pdf}.txt"
        # Call your app's parsing script (replace with your app's command)
        python3 /opt/print-processor/parse_and_save.py "$dir${file%.pdf}.txt"
        # Archive the files to avoid reprocessing
        mv "$dir$file" "$ARCHIVE_DIR/"
        mv "$dir${file%.pdf}.txt" "$ARCHIVE_DIR/"
      fi
    done
    
  • Make the script executable:
    chmod +x /opt/print-processor/monitor_jobs.sh
    
  • Set it to run on boot with a systemd service:
    Create /etc/systemd/system/print-processor.service:
    [Unit]
    Description=Process incoming cash register print jobs
    After=cups.service
    
    [Service]
    User=pi
    ExecStart=/opt/print-processor/monitor_jobs.sh
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    Enable and start the service:
    sudo systemctl enable --now print-processor.service
    
  • For your app: Write a parsing script (in your preferred language) that reads the text file, extracts data (like transaction amounts, item names, timestamps) using regex or string matching, and inserts it into your database.
5. Quick Troubleshooting Tips
  • If the cash register can't find the printer: Double-check CUPS network settings and firewall rules, and confirm your Pi's IP is static (so it doesn't change after reboot)
  • If PDFs are empty: Check the CUPS Jobs page for errors, and make sure you selected the correct generic PDF driver
  • If the monitoring script doesn't trigger: Verify directory permissions (ensure pi has read/write access) and that inotifywait is installed correctly

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 12:32:41