xilopaint / alfred-pdf-tools

Optimize, encrypt and manipulate PDF files.
MIT License
149 stars 10 forks source link

Optimize not working #10

Closed jordiparra closed 2 years ago

jordiparra commented 3 years ago

I don't know what the issue is, but all commands work except for Optimize. Can't figure out what's going on. 🤔

tinkyholloway commented 3 years ago

For me the problem was that the bundled k2pdfopt program is x86_64 and my mac is arm.

I changed alfred_pdf_tools.py as follows to use gs (that I had installed via homebrew):

def optimize(query, pdfs):
    """Optimize PDF files."""
    try:
        if query == "":
            query = "150"

        if query:
            if not query.lstrip('+-').isdigit():
                raise NotIntegerError

        if query:
            if int(query) < 0:
                raise NegativeValueError

        for pdf in pdfs:
            noextpath = os.path.splitext(pdf)[0]
            outputpath = "{} optimized.pdf".format(noextpath)
            command = "/opt/homebrew/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dColorImageResolution={} -dColorImageDownsampleType=/Bicubic -dGrayImageResolution={} -dGrayImageDownsampleType=/Bicubic -dMonoImageResolution={} -dMonoImageDownsampleType=/Bicubic -dNOPAUSE -dBATCH -sOutputFile='{}' '{}'".format(query, query, query, outputpath, pdf)

            notify.notify('Alfred PDF Tools',
                          'Optimization starting.')

            proc = Popen(command, shell=True, stdout=PIPE)

            while proc.poll() is None:  # Loop while the subprocess is running.
                line = proc.stdout.readline()

                if "Processing pages" in line:
                    pg_no = line.split()
                    wf.cache_data('page_count', pg_no[4][:-1])

                if "Page" in line:
                    pg_no = line.split()
                    wf.cache_data('page_number', pg_no[1])

            notify.notify('Alfred PDF Tools',
                          'Optimization successfully completed.')

    except NotIntegerError:
        notify.notify('Alfred PDF Tools',
                      'The argument is not an integer.')

    except NegativeValueError:
        notify.notify('Alfred PDF Tools',
                      'Negative integer is not a valid argument.')
xilopaint commented 2 years ago

Hey @tinkyholloway, I just released v3 with a new version of k2pdfopt. Could you test it and tell me if you still have the issue in your ARM Mac?

xilopaint commented 2 years ago

I received feedback from someone else that the Optimize file action of latest release is working on an ARM Mac. I'm closing the issue but you guys can reopen if still face any issue.

Kazoned commented 1 year ago

got this same problem. I downloaded the arm k2pdfopt file and replaced it with the exe in the bin folder and it works fine

xilopaint commented 1 year ago

got this same problem. I downloaded the arm k2pdfopt file and replaced it with the exe in the bin folder and it works fine

@Kazoned Thank you for the feedback. Do you have Rosetta installed? You can check it using the !diagnostic keyword.

Kazoned commented 1 year ago

ah after reading that rosetta functions, as a go between for arm and intel chips that would make sense why it didn't work off the bat. As a first time Mac user (3rd week in), stuff like this is not readily apparent and will probably will be useful to other converts in the future. So, two solutions for M1/M2 users: replace the k2pdfopt exe with an arm installation downloaded from https://www.willus.com/k2pdfopt/ or probably a much easier solution to install rosetta beforehand via terminal command: softwareupdate --install-rosetta