Reversing Pages in Preview

Has this issue ever been resolved? I had to scan a 60-page document in the only way to assure that bent corners would not obstruct the process and I wound up with the pages in reverse order. I tried Save to Pdf again and chose reverse order, but that did not work. 60 pages would take a long time to reorder the thumbnails manually. Just wondering if this problem had ever been solved since the discussion on here years ago.

iMac 27", macOS 10.14

Posted on Mar 30, 2020 12:48 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 30, 2020 1:38 PM

Sometimes, one has to roll their own tool. The following Python-Objective C application will take the name of your PDF on the command line, and write out the PDF with the pages reversed and the name "_reversed" appended to the basename.


Because Python is indent sensitive, I do not recommend copy/pasting the following content into a TextEdit or a word processing application as these will destroy the indent structure. Recommend a know programmer's editor that supports Python syntax, and an example is BBEdit where after copying this code, you use the BBEdit edit menu : Paste > and Match Indentation. Save the file as pdfrev.py on your Desktop. BBEdit is trialware that after the trial it becomes free to use with fewer features. This Python script is for Apple's Cocoa bridge in Python 2.7.16:


#!/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, Python 2.7,16 on 10.14.6
# VikingOSX, 2019-01-19, Apple Support Communities, No warranties.

from Foundation import NSURL
from Quartz.PDFKit import PDFDocument
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()

        if pdf.pageCount() == 1:
            sys.exit('only one page found')

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

        pdf_out.writeToFile_(pdfrev)


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


In the Terminal, change directory to your Desktop, make this Python script executable, and run it providing the name of the PDF on your Desktop whose pages you want reversed. This will not re-order existing page numbers in the PDF, if present.

cd ~/Desktop
chmod +x ./pdfrev.py
./pdfrev.py ./pdftoreverse.pdf


The output PDF will be on your Desktop as ./pdftorevers_reversed.pdf — using the example name here.



3 replies
Question marked as Top-ranking reply

Mar 30, 2020 1:38 PM in response to Hazel Greenberg

Sometimes, one has to roll their own tool. The following Python-Objective C application will take the name of your PDF on the command line, and write out the PDF with the pages reversed and the name "_reversed" appended to the basename.


Because Python is indent sensitive, I do not recommend copy/pasting the following content into a TextEdit or a word processing application as these will destroy the indent structure. Recommend a know programmer's editor that supports Python syntax, and an example is BBEdit where after copying this code, you use the BBEdit edit menu : Paste > and Match Indentation. Save the file as pdfrev.py on your Desktop. BBEdit is trialware that after the trial it becomes free to use with fewer features. This Python script is for Apple's Cocoa bridge in Python 2.7.16:


#!/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, Python 2.7,16 on 10.14.6
# VikingOSX, 2019-01-19, Apple Support Communities, No warranties.

from Foundation import NSURL
from Quartz.PDFKit import PDFDocument
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()

        if pdf.pageCount() == 1:
            sys.exit('only one page found')

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

        pdf_out.writeToFile_(pdfrev)


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


In the Terminal, change directory to your Desktop, make this Python script executable, and run it providing the name of the PDF on your Desktop whose pages you want reversed. This will not re-order existing page numbers in the PDF, if present.

cd ~/Desktop
chmod +x ./pdfrev.py
./pdfrev.py ./pdftoreverse.pdf


The output PDF will be on your Desktop as ./pdftorevers_reversed.pdf — using the example name here.



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.

Reversing Pages in Preview

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