Skip to main content

Making PDFs editable

I automate everything and PDFs are so easy to fill in automatically. It’s a digital world and we just live in it.

It’s so frustrating to get a “protected form” that doesn’t allow for appending documents, automated form completion or even filling out fields. It’s silly because all of these forms are printable. Many years ago, I just created an official form from scratch myself because I couldn’t find it online. Without checking a signature, forms can’t be secure. For example, one could easily write a program that takes a picture and recreates the form. Remember, in the digital world something is either secure from math (crypto) or it isn’t. The security in the transaction is provided by authenticating the user’s email. Even better, let’s move past forms and use the authentication in a website or mobile app.

Let’s do the right thing and move past forms caught in the middle ground of insecure and not usable. I think folks lock forms to prevent changes to the document, don’t do that.

Below is the code to fix this. You will need to use linux or windows subsystem for linux. While I can’t think of an evil use case, use this responsibly. (My goal in doing this is always to fill in data into a form.)

Use a little bash to print with ghostscript. Protip: this isn’t “hacking”, this is just printing.

filename=$1
outfile="${filename%.*}_clean.pdf"
echo $outfile
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outfile -c .setpdfwrite -f $1

Author: Tim Booher

Feedback

2 comments on “Making PDFs editable

  1. file=”myfile.pdf”; gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=”${file%.*}_clean.pdf” “$file”

  2. #!/usr/bin/env zsh
    # unlock-pdf — re-writes a PDF to strip protections (using Ghostscript)

    set -euo pipefail

    function usage() {
    cat <<EOF
    Usage: $(basename $0) input.pdf [output.pdf]

    If output file is omitted, it writes to _clean.pdf

    Examples:
    $(basename $0) secret.pdf
    $(basename $0) secret.pdf secret_unlocked.pdf
    EOF
    exit 1
    }

    if (( $# 2 )); then
    usage
    fi

    local input=”$1″
    if [[ ! -f “$input” ]]; then
    echo “Error: input file ‘$input’ not found” >&2
    exit 1
    fi

    local output
    if (( $# == 2 )); then
    output=”$2″
    else
    local base=”${input%.*}”
    output=”${base}_clean.pdf”
    fi

    echo “Unlocking → $output”

    # Ghostscript command
    gs -q \
    -dNOPAUSE -dBATCH -dSAFER \
    -sDEVICE=pdfwrite \
    -dCompatibilityLevel=1.5 \
    -dPDFSETTINGS=/default \
    -sOutputFile=”$output” \
    “$input”

    echo “Done: $output”%

Responses to tim

Required fields are marked *

Click here to cancel reply.