The following script will allow you to convert any of the manual pages for any bash command into a PDF document for easy viewing and printing. If you are like me, you may do your best working or thinking sitting some where else besides in front of your computer. A PDF file is at least one format that allows that viewing as a printed document or to be viewed on ebook readers for instance.
This script uses the man device command that allows text to be converted to a Post Script file. Then, the Post Script file is converted to a PDF file. Once the PDF file is created, the *.ps file is removed as it is no longer required. To create man2pdf, copy and paste the following text into your favorite text editor and save it in your home area bin folder (~/bin) as the file man2pdf.
#!/bin/bash
#: Title : man2pdf (Manual Page to PDF File Converter Script)
#: Date Created: Fri Oct 8 21:29:50 CDT 2010
#: Last Edit : Sat Oct 9 09:38:00 CDT 2010
#: Author : J. McDaniel
#: Version : 1.00
#: Description : Convert MAN Page for Command into a PDF File
#: Options : [Page Number] Name of Command , $1 and optional $2
# Created for the openSUSE Forums on Saturday October 9th, 2010
# Copy and paste this text into a file called man2pdf in your home ~/bin folder
# To Make this script executable run the terminal command chmod +x ~/bin/man2pdf
# A Temp file *.ps (Post Script) file is created & then deleted in your document area
# This file is required for ps2pdf to function, but then removed when complete
#
# Where to place your PDF Files, ~/ represents your home area folder
# ~ is automatically replaced with your home folder name, so no need to change.
#
FOLDER=~/Documents
#
# Display Program Name Header Text in Color
#
tput clear
tput setf 7
tput setb 2
tput bold
echo
echo "man2pdf - Convert Online Manuals using man Command to PDF Files"
echo
tput sgr0
#
# make sure we got command-name as command line argument
#
if $# -eq 0 ] ; then
tput setf 7
tput setb 4
tput bold
echo
echo "Terminal Command Syntax is: man2pdf [page] command-name"
tput sgr0
echo
exit 1
fi
#
# Determine if $2 is present. If present, you specified the man page number.
# Create Document Names Based on $1 and $2 if present for man page number.
#
if ! $2 ] ; then
PAGE=""
CMD="$1"
# Full Name of Post Script File to be used
PSFN="$FOLDER""/$CMD""_manual.ps"
# Full Name of PDF File to be used
PDFN="$FOLDER""/$CMD""_manual.pdf"
else
PAGE="$1"
CMD="$2"
# Full Name of Post Script File to be used
PSFN="$FOLDER""/$CMD($PAGE)""_manual.ps"
# Full Name of PDF File to be used
PDFN="$FOLDER""/$CMD($PAGE)""_manual.pdf"
fi
#
# Display Error if PDF File Already Exists & Do not create new PDF File
# Else create PDF Document based on command and page number if supplied.
#
if ! -e $PDFN ] ; then
# Create .ps file from MAN Page
man -Tps $PAGE $CMD >> $PSFN
# Convert .ps file to pdf File
ps2pdf $PSFN $PDFN
# Remove .ps file when done
rm $PSFN
tput setf 7
tput setb 1
tput bold
echo $PDFN " File Has Been Created For You..."
tput sgr0
echo
else
tput setf 7
tput setb 4
tput bold
echo $PDFN " File Already Exists..."
tput sgr0
echo
fi
exit 0
# End Of Script
Once you have saved this text as the file man2pdf in your ~/bin folder, run the following terminal command to make the file executable:
chmod +x ~/bin/man2pdf
To use man2pdf, open a terminal session. The command syntax to use man2pdf is as follows:
man2pdg [page] command-name
You may want to use the standard man command first to make sure you know that the manual exists and what your page options are. For instance, if I were to type the command:
james@LinuxUser:~> man mount
Man: find all matching manual pages (set MAN_POSIXLY_CORRECT to avoid this)
* mount (8)
mount (2)
Man: What manual page do you want?
Man:
I am asked for a page number and page 8 is the default if no number is supplied. I could supply that page number by using the terminal command:
man 2 mount
This would provide the man page 2 for the mount command. Using man2pdf, you can convert page 2 to a PDF file with the terminal command:
man2pdf 2 mount
The pdf document will have (2) added to the name. If you do not supply a page number, man2pdf will just use the default and will not ask for a number. Pages can be much longer that a single page. For instance, if you use the command:
man2pdf bash
You will end up with a 70 page PDF file. If you should try to create the same document a second time, man2pdf will not create another PDF file and will issue a warning that the PDF file name already exists.
If you have any questions about using man2pdf, just let me know.
Thank You,