Reading Time: 2 minutes
If're a Linux user and you need to script certain page extraction from PDF files, crypt protect with password a PDF file or decrypt (remote password protection from PDF) or do some kind of structural transformation of existing PDF file you can use a QPDF command line utility. qpdf is in active development and very convenient tool for Website developers (PHP / Perl / Python), as often on websites its necessery to write code to cut / tailer / restructure PDFs.
1. Install QPDF from deb / rpm package
qpdf is instalalble by default in deb repositories on Debian / Ubuntu GNU / (deb derivative) Linux-es to install it apt-get it
apt-get install –yes qpdf
On RPM based distribution CentOS / SuSE / RHEL / Fedora Linux to install qpdf, fetch the respective distribution binary from rpmfind.net or to install latest version of qpdf build it from source code.
2. Install QPDF from source
To build latest qpdf from source
- on RPM based distributions install with yum fullowing packages:
yum -y install zlib-devel pcre-devel gcc gcc-c++
- on Deb based Linuces, you will need to install
apt-get install –yes build-essential gcc dpkg-dev g++ zlib1g-dev
Then to build gather latest qpdf source from here
wget -q https://pc-freak.net/files/qpdf-5.1.2.tar.gz
tar -zxvf qpdf-5.1.2.tar.gz
Once it is installed, if you get error on qpdf runtime:
/usr/local/bin/qpdf: error while loading shared libraries: libqpdf.so.13: cannot open shared object file: No such file or directory
To solve the error find in your compile directory libqpdf.so.13 and copy it to /usr/lib or /usr/local/lib
cp -rpf ./libqpdf/build/.libs/libqpdf.so.13 /usr/local/lib
3. Decrypt password encrypted (protected) PDF file
if you have time and you like reading be sure to check the extensive qpdf-manual.
To remove password from a PDF file protected with a password with qpdf
qpdf –password=SECRET-PASSWORD –decrypt input-file.pdf output-file.pdf
QPDF has a vast range of split and merge features. It can combine all the files in a folder (*.pdf), you can use it to try to recover damaged pdf files, extract individual pages from PDF, dump and reverse page range, make new created PDF with old PDF's reversed pages (pages 1,2,3,4 to become in order 4,3,2,1), apply some single pdf file metadata to multiple files.
4. Try to Recover damaged PDF file
To try to recover some damaged file with qpdf:
qpdf file-to-repair.pdf repaired-file.pdf
5. Extract certain pages or page range from PDF
It is recommended to use the version built from source to extract certain page range from PDF
/usr/local/bin/qpdf –empty –pages input-file.pdf 1-5 — outfile-file.pdf
If you wanted to take pages 1–5 from file1.pdf and pages 11–15 from file2.pdf in reverse, you would run
qpdf file1.pdf –pages file1.pdf 1-5 file2.pdf 15-11 — outfile.pdf