6

I have created a .ps file using this command:

man -t man > test.ps

But what I want to do is convert the .ps file to a .pdf file.

If someone could point me in the right direction I would appreciate it.

0

4 Answers 4

6

According to Apple, starting with macOS Ventura, Preview no longer supports PostScript and Encapsulated PostScript.

You can convert PS to PDF using the standard command-line tool pstopdf, which resides in /usr/bin:

pstopdf foo.ps

to produce foo.pdf or

pstopdf foo.ps -o bar.pdf

to produce bar.pdf.

There are no switches for controlling this conversion. In particular, pstopdf seems to output uncompressed PDF 1.3. For other options, see

man pstopdf

If you happen to have MacTeX installed, then it includes other command-line tools based on Ghostscript, which gives you full control on the conversion.

(partially adapted from this question)

0
4

As of macOS 14 (Sonoma), the PostScript interpreter has been removed from the OS. This includes the pstopdf shell command.

You'll need to use third-party software for interpreting PostScript, such as GhostScript.

Alternatively, see this other answer for how to produce PDF directly from man pages, using the built-in mandoc -T pdf command.

How to produce an A4-format pdf of a man page?

0
3

If you find yourself doing this more than once or twice, you may want to create a reusable shell script to automate the procedure. Save the below as /usr/local/bin/man2pdf (or anywhere else in your $PATH) and make it executable:

#!/bin/zsh --no-rcs

case $1 in
  -h|--help|'') echo "usage: ${0##*/} <command> [dest_dir]"; exit;;
esac

if ! hash ps2pdf &>/dev/null; then
cat <<-EOF >&2
\`ps2pdf\` command not found. You can install it using Homebrew:
  brew install ghostscript
EOF
exit 1
fi

if man -t $1 | ps2pdf - /tmp/$1.pdf ; then
  if [[ -d $2 ]] ; then
    mv -i /tmp/$1.pdf "$2"
  else
    open -b com.apple.Preview /tmp/$1.pdf
  fi
else
  echo >&2 "error generating PDF from manpage"
  exit 1
fi

Example usages:

man2pdf diskutil
man2pdf ls ~/Desktop
3
  • This is cool and useful. Would it be possible to modify it to save the PDF (named after the man page) in a specified directory, or the current working directory? Even better would be the option to either open in Preview or save PDF. May 21 at 16:30
  • @JimWorrall I modified my script and added your suggested changes. If you supply a destination dir, the PDF will be quietly saved there, otherwise Preview will be opened from the /tmp dir.
    – luckman212
    May 22 at 11:19
  • Thank you @luckman212. As you can see below I worked on editing it myself as well, but your solution is simpler. May 23 at 16:52
0

I modified @luckman212's nice script to provide the option of directly saving the PDF instead of opening Preview. This is done with an optional second argument specifying path/filename or just filename. I'm not a pro like luckman212 so I'm sure it can be improved.

#!/bin/zsh --no-rcs
# man2pdf converts man page to PDF.
# Takes one or two arguments.  First is the command whose man page you want.
# If no 2nd argument, it opens in Preview.
# 2nd is optional path and file for the PDF produced.
# If the 2nd argument has no path, file will be saved in user's home directory.

case $1 in
  -h|--help|'') echo "usage: ${0##*/} <command> [PDF to save]"; exit;;
esac

if ! hash ps2pdf &>/dev/null ; then
cat <<EOF
\`ps2pdf\` command not found. You can install it using Homebrew:
  brew install ghostscript
EOF
fi

if [ -n "${2+1}" ]; then
    if man -t $1 | ps2pdf - $2; then
        echo "Saved PDF as $2"
    else
        echo 1>&2 "error generating PDF from manpage"
        exit 1
    fi
else
    if man -t $1 | ps2pdf - /tmp/$1.pdf ; then
        open -b com.apple.Preview /tmp/$1.pdf
    else
        echo 1>&2 "error generating PDF from manpage"
        exit 1
    fi
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .