0

I don't know if this can be done, I'm just learning ghostscript. Say I have multiple PDF files each about 500 Pages in length. Can I setup ghostscript to go extract every 100 pages from each document and save each as a separate PDF file?

So I have FileA.pdf, 500 Pages in Length. So I'd now want FileA_0001.pdf FileA_0002.pdf FileA_0003.pdf FileA_0004.pdf FileA_0005.pdf

I've managed to write a script that will split files and merge them based on my interval, I'm having trouble trying to rename files properly. The issue I've run into is after it finishes splitting and merging after the first file it will rename it as FileA_0001.pdf FileA_0002.pdf FileA_0003.pdf FileA_0004.pdf FileA_0005.pdf

However the problem is once it begins the process for FileB, it does this FileB_0006.pdf FileB_0007.pdf I've tried a few different ways, but each have failed, advice? Can someone assist?

    for file in /mnt/bridge/pdfsplit/staging/*.[pP][dD][fF]
do
  echo $file
  #Splits All the Files
  gs -q -dNOPAUSE -sDEVICE=pdfwrite -o tmp_%04d.pdf $file

  #Removes Last File in List; Ghostscript creates a blank file everytime it splits
  find /mnt/bridge/pdfsplit/ -name "tmp*"  | tail -1 | while read filename ; do rm $filename; done

    pageCount=$(find . -name "tmp*" | wc -l)
    documents=$(((pageCount / 998) + (pageCount % 998 > 0)))
    pages=$(((pageCount/documents) + (pageCount % documents > 0 )))

    for ((i=1; i<$pageCount; i++)); do
      list=$(ls -1 tmp* 2>/dev/null | head -$pages)
      count=$(ls -1 tmp* 2>/dev/null| wc -l)
      gs -q -dNOPAUSE -sDEVICE=pdfwrite -o $(basename $file .pdf )_Part_$(printf %04d $i).pdf -dBATCH $list
      rm -f $list
      if [[ $count -eq 0 ]]; then
         echo "Done"
         break
         fi
    done


   #Removes Last File in List; Ghostscript is creating a blank file
   mv *.pdf /mnt/bridge/pdfsplit/splitFiles/
   find /mnt/bridge/pdfsplit/splitFiles/ -name "*.pdf"  | tail -1 | while read filename ; do rm $filename; done

done
  • Does it have to be Ghostscript, or would another tool be acceptable? – Stephen Kitt Mar 20 at 20:04
  • Ghostscript would be best, since it’s the main tool we use to do PDF manipulation, but I’m open to suggestions. – Josh Adams Mar 20 at 23:18
1

Does this help at all?

#!/bin/bash

function getChunk {
    #extract a page range
    gs -q -dNOPAUSE -sDEVICE=pdfwrite -sPageList=$1-$2 -o ${3%%.*}_$(printf %04d $4).pdf $3
}

for file in *.pdf; do

    #Use gs to get the page count
    pgs=$(gs -q -dNODISPLAY -c "($file) (r) file runpdfbegin pdfpagecount = quit")

    #specify the number of pages in each chunk as step
    step=10

    #calculate the number of whole chunks 
    chunks=$(( pgs / step))

    #reset all counters between pdfs
    f=0    #first page to extract in chunk
    l=0    #last page to extract in chunk
    i=0    #chunk counter

    #Extract the whole chunks 
    for ((i=0; i<$chunks; i+=1)); do

        #calculate the first and last pages 
        f=$((i*step+1))
        l=$((f+step-1))
        getChunk $f $l $file $i
    done

    #Pick up any part chunk at the end of the file
    f=$((l+1))
    if [ $f -le $pgs ]; then
        getChunk $f $pgs $file $i
    fi
done

I will let you sort out the naming.....

  • I tried your script above, the problem I'm having is, it's not doing any sort of extraction. It's creating the chunks, but pretty much copies all the pages into the new file, so I have a document 1200 pages, it's just creating a new document with 1200 pages – Josh Adams Apr 9 at 13:29
  • Just tried this again on my machine 3621 pg document chunked into 100 page pieces + leftovers all as planned. The script is to be run from the pdf containing directory and is NOT error checked for any filename white-space issues, though I just checked that and while white space makes it fall over, the error is not such that it just copies the file....... This was with the code above copied and pasted into a script file. – bu5hman Apr 9 at 19:38
  • Try extracting just 1 chunk with a manual run of the getChunk function. gs -q -dNOPAUSE -sDEVICE=pdfwrite -sPageList=1-100 -o output.pdf input.pdf – bu5hman Apr 9 at 19:42
  • Just had a through what version of ghostscript are you using? I'm using 9.07, from what I can tell their is no reference of -sPageList in this version. – Josh Adams Apr 10 at 12:34
  • Actually I figured out how to script what I need it to do with my original script, I'll update it above. – Josh Adams Apr 10 at 15:03
0

Without gs

Create FileA_0001.pdf, FileA_0002.pdf, ..., FileA_0100.pdf

for i in $(seq 1 100); do pdftocairo -pdf -f $i -l $i FileA.pdf $(printf 'FileA_%04d.pdf' $i); done

Create FileA_1.pdf, FileA_2.pdf, ..., FileA_100.pdf

pdfseparate -l 100 FileA.pdf FileA_%d.pdf

My favourite is pdftocairo. Faster and more reliable than gs from my experience. Try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.