pts / pdfsizeopt

PDF file size optimizer
GNU General Public License v2.0
762 stars 66 forks source link

MacOS installation procedure did not work #141

Closed baldvin-kovacs closed 1 year ago

baldvin-kovacs commented 4 years ago

I executed the MacOS install procedure, and the test failed:

RuntimeError: Ghostscript does not seem to work: TMPDIR=/var/folders/4g/_1lfx5z12ng9xv9tt0ff6ptw002r9l/T TEMP=/var/folders/4g/_1lfx5z12ng9xv9tt0ff6ptw002r9l/T gs

[~/pdfsizeopt]?1$ find . -name gs
./pdfsizeopt_libexec/pdfsizeopt_gs/gs
[~/pdfsizeopt]$ file ./pdfsizeopt_libexec/pdfsizeopt_gs/gs
./pdfsizeopt_libexec/pdfsizeopt_gs/gs: Mach-O executable i386
[~/pdfsizeopt]$ file /usr/bin/passwd
/usr/bin/passwd: Mach-O 64-bit executable x86_64
[~/pdfsizeopt]$ ./pdfsizeopt_libexec/pdfsizeopt_gs/gs
zsh: bad CPU type in executable: ./pdfsizeopt_libexec/pdfsizeopt_gs/gs
byronwall commented 4 years ago

I got the same error initially. I was able to get one step further by installing Ghostscript:

brew install gs

That just moved the goalpost to a different Ghostscript related error.

I ended up using a simple bash script to shrink some PDFs. It's far inferior to pdfsizeopt for image compression, but works good enough until I sort out the MacOS issues.

#!/bin/bash
#
# optpdf file.pdf
#   This script will attempt to optimize the given pdf

file="$1"
filebase="$(basename "$file" .pdf)"
optfile="/tmp/$$-${filebase}_opt.pdf"
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH \
        -sOutputFile="${optfile}" "${file}"

if [ $? == '0' ]; then
    optsize=$(stat -f "%z" "${optfile}")
    orgsize=$(stat -f "%z" "${file}")
    if [ "${optsize}" -eq 0 ]; then
        echo "No output!  Keeping original"
        rm -f "${optfile}"
        exit;
    fi
    if [ ${optsize} -ge ${orgsize} ]; then
        echo "Didn't make it smaller! Keeping original"
        rm -f "${optfile}"
        exit;
    fi
    bytesSaved=$(expr $orgsize - $optsize)
    percent=$(expr $optsize '*' 100 / $orgsize)
    echo Saving $bytesSaved bytes \(now ${percent}% of old\)
    rm "${file}"
    mv "${optfile}" "${file}"
fi
Clor commented 4 years ago

I had the same problem. I managed to get it to work with the following steps:

  1. Install some executables with brew install gs jbig2enc qpdf
  2. I got the darwinc64 release from https://github.com/pts/sam2p/releases , changed the filename to just sam2p and did chmod +x sam2p from the terminal to turn the file into an executable binary. Then try to run it as ./sam2p, and do not move it to the bin when Mac OS X wants you to do that. Then go into the Mac OSX security settings and choose 'allow anyway', such that the file can now be run.
  3. Repeat the above steps for png22pnm at https://github.com/pts/tif22pnm/releases .
  4. Copy those two files (sam2p and png22pnm) to the pdfsizeopt_libexec folder to overwrite the already present executables
  5. I changed the name of the qpdf and jbig2 files to 'qpdf_backup' and 'jbig2_backup' such that those files are not found.
pts commented 3 years ago

As a temporary workaround, does pdfsizeopt work for you with Docker (instructions here: https://github.com/pts/pdfsizeopt) on macOS?

pts commented 3 years ago

FYI pdfsizeopt doesn't work (for some PDF inputs) with the latest version of Ghostscript, so brew install gs is not a universal workaround. You also have to specify pdfsizeopt --do-optimize-fonts=no.

FYI No need for brew install jbig2enc, here is how to compile the jbig2 tool used by pdfsizeopt for amd64 macOS: https://github.com/pts/pdfsizeopt-jbig2/blob/master/c-darwinc64.sh

FYI The qpdf exectuable for i386 macOS was built with https://github.com/pts/pts-qpdf/blob/master/c-darwinc32.sh . There is no am64 equivalent yet, but probably it's not too hard to create one.

byronwall commented 3 years ago

@pts I was able to use Docker but the install was too big for long term use. The recommendation to avoid font optimization seemed to be the missing piece. It's working for now.

pts commented 1 year ago

This has been fixed recently (see https://github.com/pts/pdfsizeopt/issues/158 for details). Please follow the updated Installation instructions and usage on macOS section in https://github.com/pts/pdfsizeopt.