DEV Community

Stephano Kambeta
Stephano Kambeta

Posted on

Automating File Conversion (PDF ↔ Image) via Termux

If you work on mobile and need to convert PDFs to images or images to a PDF quickly, Termux is the easiest, most flexible tool you can carry in your pocket. This guide walks you through installing the right packages, writing a small automation script, and running it on your phone so conversions happen with one command. I keep things simple and practical so you can follow along even if you are new to Termux.

Along the way I link to related posts from the blog so you can expand your setup, secure your workflow, or try quick Termux projects. If you have Termux installed, skip to the install commands. If not, check the Termux install guide first.

How to install Termux


Why automate file conversion on a phone?

  • Batch work: convert dozens of pages into images or bundle multiple photos into a single PDF.
  • Mobility: no laptop, no problem. Do everything on Android with Termux.
  • Integration: convert files automatically when they land in a folder or when you share them to Termux.
  • Privacy: process files locally instead of uploading to unknown cloud services. If you do need cloud upload, consider using a VPN for extra privacy like in this VPN review and list of recommended VPNs for Termux.

What you will learn

  1. Install the packages needed for converting PDFs and images.
  2. Use command line tools to convert PDF→images and images→PDF.
  3. Create a reusable bash script to automate conversions.
  4. Schedule or trigger conversions with Termux add-ons or cron-like tools.
  5. Tips for handling common problems and keeping things secure.

Prerequisites

  • Termux installed and storage permission granted.
  • Basic comfort with the command line.
  • Enough free space for temporary images (PDF pages generate images which can be large).

If you want small sample projects to try after this one, see quick Termux projects.


Install required packages

Open Termux and run these commands. They install poppler-utils (for pdftoppm), ImageMagick (convert), and img2pdf which makes reliable PDFs from images.

pkg update && pkg upgrade -y
pkg install poppler imagemagick python git
pip install img2pdf

Notes:

  • pdftoppm (from poppler) converts PDF pages to PNG/JPEG.
  • convert (ImageMagick) can manipulate images and also create PDFs, but it may change image compression. I prefer img2pdf for lossless image-to-PDF packing.

Commands you will use

Task Command Notes
PDF → PNGs pdftoppm -png input.pdf output_prefix Produces files like output_prefix-1.png
PDF → JPEGs pdftoppm -jpeg input.pdf output_prefix JPEGs are smaller but lossy
Images → PDF img2pdf *.png -o out.pdf Maintains original image quality and order
Resize or compress convert input.png -resize 1240x1748\> output.png Useful for limiting file size

Make a reusable script

Create a folder for your scripts, then create the main conversion script. This example supports two modes: pdf2img and img2pdf. Save the file as convert.sh in $HOME/bin or any folder in your PATH.

#!/bin/bash
# Usage: convert.sh pdf2img input.pdf outprefix
#        convert.sh img2pdf out.pdf img1.png img2.png ...

set -e

mode="$1"
shift

if [ "$mode" = "pdf2img" ]; then
  infile="$1"
  outprefix="${2:-page}"
  mkdir -p tmp_conv
  pdftoppm -png "$infile" "tmp_conv/$outprefix"
  echo "Saved images to tmp_conv/"
elif [ "$mode" = "img2pdf" ]; then
  outfile="$1"
  shift
  # pack remaining args as images
  img2pdf "$@" -o "$outfile"
  echo "Created $outfile"
else
  echo "Usage: $0 pdf2img input.pdf outprefix"
  echo "       $0 img2pdf out.pdf img1.png img2.png ..."
  exit 1
fi

Make it executable:

chmod +x ~/bin/convert.sh

Example runs:

# PDF to images
convert.sh pdf2img ~/downloads/ebook.pdf ebook_page

# Images to PDF

convert.sh img2pdf ~/downloads/album.pdf photo1.jpg photo2.jpg photo3.jpg

Automate with Termux:Tasker or Termux:Boot

There are several ways to trigger the script automatically.

  • Termux:Boot runs scripts at boot. Good for persistent watchers.
  • Termux:Tasker integrates with Tasker so you can run conversions when a folder gets new files or when you share files to Termux.
  • Simple loop watcher a small bash loop checks a folder for new PDFs and runs conversion. This is crude but works for personal automations.

Sample watcher script that converts any new PDF in $HOME/watch to images:

#!/bin/bash
watchdir="$HOME/watch"
processed="$watchdir/processed"
mkdir -p "$watchdir" "$processed"

inotifywait -m -e create --format '%f' "$watchdir" | while read file; do
  if [[ "$file" == *.pdf ]]; then
    echo "New PDF: $file"
    ~/bin/convert.sh pdf2img "$watchdir/$file" "${file%.*}"
    mv "$watchdir/$file" "$processed/"
    echo "Processed $file"
  fi
done

To use inotifywait you may need the inotify-tools package. If unavailable in your Termux repo, use a timed loop while true; do sleep 10; ....

For a tasker setup where you share a file from Android's share menu directly to Termux, check out guides like the cron and automation posts on the blog for similar wiring examples: ngrok and services and running a simple server.


Practical examples and tips

Make a small OCR workflow

If you want searchable PDFs, convert pages to images, run OCR, then pack pages into a PDF. Use tesseract for OCR.

pkg install tesseract
pdftoppm -png input.pdf page
for f in page-*.png; do
  tesseract "$f" "${f%.*}" pdf
done

Tesseract can output searchable PDFs. That is useful when you need text extraction for search or archiving.

Compressing final PDF

Image-heavy PDFs can be large. Resize source images before packing, or use ImageMagick to reduce DPI. Example resize step:

for img in tmp_conv/*.png; do
  convert "$img" -resize 1240x1748\> "$img"
done
img2pdf tmp_conv/*.png -o reduced.pdf

When to use ImageMagick vs img2pdf

  • Use img2pdf when you want exact image packing without recompression.
  • Use convert if you must resize or change color profiles.

Security and operational advice

  • Keep your Termux packages updated. See post-install checklist for general maintenance tips.
  • Avoid sending sensitive files to random cloud converters. If you must upload, use a trusted VPN or a trusted service. Review VPN options in the blog post linked earlier.
  • Make backups of originals before running batch conversions. A simple backup step in your script can save headaches.
  • If your automation touches production data for a business, treat it like any other IT process. See posts on cyber security plans for small business and network security tips to add proper controls.

Common problems and fixes

Problem Fix
Output images too large Resize before packing with ImageMagick or use JPEG output mode: pdftoppm -jpeg input.pdf out
Fonts missing in PDF when using convert Use img2pdf after converting pages to images so fonts are preserved as raster images
Script not executable Run chmod +x script.sh and ensure Termux storage permissions are granted

Where this fits in a larger workflow

Automated file conversion is one piece in mobile workflows. You might convert receipts to searchable PDFs and then upload them to cloud storage, or convert a client's PDF into image slides for quick review. If you need more advanced automation—like web posting, alerts, or integrating with other tools—check related project guides such as quick Termux projects and how to turn your device into a mini server with nginx in Termux.

Also see posts about incident response and threat awareness so you keep automation safe when used in business contexts: incident response and cyber security for small companies.


Wrap up

Converting files on Android via Termux is fast, private, and highly automatable. With just a few packages and a small script you can build a reliable converter, tie it to a watcher or Tasker, and keep everything on-device. Start with the commands in this guide, test with a few files, then grow your script to match your own needs.

If you want a guided walkthrough tailored to your device and files, I can write a customized script that handles your specific file types and storage layout. Or if you want to explore connecting this workflow to an offline OCR or notification system, I can add that too. For more advanced Termux automation patterns, check: automation examples and network tricks.

Top comments (0)