]> arthur.barton.de Git - pdfman.git/commitdiff
Add support for ps2pdf(1) command of the Ghostscript suite
authorAlexander Barton <alex@barton.de>
Wed, 1 Nov 2023 19:44:24 +0000 (20:44 +0100)
committerAlexander Barton <alex@barton.de>
Wed, 1 Nov 2023 19:44:24 +0000 (20:44 +0100)
This not only makes this script useful on Linux and BSD systems but is
important on current macOS systems as well, as Apple dropped PostScript
support from macOS starting with version 14, "Sonoma", and no longer
ships a `pstopdf` command with the system!

README.md
pdfman

index 92e3ff6a2fed84f34522214cd048de0668c62346..f37cbaf2e647001c79ef6daf14042fc23d9e3a29 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,9 +1,10 @@
-# pdfman: View Manual Pages as Portable Document Format (PDF) files
+# pdfman: View UNIX manual pages as Portable Document Format (PDF) files
 
-`pdfman` converts UNIX manual pages to PDF using the `pstopdf(1)` command and
-displays them by calling `open(1)` on the resulting PDF file. This works very
-good on macOS and enhances the useability of the manual pages quite a bit
-because you can use your preferred PDF reader.
+`pdfman` converts UNIX manual pages to PDF documents using Ghostscript's
+`ps2pdf`(1) or Apple's `pstopdf`(1) command (included in macOS up to version 13
+"Ventura") and displays them by calling `open`(1) on the resulting PDF file.
+This enhances the useability of the manual pages quite a bit, because you now
+can make use of your preferred PDF file viewer and all of its functionality.
 
 The conversion is only done once and a cached PDF file gets used for subsequent
 invocations for the same topic.
diff --git a/pdfman b/pdfman
index b0c7a57d7aa31a04869d93d66962c064f721f514..e68d4405e8ef4085e4bee7dc74d7e6561ccba97a 100755 (executable)
--- a/pdfman
+++ b/pdfman
@@ -1,8 +1,8 @@
 #!/bin/bash
 # shellcheck disable=SC2250
 #
-# pdfman - View Manual Pages as PDF Files, using pstopdf(1) and open(1)
-# Copyright (c)2010,2013,2016,2019 Barton IT-Consulting, Alexander Barton
+# pdfman - View UNIX manual pages as Portable Document Format (PDF) files
+# Copyright (c)2010,2013,2016,2019,2023 Barton IT-Consulting, Alexander Barton
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -52,6 +52,16 @@ while [[ $# -gt 0 ]]; do
        shift
 done
 
+run_ps2pdf() {
+       # Ghostscript ps2pdf(1)
+       ps2pdf - "$1"
+}
+
+run_pstopdf() {
+       # Apple pstopdf(1)
+       pstopdf -i -o "$1" >/dev/null
+}
+
 # Manual page name(s) given?
 if [[ -z "$1" ]]; then
        # Give man's wtf error ("help message"):
@@ -65,10 +75,21 @@ if [[ ! -t 1 ]]; then
        exit $?
 fi
 # Make sure required tools are available ...
-if ! command -v open >/dev/null || ! command -v pstopdf >/dev/null; then
+if ! command -v open >/dev/null; then
+       man "$@"
+       exit $?
+fi
+
+# Detect PS-to-PDF converter to use ...
+if command -v ps2pdf >/dev/null; then
+       ps_to_pdf_function=run_ps2pdf
+elif command -v pstopdf >/dev/null; then
+       ps_to_pdf_function=run_pstopdf
+else
        man "$@"
        exit $?
 fi
+[[ -n "$VERBOSE" ]] && echo "Using ${ps_to_pdf_function#*_}(1) ..."
 
 # Try to move old cache directory ...
 if [[ -d "$HOME/.pdfman" && ! -d "$CACHE" ]]; then
@@ -99,8 +120,7 @@ man -w "$@" | while read -r MANFILE; do
        if [[ ! -r "$PDF" ]]; then
                mkdir -p "$CACHE/$SECTION"
                [[ -n "$VERBOSE" ]] && echo "Converting \"$MANFILE\" to \"$PDF\" ..."
-               # shellcheck disable=SC2312,SC2086
-               if ! man -t "$MANFILE" | pstopdf $VERBOSE -i -o "$PDF" >/dev/null; then
+               if ! man -t "$MANFILE" | $ps_to_pdf_function "$PDF"; then
                        echo "Failed to convert manual page to PDF!" >&2
                        exit 1
                fi