Converting Bash ‘man’ to PDF

Here’s a Bash script to convert Bash man pages to PDF files:

#!/bin/bash

function generate
{
    item=$1
    manpath="`man -w ${item}`"
    psfile="${item}.man.ps"
    pdffile="${item}.man.pdf"

    manfile="${manpath##*/}"
    manbase="${manfile%%.*}"
    manext_with_num="${manfile:$[${#manbase}]}"
    manext="${manext_with_num##.[1-9]}"

    echo

    if [ "${manpath}" == "" ]
    then
        echo "Aborting."
        return
    else
        echo "Man page file = ${manpath}"
    fi

    #echo "[${manext}]"
    if [ "${manext}" == ".gz" ]
    then
        echo "Processing GZIP format..."
        data="`zcat "${manpath}"`"
    elif [ "${manext}" == "" ]
    then
        echo "Processing PLAIN format..."
        data="`cat "${manpath}"`"
    else
        echo "Unknown format: [${manext}]"
        exit 1
    fi

    echo "Formatting..."
    echo "$data" | groff -man - | ps2pdf - - > ${pdffile}

    #ls -l ${psfile}
    ls -l ${pdffile}
}

for item in $@
do
    generate ${item}
done

And here’s a related, more basic script to extract the text from info pages:

#!/bin/bash

item=$1
infopath="`info -w ${item}`"
infofile="${infopath##*/}"
infobase="${infofile%%.*}"

#txtfile="${infobase}.info[${item}].txt"
txtfile="${infobase}.info.txt"

zcat "${infopath}" > "${txtfile}"
ls -l "${txtfile}"
Advertisements

2 Responses to “Converting Bash ‘man’ to PDF”

  1. NicoLarve Says:

    Isn’t this more simple:

    man -t bash | ps2pdf – > bash.pdf

  2. NicoLarve Says:

    I realize that it’s been the second time I search things, found answers on your blog!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: