Split PDF files easily using Ubuntu scripts

One of the fun parts of reinstalling an OS (yes, I think it is fun!), is to discover new software and new ways of doing things. As such, it works as a “digital shower”, getting rid of unnecessary stuff that has piled up.

Trying to also get rid of some physical mess, I am scanning some piles of paper documents. This leaves me with some large multi-page PDFs that I would like to split up easily. In the spirit of software carpentry I looked for a simple solution for splitting up a PDF file, and came across the command “burst” in the little terminal application pdftk. To use it on Ubuntu, you will first need to install it, using the terminal command:

sudo apt update && sudo apt install pdftk

Then this one-liner is all that is necessary to split a PDF file into a series of individual PDFs:

pdftk your-file.pdf burst

For convenience, I also made it into a small Ubuntu script:

This script can run by right-clicking on a file in the file manager:

And the end-result is a series of individual PDF files:

And then you can of course also combine the files again, either all PDFs:

pdftk *.pdf cat output newfile.pdf

or only the files you like:

pdftk file1.pdf file2.pdf cat output newfile.pdf

Shell script for compressing PDF files on Ubuntu

ubuntu-logo112Back on OSX one of my favourite small programs was called PDFCompress, which compressed a large PDF file into something more manageable. There are many ways of doing this on Ubuntu as well, but nothing really as smooth as I used to on OX.

Finally I took the time to figure out how I could make a small shell script based on ghostscript. The whole script looks like this:

#!/bin/sh
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages -dCompressFonts=true -r150 -sOutputFile="compress_$@" "$@"

and by saving it in the nautilus scripts directory:

 ~/.local/share/nautilus/scripts

It shows up when I right click on a file. For most of the files I have tried so far today (uncompressed PDF files), it compresses the files to at least 1/10th of the original size. Very useful, particularly when I only need screen resolution for files.

Screenshot from 2016-06-29 16-55-49

Reducing PDF file size

I am working on finalizing an electronic version of a large PDF file (600 page NIME proceedings), and have had some problems optimizing the PDF file. This may not be so strange, since the file is an assembly of 130 individual PDF files all made by different people and using all sorts of programs and OS.

Usually, PDFCompress works wonders when it comes to reducing PDF file sizes, but for the proceedings-file it choked at some of the fonts. Strangely enough, Acrobat Pro also encountered problems, and with no useful explanation on what went wrong.

Fortunately, OSX came to the rescue. When saving a PDF file in OSX it is possible to apply a quartz filter. And OSX had no problems saving and reducing a new PDF file of the proceedings-file. However, the built-in “reduce file size” filter reduced the images too much. But I found an explanation on how to create your own quartz filters, where it is possible to choose compression settings.

I find it strange that Acrobat Pro couldn’t do the job, but I am very happy to have found a solution.

Use Preview instead of Adobe Reader in Texmate

I just installed Adobe Reader on my new computer, only to discover that it hijacked the PDF preview window in TextMate when working on LaTeX documents. This also happened the last time I installed a new system, and I couldn’t remember what I did to change it back to using Preview as the default PDF viewer.

After googling around, I remembered that TextMate is just using the regular browser settings when it comes to displaying PDF files. For some weird reason, you have to go in the Adobe Reader preferences to change this back. Unfortunately, on my system the uncheck box for this functionality is grayed out:

adobe-reader

Finally, I just decided to delete Adobe’s PDF viewer plugin altogether, by deleting this file:

/Library/Internet Plug-Ins/AdobePDFViewer.plugin

This solved the problem, both in Textmate and in my browsers. If anyone wonders, Preview is much, much faster than Adobe Reader and I also think it displays the fonts nicer.

PDF merge in preview

After I began using PDFCompress for minimizing PDF files, the only reason I have had for using the full Adobe Acrobat has been to combine PDFs. Now I realize that since OS 10.5 this functionality has been built into Preview. I guess I should really start reading the release notes of OSes and applications a bit more carefully, since I managed to get to 10.6 before I found out about this feature.

Anyways, it is super easy to combine files: Just open in Preview and drag the icon(s) from one file on top of the icon in another (see demonstration).

Looking around for this solution I also discovered the free iCombiner application, which may be even easier to work with, and especially when having many PDF files that I want to combine (e.g. lots of chapters in a book).

So now I don’t really see any good reasons for upgrading my old Acrobat to a newer version any longer.