Reverse a PDF using Automator

After some digging, I found there was no built-in option to reverse a PDF file's pages. With the help of a few other forums, I was able to compile the attached workflow.


All you need to do is open the application/workflow and select the PDF to reverse. It then splits it into a series of individual pages and then merges them all back together in reverse. Then cleans up the fragmented PDF files.


I hope other people find this handy. Try it out and let me know what you think!


(Unfortunately, Apple Communities doesn't allow me to attach the script. So here's a screenshot of the script that you can recreate ... )


Posted on Jan 19, 2019 2:08 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 19, 2019 5:31 PM

Here is a Python script that takes one or more PDF files on its command-line and writes each document's reversed pages into a new document with "_reversed" appended to the filename (e.g. foo.pdf => foo_reversed.pdf). This requires the System Python because it is using the Apple PDFKit framework. This is adaptable to an Automator Run Shell Script with passed PDF file arguments.


Highly recommend this is copy/pasted into a programmer's editor that understands Python indents. Some editors (e.g. BBEdit, Sublime Text) facilitate this on their Edit menu with the ability to paste and indent to preserve program structure.


#!/usr/bin/python
# coding: utf-8

# write a PDF with its pages reversed into name_reversed.pdf
# tested with Python 2.7.10 on macOS High Sierra 10.13.6
# VikingOSX, 2019-01-19, Apple Support Communities, No warranties.

from Foundation import NSURL
from Quartz import PDFDocument, PDFPage
import os
import sys


def usage():
    print("Usage: {} file1.pdf … filen.pdf".format(__file__))


def main():
    if len(sys.argv) < 2:
        usage()
        sys.exit(1)

    for apdf in sys.argv[1:]:
        pdf_file = os.path.expanduser(apdf)
        if not pdf_file.endswith('.pdf'):
            raise Exception('PDF file type required')

        # build output PDF filename
        bpath, ext = os.path.splitext(pdf_file)
        pdfrev = os.path.basename(bpath + '_reversed' + ext)

        url = NSURL.fileURLWithPath_(pdf_file)
        pdf = PDFDocument.alloc().initWithURL_(url)
        pdf_out = PDFDocument.alloc().init()
        page_cnt = pdf.pageCount()
        pdf_page = PDFPage

        # n is sequential page increase, r is the reversed page number
        for n, r in enumerate(reversed(range(0, page_cnt))):
            pdf_page = pdf.pageAtIndex_(r)
            pdf_out.insertPage_atIndex_(pdf_page, n)

        pdf_out.writeToFile_(pdfrev)


if __name__ == '__main__':
    sys.exit(main())


2 replies
Question marked as Top-ranking reply

Jan 19, 2019 5:31 PM in response to schnautz07

Here is a Python script that takes one or more PDF files on its command-line and writes each document's reversed pages into a new document with "_reversed" appended to the filename (e.g. foo.pdf => foo_reversed.pdf). This requires the System Python because it is using the Apple PDFKit framework. This is adaptable to an Automator Run Shell Script with passed PDF file arguments.


Highly recommend this is copy/pasted into a programmer's editor that understands Python indents. Some editors (e.g. BBEdit, Sublime Text) facilitate this on their Edit menu with the ability to paste and indent to preserve program structure.


#!/usr/bin/python
# coding: utf-8

# write a PDF with its pages reversed into name_reversed.pdf
# tested with Python 2.7.10 on macOS High Sierra 10.13.6
# VikingOSX, 2019-01-19, Apple Support Communities, No warranties.

from Foundation import NSURL
from Quartz import PDFDocument, PDFPage
import os
import sys


def usage():
    print("Usage: {} file1.pdf … filen.pdf".format(__file__))


def main():
    if len(sys.argv) < 2:
        usage()
        sys.exit(1)

    for apdf in sys.argv[1:]:
        pdf_file = os.path.expanduser(apdf)
        if not pdf_file.endswith('.pdf'):
            raise Exception('PDF file type required')

        # build output PDF filename
        bpath, ext = os.path.splitext(pdf_file)
        pdfrev = os.path.basename(bpath + '_reversed' + ext)

        url = NSURL.fileURLWithPath_(pdf_file)
        pdf = PDFDocument.alloc().initWithURL_(url)
        pdf_out = PDFDocument.alloc().init()
        page_cnt = pdf.pageCount()
        pdf_page = PDFPage

        # n is sequential page increase, r is the reversed page number
        for n, r in enumerate(reversed(range(0, page_cnt))):
            pdf_page = pdf.pageAtIndex_(r)
            pdf_out.insertPage_atIndex_(pdf_page, n)

        pdf_out.writeToFile_(pdfrev)


if __name__ == '__main__':
    sys.exit(main())


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Reverse a PDF using Automator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.