Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a PDF with Ghostscript

I try to split a multipage PDF with Ghostscript, and I found the same solution on more sites and even on ghostscript.com, namely:

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

But it seems not working for me, because it produces one file, with all pages, and with the name outname.1.pdf.

When I add the start and end pages, then it is working fine, but I want it to work without knowing those parameters.

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

When I use a different device, for example pswrite, but same parameters, it works correctly, producing as many ps files, as my input.pdf contains.

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

like image 443
zseder Avatar asked Apr 19 '12 12:04

zseder


People also ask

Can Ghostscript edit PDF?

This paper intends to introduce some of the ways the Ghostscript tools can be used to manipulate a PDF file, including creation of bookmarks, annotations, and setting document properties.

How do you split a PDF with power automated?

Split A PDF File After Every Page Using A Desktop FlowCreate a new flow in Power Automate Desktop and call it Split PDF Document. We want the user to select a PDF file to be split. Add a Display select file dialog action with the title “Select PDF To Split.” Then set another variable called CurrentPage.

How do I merge PDF files in Ghostscript?

Ghostscript gives you the power to combine files, convert files, and much more, all from the command line. It is easy to combine several input files into one combined PDF using Ghostscript: gs -sDEVICE=pdfwrite \ -dNOPAUSE -dBATCH -dSAFER \ -sOutputFile=combined. pdf \ first.


1 Answers

I found this script wriiten 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's 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 gs -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$4" -dFirstPage=$2 -dLastPage=$3 -sDEVICE=pdfwrite "$1" 

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

save it as pdfsplit.sh, see the magic happens.

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

like image 95
Juanito Fatas Avatar answered Oct 02 '22 23:10

Juanito Fatas