Thursday, April 18, 2013

Splitting PDF-File via Command-Line

To Split a PDF file, or lets say, cut-out some pages, and save them as a new PDF file in Linux, you don't need to install any extra software. Of course you can use a nice GUI driven program to do this, but it is actually not that hard to do in command line. (I like to keep my system clean with the least amount of clutter, so I prefer to use the tools available by default)

If you want to use the command line function for this manually, it is a bit of a pain, so I recommend to create an alias for this PDF splitting/extract function.
You do that by adding the following code in your ~/.bashrc file.


1. open a terminal screen. (Ctrl + T)

2. type:  gedit ~/.bashrc

3. add the following paragraph to the text already in the file. 


#------------------------------------------------------
# function uses 3 arguments:                          -
# $1 is the first page to extract from input-file     -
# $2 is the last page to extract from input-file      -
# $3 is the input file                                -
# Output file will be named "inputfile_pXX_to_pYY.pdf"-
#------------------------------------------------------

pdfextract()
{
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
-dFirstPage=${1} \
-dLastPage=${2} \
-sOutputFile=${3%.pdf}_page${1}_to_page${2}.pdf \
${3}
}


4. save the file and exit.

5 Use the extract function as follows:
(Example: a PDF-File "inputfile.pdf" of 50 pages, and you want to extract pages 21 to 37 into a new PDF-file "inputfile_p21_to_p37.pdf"

You do that by opening a terminal screen (Ctrl+T) and go to the directory where your input PDF-file is located. and type:

pdfextact 21 37 inputfile.pdf


That's it.
enjoy. :-)



No comments:

Post a Comment

Please be courteous, even if you do not share the same view.