DEV Community

Convertica
Convertica

Posted on

Automating the PDF Conversions You Keep Doing by Hand

Most PDF work is a one-off: a file lands, you convert it, you move on. But some of it repeats. A folder of scanned invoices every Monday. Statements that need to become spreadsheets before month-end. Reports that arrive as PDF and have to go out as Word. If you are doing the same conversion by hand every week, that is a job for a script, not a person.

Here is how I moved those repeat conversions off my plate.

First, the ten-second check that saves you

Before automating anything, know which kind of PDF you are feeding the pipeline. Select a word in the file. If it highlights, the text is real and conversion is clean and deterministic. If nothing highlights, the page is an image, and you need OCR in the loop, which is still fine but adds a recognition step you should spot-check. Automations break most often because someone assumed every input was a native PDF when half of them were scans.

Wrapping a converter in a script

The pattern is boring in the best way: watch a folder or a queue, POST each file to a conversion endpoint, write the result somewhere. I use Convertica for the conversion step because it has a free API and does not require an account, so there is no OAuth dance to script around. A single conversion is just a POST with the file:

curl -X POST https://convertica.net/api/v1/pdf-to-word/ \
  -F "file=@invoice.pdf" \
  -o invoice.docx
Enter fullscreen mode Exit fullscreen mode

Drop that into a loop and a cron entry and the Monday-invoices chore runs itself:

#!/usr/bin/env bash
set -euo pipefail
IN=./incoming
OUT=./converted
mkdir -p "$OUT" "$IN/done"
for f in "$IN"/*.pdf; do
  name=$(basename "${f%.pdf}")
  curl -fsS -X POST https://convertica.net/api/v1/pdf-to-excel/ \
    -F "file=@${f}" -o "${OUT}/${name}.xlsx" \
    && mv "$f" "${IN}/done/"
done
Enter fullscreen mode Exit fullscreen mode
# every Monday at 6am
0 6 * * 1 /home/me/scripts/convert-invoices.sh >> /var/log/convert.log 2>&1
Enter fullscreen mode Exit fullscreen mode

The set -euo pipefail and curl -fsS matter more than they look. Without them a failed conversion writes a zero-byte file, your mv marks it done, and you lose the input silently. Fail loudly instead.

The parts people skip

  • Idempotency. Move processed files out of the input folder (or track them) so a re-run does not convert the same file twice.
  • Scanned inputs. If the batch might contain scans, expect OCR and add a sanity check on the output. A zero-length or suspiciously tiny result usually means a bad scan.
  • Cleanup. Whatever service you use, know its retention. Convertica deletes uploads from the server after a short window, which is what you want for statements and contracts rather than throwaway samples.
  • Secrets stay home. For genuinely confidential documents I generate the file from source rather than round-tripping it through any external service, mine included. Automation is for the routine stuff, not the crown jewels.

Do not forget the passwords

One thing that quietly breaks batch jobs: a password-protected PDF in the input. There are two kinds, and they fail differently. An owner-password file (restrictions only) usually still converts. An open-password file is encrypted and will not convert without the password, and no tool bypasses real encryption, so your script should catch that case and set the file aside for a human rather than retrying forever.

The point

The work has not changed. Files still need to be smaller, converted, and machine-readable. What changed is that the repetitive slice of it belongs in a five-line script and a cron entry. Spend the ten seconds to check text-versus-scan, fail loudly, respect retention, and the Monday chore stops being a chore.

Top comments (0)