Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Setting Up CUPS Printing on Ubuntu 18.04 and Android PDF Print Integration

Tech 1

Installing CUPS on Ubuntu 18.04

Begin by updating the system and installing CUPS along with essential dependencies:

sudo apt update
sudo apt install cups foomatic-filters ghostscript

Restart the CUPS service to apply changes:

sudo systemctl restart cups

Enable and start the service if not already running:

sudo systemctl enable --now cups

Access the CUPS web interface at http://localhost:631 for GUI-based configuration.

If encountering D-Bus errors:

sudo systemctl start dbus

Printer Setup via CLI

Add a network printer using IPP:

lpadmin -p Epson_L3251 -E -v ipp://11.22.33.44/ipp/print -m everywhere
lpadmin -p HP_M104W -E -v ipp://11.22.33.44/ipp/print -m everywhere

Set a default printer:

lpoptions -d Epson_L3251

Print a PDF file:

lp -o media=A4 document.pdf

Check printer status:

lpstat -p

Remove a printer:

lpadmin -x Epson_L3251

Locate PPD files:

lpinfo -m | grep -i epson
find /etc/cups/ppd -name "*Epson*"

Android PDF Printing via Vendor Plugins

Instead of embedding print logic, leverage manufacturer-provided print services:

  • HP: com.hp.android.printservice
  • Epson: Epson iPrint
  • Canon: Canon PRINT Business
  • Samsung: Samsung Mobile Print

Use Android’s share intent to delegate printing:

private fun printPdf(pdfPath: String) {
    val file = File(pdfPath)
    if (!file.exists()) {
        Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show()
        return
    }

    val uri = Uri.fromFile(file)
    val intent = Intent(Intent.ACTION_SEND).apply {
        type = "application/pdf"
        putExtra(Intent.EXTRA_STREAM, uri)
    }
    startActivity(Intent.createChooser(intent, "Print PDF"))
}

Cross-Compiling CUPS for Android (Advanced)

While possible, compiling CUPS for Android is complex and generally discouraged unless absolutely necessary.

Prerequisites

  1. Install Android NDK (e.g., r21e).
  2. Create a standalone toolchain:
$NDK/build/tools/make_standalone_toolchain.py \
  --arch arm --api 24 --install-dir /opt/toolchain

Set environment variables:

export PATH=/opt/toolchain/bin:$PATH
export CC=armv7a-linux-androideabi24-clang
export CXX=armv7a-linux-androideabi24-clang++

Patching and Building CUPS

CUPS requires modifications for Android compatibility:

  • Disable D-Bus, PAM, and TLS.
  • Replace missing nl_langinfo implementation (from AOSP sed source).
  • Stub out password/group database functions (getpwuid, endpwent, etc.).
  • Comment out pthread_cancel usage in threading code.

Example configure command:

./configure \
  --host=arm-linux-androideabi \
  --disable-dbus \
  --disable-pam \
  --without-tls \
  --prefix=/system/usr/root \
  --with-cups-user=system \
  --with-cups-group=system

Apply necessary patches before building:

make
make install DESTDIR=/output

Deployment on Android

After compilation:

  1. Push binaries to /system/usr/root/{bin,sbin}.
  2. Copy libraries to /system/lib.
  3. Place config files in /system/usr/root/etc/cups/.
  4. Set permissions:
chmod 755 /system/usr/root -R
chmod 777 /system/usr/root/var -R

Configure cupsd.conf to allow network access:

<Location />
  Order allow,deny
  Allow all
</Location>

Launch CUPS daemon:

export LD_LIBRARY_PATH=/system/usr/root/lib
export PATH=/system/usr/root/bin:/system/usr/root/sbin:$PATH
cupsd -f &

Verify operation by accessing http://localhost:631 via a mobile browser.

Common Issues

  • "Unsupported document-format": Requires cups-filters for PDF rendering.
  • Backend crashes: Ensure libcups.so.2 is in LD_LIBRARY_PATH.
  • PPD creation failure: Confirm printer supports IPP Everywhere via:
ipptool -tv ipp://PRINTER_IP/ipp/print get-printer-attributes.test

Note: Full PDF printing on Android typically requires additional components like cups-filters, qpdf, and image libraries (Poppler, LCMS2, etc.), which must also be cross-compiled and deployed.

Tags: CUPS

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.