10

I know that man -t ls | open -f -a /Applications/Preview.app produce a pdf of the man page of the ls command.

But the pdf produced is in US Letter size (21.59 cm x 27.94 cm).

The man page of man says that -t option use internally /usr/bin/groff -Tps -mandoc -c.

So, according the groff man, I tried this for obtaining A4 size (21 cm x 29.7 cm) in the pdf (A4 size is widely used in Europe):

man -t ls | /usr/bin/groff -Tps -mandoc -c -P-pa4 | open -f -a /Applications/Preview.app.

I obtain the A4 size as desired, but the formatting is ugly (no bold, italic, characters doubled, underscored)...

How to have the good formatting (as in the first command) AND the right size (as in the later command)?

Update (February 2024)

Apple has dropped the support of the PostScript format, internally used by man -t, with macOS Sonoma (macOS 14). See https://eclecticlight.co/2023/09/25/postscripts-sudden-death-in-sonoma/.

So man -t ls | open -f -a /Applications/Preview.app don't work on macOS Sonoma and after. But in comment, Gilby provide a working alternative.

Below is a proof that man -t create a PostScript file.

With man -t groff > groff.txt, the output of man -t groff is sent to a new file, created with the name groff.txt. The first lines of this file contains:

%!PS-Adobe-3.0
%%DocumentData: Clean7Bit
%%Orientation: Portrait

We recognise at beginning the magic number (see https://en.wikipedia.org/wiki/Magic_number_(programming)#In_files) of a PostScript file.

2
  • Doesn't it "just work" using mandoc?
    – Gilby
    Commented Feb 9 at 10:02
  • @Gilby Thanks a lot. So the straight solution is: mandoc -T pdf -O paper=a4 $(man -w ls) > man-ls.pdf (man -w ls is for obtaining the path to the ls man file). If you write a answer I can accept it.
    – quark67
    Commented Feb 28 at 0:50

3 Answers 3

4
+300

As of macOS 14 Sonoma, mandoc can directly create A4 output, though the default is US Letter. All that is needed is:

mandoc -T pdf -O paper=a4 $(man -w ls) > man-ls.pdf

Regarding paper size man mandoc says:

paper=name
The paper size name may be one of a3, a4, a5, legal, or letter. You may also manually
specify dimensions as NNxNN, width by height in millimetres. If an unknown value is
encountered, letter is used.

To simplify viewing man pages, I use the function pman defined in my .zshrc as:

function pman() { mandoc -T pdf -O paper=a4 "$(man -w $@)" | open -fa Preview }

1
  • Works like a charm !
    – tama
    Commented Feb 28 at 23:03
8

Right now you are running groff twice, once as part of man -t, once on it‘s own on the PostScript file generated by man.

Try

groff -Tps -mandoc -c -P-pa4 /usr/share/man/man1/ls.1 >out.ps
ps2pdf out.ps man-ls.pdf
open man-ls.pdf

or directly

groff -Tps -mandoc -c -P-pa4 /usr/share/man/man1/ls.1 | 
    open -f -a /Applications/Preview.app

To have it work for all man pages accessible to man, a little script will help.

#!/bin/sh
page=$(man -W $1)
if [ -r "$page" ]; then
    groff -Tps -mandoc -c -P-pa4 "$page" | open -f -a Preview.app
fi

Save it as manA4, run chmod +x manA4 once, and then use it as manA4 ls.


In case you are working with compressed man pages (which macOS does not use by default) you need to uncompress them on the fly.

#!/bin/bash
page=$(man -W $1)
if [ -r "$page" ]; then
    if [[ "$page" =~ .*.gz$ ]]; then
        gzcat "$page" | groff -Tps -mandoc -c -P-pa4 | open -f -a Preview.app
    else
        groff -Tps -mandoc -c -P-pa4 "$page" | open -f -a Preview.app
    fi
fi
5
  • This solution does not work with compressed man pages.
    – fd0
    Commented Oct 30, 2017 at 14:45
  • @fd0 I felt tempted to leave this as an exercise to the reader (especially because macOS doesn't work with compressed man pages by default) :-)
    – nohillside
    Commented Oct 30, 2017 at 15:26
  • True, but in man.conf, /usr/local/share/man is mapped which may have compressed man pages.
    – fd0
    Commented Oct 30, 2017 at 15:45
  • Is ps2pdf a command? I do a man ps2pdf and also enter ps2pdf at a prompt and get error messages. Commented Feb 13, 2018 at 14:36
  • @DavidAnderson maybe I installed it through Homebrew or as part of TeX. But it‘s not required anyway, Preview can read and render Postscript files directly.
    – nohillside
    Commented Feb 14, 2018 at 7:41
3

Edit /private/etc/man.conf by commenting out the line

TROFF       /usr/bin/groff -Tps -mandoc -c

then add this line

TROFF       /usr/bin/groff -Tps -mandoc -c -P-pa4

Now man -t will write ps files in A4 paper size.

man -t ls | open .......
1
  • 1
    And don't forget to repeat this after every macOS upgrade...
    – nohillside
    Commented Oct 29, 2017 at 15:06

You must log in to answer this question.

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