Saturday, January 13, 2007

How to join together multiple pdfs or seperate a single pdf into multiple pages using pdftk in Ubuntu

This tutorial was written in and about Ubuntu Edgy Eft. It should work in many other Debian based distros and beyond.


If you’ve got several pdf documents that you’d like joined together,
or one multi-paged pdf document that you’d like split apart, the
following two scripts using pdftk will be wonderfully helpful too you.


Pdftk is a very powerful tool and these scripts are just skimming the surface of what you can do with it.





First, you’ll want to download pdftk. Either use Synaptic (System- Administration- Synaptic Package Manager) or type the following into a terminal session:


sudo apt-get install pdftk


Now that you’ve downloaded and installed pdftk, we’re going to make two scripts in your /usr/bin directory and then let your system know that .pdf files can be opened by them.


cd /usr/bin


gksudo gedit pdftk_burst


Cut and paste the following text into your gedit session and then save the file.


#!/bin/bash


cd ${1%/*}


/usr/bin/pdftk “$1″ burst


rm doc_data.txt


That’s the script that will break apart multi-page pdf documents. If
you have a 5 page pdf, you’ll end up with 5 single page pdfs.


Let’s do the opposite script now - pdftk_concat.


gksudo gedit pdftk_concat


Cut and paste the following text into your gedit session and then save the file.


#!/bin/bash


cd ${1%/*}


outfile=”concat.pdf”


if [ -f $outfile ] ; then


rm -f $outfile


fi


/usr/bin/pdftk *.pdf cat output $outfile


Now that we’ve got our scripts in place, let’s make them executable. Type:


chmod +x pdftk_*


Let’s tell Nautilus how to know if you want to open a pdf using pdftk_burst or pdftk_concat.


First you’ll need a pdf file on your desktop. You can find a bunch here.


Once you have your pdf on your Desktop, go to Places- Desktop in your taskbar.


Right click on the pdf and choose Properties.







Go to the Open With tab.


Click the Add button.


Click the Use a custom command drop down towards the bottom and browse to /usr/bin/pdftk_burst.


Now click the Add button at the bottom.


Click the Add button again and do the last two steps for pdftk_concat.


From this point on, while your browsing through your directories or
on your Desktop you can right click on a pdf and choose Open With to
select pdftk_burst or pdftk_concat.


Here’s what they’ll do.


pdftk_burst will take each page of the pdf file and
make a seperate pdf file out of it (also leaving you a copy of the
original). If you’re working with a document more than three or four
pages in length, I’d recommend putting it in it’s own directory before
you do this.


pdftk_concat will take every pdf in a directory and
stitch it into a single pdf document, based on filename in
numerical/alphabetical order. The file it will create is going to be
called concat.pdf.


This won’t work on any pdfs that have been protected by a password or encryption.





powered by performancing firefox

No comments: