man2pdf - Command Manual Page to PDF File Converter Script

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,

On 2010-10-09 17:36, jdmcdaniel3 wrote:
>
> 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.

Interesting.

However… :slight_smile:

<http://en.wikipedia.org/wiki/Man_page#Writing_man_pages>

]> Besides viewing manual pages online (with man), man pages may also readily be converted to PDF
format for immediate printing. In Mac OS X and GNU/Linux (with command replaced by the name of the
desired command):


groff -mandoc command.1 >command.ps
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=command.pdf command.ps

]> The former command exports the manual page to PostScript format, while the latter then converts
the resulting PostScript file to PDF. (The above works for both man- and mdoc-formatted manual
pages.) In some cases, the hardcopy version of the manual page may be more readable if printed using
a monospaced font; to do so, add the option -f C after the -mandoc option in the groff command above.

If the intention is to print the file, just print the postscript file. The linux printing system has
to convert anything to postscript, then sends the postscript to the print system - directly if the
printer supports ps, or via a converter if it doesn’t. If you convert to pdf, you are in fact
converting and de-converting later.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

Carlos E. R., there is always more than one way to skin a cat as they say. Perhaps our many users do not yet know all of those ways to view and print man pages. The PDF format allows you to come back at anytime for viewing and printing without having to recall how it was produced.

As an alternate suggestion, when you are online, you have yet another way to do so while using openSUSE.

Thanks for your comments and suggestions Carlos.

Thank You,