Rearranging PDF pages to be ordered by size

I have been trying to figure out a solution to this problem for months so any help/guidance/ideas would be incredibly appreciated. I feel like there’s gotta be a solution to this between applescript, automator, keyboard maestro, perhaps an iOS shortcut, or something else but I’m stumped.

I often need to print PDFs that have multiple page sizes (legal and letter) and the different page sizes are mixed throughout the total document. When I’m at my office I have the ability to print exactly in the order needed and have mixed paper sizes by using Adobe Acrobat. However when I am not at my office I don’t have this ability as I carry a single tray printer with me in my car. I often have little notice/time to get back to the office to print the PDF document so I need a way to do this with my single tray printer. I’d also love to be able to do this locally on a mac I carry with or an iPad. I don’t want to use a web service to sort the pages due to confidentiality reasons.

So that’s my long lead up to my question…does anyone know a way to sort PDF pages by paper/page size? I’d like the pages still kept in order within their “stacks” of letter and legal size pages. Even better would be to have some kind of index page print at the end showing what pages were moved/where they go when manually reassembled but that’s a lofty ambition and not a necessity.

Thanks for any help and guidance!

Let’s start with a couple of useful command line tools for working with PDFs.

The (untested) approach I would take would then be to write a shell script utilising qpdf and pdfinfo burst a PDF into multiple single page PDFs (which should each have a single page size and orientation), iterate over those pages and sort them into different folders or rename them based on their individual page size and orientation. Each group could then be recombined (maintaining sort order based on the page numbering in the file names), and then printed in discrete, ordered sets.

You could also add in some sort of log file or files that are later concatenated, that record what goes where to cover your indexing.

Hope that helps.

I thought I had looked at those awhile ago but perhaps I was looking at another pdf homebrew tool. I’ll take a look and see what I can come up with.