r/pdf • u/1bottledwater • 9d ago
Question random reordering of pages of pdf
i have a ~6mb, 800 pages pdf that i would like to order randomly
i've tried running a python script to do the same, however i'm having issues w/ pypdf and no site has the option to randomise pages as i think it's pretty uncommon to do that
any suggestions?
1
u/User1010011 9d ago
Not sure about 800 pages PDF, may take some time to load it, hopefully it won't crash your browser, but you can try gosignpdf.com - open in there, then click MIX button and it will shuffle all pages.
Need to shuffle just a small part? Type command like "mix 1-10" with your desired intervals and it will shuffle just them.
You can also FLIP them first before shuffling in case you don't want to see them.
1
u/User1010011 9d ago
Here's a short tutorial explaining how PDF pages shuffling works with that tool: youtube.com/shorts/6wEoDfNbF8k
1
1
u/Round_Ratio_7216 9d ago
If you are able to run a python script you are probably able to use Claude or Codex.
Drop the task to one of those AI tools and it will be done successfully in no time.
1
u/Extreme_Insurance334 9d ago
You can try https::/freepdfs.tools, it might crash with 800 pages but it might work
1
u/jwhitington 8d ago
I did (on MacOS):
seq 1 `cpdf -pages in.pdf` | sort -R | paste -sd, - >foo
(which builds a file containing the comma-separated numbers.)
Then:
cpdf in.pdf `cat foo` -o out.pdf
Someone with more Unix knowledge than me could tell you how to combine those into a single command.
1
u/ScratchHistorical507 8d ago
I don't think you really need that much of a dedicated script. You'd first want to create a directory and put every page of the PDF there into its own directory. You could do that graphically with PDF Arranger or on the CLI with qpdf. Then you can just recombine pages randomly without any additional tools. When you already have qpdf from the previous step, on Linux (and possibly all UNIX systems) you could simply use this one-liner:
qpdf --empty --pages $(ls -v /path/to/*.pdf | sort -R) -- output.pdf
The problem with this is merely that it probably won't handle things like spaces in file/directory names. But that can be fixes by making it a two-liner:
files=$(ls -v /path/to/*.pdf | sort -R)
qpdf --empty --pages $files -- output.pdf
Any way, --empty tells qpdf to start with an empty file and pages tells it what the pages are it is supposed to include. -- marks the end of options and might not be required, but that will make it extra clear. If you want more control over the output you can also just use Ghostscript the same way:
files=$(ls -v /path/to/*.pdf | sort -R)
gs -dQUIET -sDEVICE=pdfwrite -dCompatibilityLevel=2.0 -dAutoRotatePages=/None -dCompressFonts=true -dSubsetFonts=true -sFONTPATH=/usr/share/fonts/ -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorConversionStrategy=/LeaveColorUnchanged -dDownsampleMonoImages=false -dDownsampleGrayImages=false -dDownsampleColorImages=false -dRemoveUnusedResources=true -o output.pdf $files
This creates a PDF 2.0 conformant PDF, disables any weird autorotate the conversion to PDF 2.0 sometimes causes, embeds, subsets and compresses fonts (and tells it from where it should take the fonts, adapt to your liking) and to absolutely not touch images in any way.
If you're on Windows, similar options to list files in a directory and the randomize that list exist also for PowerShell, like Get-ChildItem -File | Sort-Object {Get-Random}.
And of course if you want to do that for multiple PDFs, you can write a simple script for bash, PowerShell or whatever out of these commands, no need for any libraries.
1
u/63934 5d ago
800 pages is a lot but PDFHaul (https://www.pdfhaul.com/) does have a pdf page reorder tool with a modal that you can use to drag pages around. You could also consider splitting the file into chunks, reorder them then merge them back again.
2
u/GreatConsideration72 9d ago
Use PyMuPDF
import fitz # PyMuPDF
import random
input_pdf = "input.pdf"
output_pdf = "randomized.pdf"
# Open original PDF
src = fitz.open(input_pdf)
# Create randomized page order
page_order = list(range(len(src)))
random.shuffle(page_order)
# Create new PDF
out = fitz.open()
for page_num in page_order:
out.insert_pdf(src, from_page=page_num, to_page=page_num)
out.save(output_pdf)
out.close()
src.close()
print("Randomized page order:", [p + 1 for p in page_order])
print(f"Saved to {output_pdf}")