Separating PDF with Ghostscript

I am trying to share multi-page PDF with Ghostscript, and I found the same solution on other sites and even on ghostscript.com , namely:

gs -sDEVICE=pdfwrite -dSAFER -o outname.%d.pdf input.pdf 

But it seems to me that this does not work for me, because it creates one file, with all pages and the name outname.1.pdf.

When I add the start and end pages, then it works fine, but I want it to work without knowing these options.

I found a solution for this in the gs-devel archive: http://ghostscript.com/pipermail/gs-devel/2009-April/008310.html - but I feel like I am doing this without pdf_info .

When I use another device, for example pswrite , but the same parameters, it works correctly, creating as many ps files as possible, since my input.pdf contains.

Is this normal when using pdfwrite ? Am I doing something wrong?

+20
pdf-generation ghostscript
Apr 19 2018-12-12T00:
source share
4 answers

What you see is a "normal" behavior: the current version of the Ghostscript pdfwrite output pdfwrite does not support this function. This is also (admittedly, in some vague way) documented in Use.htm :

"Please note, however, that one page for each file may not be supported by all devices ..."

It seems that I remember that one of the Ghostscript developers mentioned in IRC that they can add this function to pdfwrite in some future version, but it seems that this requires serious code processing, so they haven't done it yet ...




Update: As Gordon’s comment already mentioned, starting with version 9.06 (released July 31, 2012), Ghostscript now supports the command line, which is also indicated for pdfwrite in the question. (Gordon must have found unofficial support for this as early as 9.05, or he compiled his own executable file from source sources that had not yet been marked as 9.06).

+8
Apr 19 2018-12-12T00:
source share

I found this wriiten script by Mr. Weimer super useful:

 #!/bin/sh # # pdfsplit [input.pdf] [first_page] [last_page] [output.pdf] # # Example: pdfsplit big_file.pdf 10 20 pages_ten_to_twenty.pdf # # written by: Westley Weimer, Wed Mar 19 17:58:09 EDT 2008 # # The trick: ghostscript (gs) will do PDF splitting for you, it just not # obvious and the required defines are not listed in the manual page. if [ $# -lt 4 ] then echo "Usage: pdfsplit input.pdf first_page last_page output.pdf" exit 1 fi yes | gs -dBATCH -sOutputFile="$4" -dFirstPage=$2 -dLastPage=$3 -sDEVICE=pdfwrite "$1" >& /dev/null 

Origin from: http://www.cs.virginia.edu/~weimer/pdfsplit/pdfsplit

save it as pdfsplit.sh , see magic.

PDFSAM could also do the job. Available on Windows and Mac.

+15
May 9 '12 at 4:34
source share
  #!/bin/bash #where $1 is the input filename ournum=`gs -q -dNODISPLAY -c "("$1") (r) file runpdfbegin pdfpagecount = quit" 2>/dev/null` echo "Processing $ournum pages" counter=1 while [ $counter -le $ournum ] ; do newname=`echo $1 | sed -es/\.pdf//g` reallynewname=$newname-$counter.pdf counterplus=$((counter+1)) # make the individual pdf page yes | gs -dBATCH -sOutputFile="$reallynewname" -dFirstPage=$counter -dLastPage=$counter -sDEVICE=pdfwrite "$1" >& /dev/null counter=$counterplus done 
+3
Nov 10 '13 at 2:40
source share

Here is a simple python script that does this:

 #!/usr/bin/python3 import os number_of_pages = 68 input_pdf = "abstracts_rev09.pdf" for i in range(1, number_of_pages +1): os.system("gs -q -dBATCH -dNOPAUSE -sOutputFile=page{page:04d}.pdf" " -dFirstPage={page} -dLastPage={page}" " -sDEVICE=pdfwrite {input_pdf}" .format(page=i, input_pdf=input_pdf)) 
+1
Aug 27 '15 at 21:33
source share



All Articles