My Computer Tips

Home | About | Categories | All Tips & Tutorials

Export man pages in .html or .pdf format by installing mandoc on Ubuntu and Ubuntu based distributions

ID: 311

Category: Linux Terminal

Added: 9th of July 2022

Views: 1,643

To access the man page for a specific linux command or application, you would enter the following command in the terminal. In the example below I am requesting the manual page for the ls command

man ls


The manual page is then displayed in the terminal window.

On Ubuntu and Ubuntu based distributions, man pages (Manual pages) are located in the following directory.
/usr/share/man/man1/


You might like a permanent copy of a man page for reference at later stage. You can copy and paste the information directly to a text file from the terminal, but it will be unformatted.

Another alternative is to install the mandoc package, which has a function to export the man page in .html or .pdf format.

To install mandoc enter the following command in the terminal
sudo apt-get install mandoc


Once installed issue the following command to export the ls man page in .html format
mandoc -Thtml /usr/share/man/man1/ls.1.gz > ls.html


Or issue the following command to export the ls man page .pdf format
mandoc -Tpdf /usr/share/man/man1/ls.1.gz > ls.pdf


Instead of entering the command each time, I put together a small bash script that searches the man doc directory and then exports the man page in .html and .pdf formats.

Create a folder and file on your desktop using the following commands.
Replace username with your own username
mkdir /home/username/Desktop/manhtmlpdf

touch /home/username/Desktop/manhtmlpdf/manhtmlpdf.sh


Copy and paste the code below, and save in the manhtmlpdf.sh file you created above.
#!/usr/bin/bash
echo "*** Create HTML/PDF copy of man document ***"

echo "What man page are you looking for?"
read manpage

if [ -f "/usr/share/man/man1/$manpage.1.gz" ]
then
echo "Creating $manpage.html and $manpage.pdf files"
mandoc -Thtml /usr/share/man/man1/$manpage.1.gz > $manpage.html
mandoc -Tpdf /usr/share/man/man1/$manpage.1.gz > $manpage.pdf
else
echo "The man page for $manpage was not found"
fi


Make the file executable by entering the following command in the terminal
chmod +x manhtmlpdf.sh


Run the script by entering the following command
./manhtmlpdf.sh

Related Tips & Tutorials

How to access linux man pages and output specific manual page to a .txt file