How to split PDF files from Linux Terminal with PDFtk

PDFtk can not only split PDF files but also edit and modify these files.

One of the best ways to split PDF files on Linux is not using GUI tools like Evince or Ocular. Instead, use a terminal application called PDFtk . Not only can it split PDF files, it can also edit and modify these files.

Install PDFtk

This application has been released for a while and can be easily installed on most Linux distributions. Open a terminal window and follow the instructions below to install the application.

Note: To install PDFtk, you must use Ubuntu, Debian, Arch Linux, Fedora or OpenSUSE. If you haven't run any Linux distributions yet, follow the instructions at the bottom.

Ubuntu

 sudo apt install pdftk 

Debian

 sudo apt-get install pdftk 

Arch Linux

PDFtk can be used on Arch Linux, but users will not be able to install software from the main sources of Arch Linux. Instead, interacting with Arch Linux AUR is a mandatory condition. To start installing PDFtk on Arch, open a terminal and use the Pacman package manager to synchronize the latest version of the Git tool.

Note: Having another PDFtk package on AUR makes it easier to install the program, because it decodes the program, instead of building from the source code. We do not recommend this option, as there are some problems with the built-in GCC-GCJ package.

 sudo pacman -S git 

Now that Git is working on Arch Linux, you will be able to use it to download the latest version of PDFtk AUR. In the terminal, use git clone to download the build guide file.

 git clone https://aur.archlinux.org/pdftk.git 

Use the CD command, move the terminal from the user's Home folder to the newly cloned pdftk folder.

 cd pdftk 

Inside the PDFtk source folder, start the build process by running makepkg. Remember that running makepkg will automatically download, compile and install any required dependencies. However, if these dependencies are not taken automatically, you will need to install them manually. All dependent files for the PDFtk AUR package can be found here.

Fedora

Currently, there is no PDFtk Fedora package in the software repository. Fortunately, it's easy to make OpenSUSE packages work. Start using wget to download the necessary packages.

 wget http://ftp.gwdg.de/pub/opensuse/distribution/leap/42.3/repo/oss/suse/x86_64/pdftk-2.02-10.1.x86_64.rpm 
wget http://ftp.gwdg.de/pub/opensuse/distribution/leap/42.3/repo/oss/suse/x86_64/libgcj48-4.8.5-24.14.x86_64.rpm

Use the CD command, move the terminal to the Downloads folder.

 cd ~ / Downloads 

Finally, use the DNF package manager to install PDFtk:

 sudo dnf install libgcj48-4.8.5-24.14.x86_64.rpm pdftk-2.02-10.1.x86_64.rpm -y 

OpenSUSE

 sudo zypper install pdftk 

Build from the source code

Building PDFtk from source does not take you too much effort because there are already built-in files built into the source directory. To build the program from the source code, you first need to download the code. To get the code, use the wget download tool in the terminal device.

How to split PDF files from Linux Terminal with PDFtk Picture 1How to split PDF files from Linux Terminal with PDFtk Picture 1

To make sure PDFtk is built correctly, make sure you have GCC, GCJ and libgcj installed on Linux.

 wget https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-2.02-src.zip 

Next, use the Unzip command to extract the PDFtk Zip folder into the Linux Download folder. If you have not installed the Unzip application, do this to "extract" the zip file and install it.

 cd ~ / Downloads 
unzip pdftk-2.02-src.zip

Extracting PDFtk will create a folder 'pdftk-2.02-src' in Downloads. In the terminal, use the CD command to enter.

 cd pdftk-2.02-src 

In the original PDFtk folder, you cannot manipulate much. To compile anything, we need to move the terminal to the subdirectory of PDFtk.

 cd pdftk 

The PDFtk subdirectory has dozens of dedicated Makefiles that users can use to automatically create different operating systems. Use the LS command, list the contents of the directory.

 ls 

Look over and find the specific Makefile you need and start the build process with the command below. Remember to change the " Makefile.filename " in the command below with the name of the Makefile you need to use.

 make -f Makefile.filename 

Use PDFtk

One of the main attractions of PDFtk is the ability to intervene and split PDF files. For example, to split a PDF file so that each page of the document becomes a separate file, try using the burst command:

How to split PDF files from Linux Terminal with PDFtk Picture 2How to split PDF files from Linux Terminal with PDFtk Picture 2

 pdftk testfile.pdf burst 

PDFtk will export split files in the same location as the source file.

If you want to merge all PDF files broken down into a single file, start by renaming the original PDF file (to be safe).

 mv testfile.pdf testfile.bak 

Now, the tested PDF file is safe, combining everything with PDFtk. First, use the LS command to view the files in the directory.

 ls 

Next, run the LS command again, but this time use it to store all PDF files.

 ls * .pdf >> pdf-filenames.txt 

Assign the contents of the file pdf-filenames.txt to the Bash variable. The Bash variable in this case is not simply a wildcard when we join the PDF files but also marks the files in order.

 value = $ ( 

Finally, combine PDF files with PDFtk and $ values.

 pdftk $ value cat output recombined-document.pdf 

See more:

  1. How to split PDF file without Vietnamese error
  2. Split or merge PDF files in Linux
  3. How to edit PDF file using FormSwift online
4.3 ★ | 8 Vote