如何将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:
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
piuser to thelpadmingroup 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 withsudo nano /etc/cups/cupsd.conf:- Change
Listen localhost:631toListen 0.0.0.0:631(lets all devices on the network access CUPS) - In the
<Location />,<Location /admin>, and<Location /admin/conf>blocks, replaceAllow localhostwithAllow @LOCAL(grants access to your local network) - Save and exit, then restart CUPS:
sudo systemctl restart cups
- Change
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/):
Then editsudo mkdir -p /opt/print-jobs && sudo chown lp:lp /opt/print-jobssudo nano /etc/cups/cups-pdf.confand setOut ${HOME}/PDFtoOut /opt/print-jobs - Now set up the virtual printer via the CUPS web interface:
Open a browser and go tohttp://<your-pi-ip>:631, log in with yourpiusername/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
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
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
inotifywaitto 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:
Enable and start the 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.targetsudo 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.
- 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
pihas read/write access) and thatinotifywaitis installed correctly
内容的提问来源于stack exchange,提问作者moboldi

