banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Thunar Custom Action Joins Multiple PDFs


by Paul Arnote (parnote)

Around our house, one of the kids' premiere questions every morning before school is, "What's for breakfast and lunch at school today, daddy?"



A lunch my kids would probably eat at school. Otherwise, it's "brown bag" time.

So, I find it easiest to keep a PDF file on my desktop that has all of the meal menus for their school, just so I can answer that daily question. I used to be able to just download the PDFs from their school's website, but last year they changed to some funky clunker that only allows you to "print off" the menus, one at a time. So, I've had to adapt.

Now I'm a HUGE fan/user of CUPS-PDF, but it really doesn't work well in this particular situation, since each menu (there's a separate menu for each meal at school) has the exact same name. Fortunately, Firefox's print dialog has an option to save the current document to a PDF file. So, by using that, I can give each menu a separate filename, where CUPS-PDF doesn't allow me to specify a name for each PDF file. Rather, with CUPS-PDF, the title of the page serves as the filename, and since they are all named the same, each downloaded menu would just overwrite the previous one. I know this because I've tried it.

Previously, when the school's website had pre-made PDF files of the school menus, they still had separate menus for each meal. But who wants to have to open multiple PDFs when you can get all of the information you're seeking by looking at just one PDF? So even back then, I would combine all of the downloaded PDF files into one contiguous PDF file that contained all of the menus.

And, until the past month, I would combine them all "manually" using command line tools from the command line. But that became an arduous task (ok … just more arduous than it should be), and the whole thing was just begging for simplification. I finally came up with a solution that works exceptionally well. That's not to say that there aren't other ways of doing the same thing, but it is what works very well for my use.

I had thought about trying to write a bash script, but that proved problematic for reasons I won't go into here. I needed two things. First, it had to allow me the opportunity to specify the output filename. Second, I had to have a way to select which PDFs I wanted to join together into one. What I came up with accomplishes both requirements, and is executed from a Thunar Custom Action "one liner."

Here's the one liner that I came up with:

P="$(zenity --title="PDF Joiner" --entry --text="Please enter the output file name:")"; pdfunite %N $P

So, "P" defines a Zenity dialog box that allows me to specify the filename for the output file. Once I click "OK" in the Zenity dialog box, pdfunite joins the selected PDF files (specified by the %N parameter, which is just the filenames of the selected files without any path information) into the single PDF file specified in the Zenity dialog box.



I chose pdfunite over pdfjoin because it has fewer command line options. With pdfjoin, you have to specify the output file by prefacing it with "-o" in the command line. With pdfunite, the last PDF file listed is the output file. You *can* use pdfjoin if you want, but you will have to be sure to have the pdftk tools installed. The pdfunite utility *should* already be on your system, since it comes from the Poppler developers, requiring nothing further to install.

If you choose to use pdfjoin, you will have to slightly alter the one-line command.

P="$(zenity --title="PDF Joiner" --entry --text="Please enter the output file name:")"; pdfjoin %N -o $P

I also chose to use Zenity for the dialog box because I liked its appearance better. I'm sure that yad can be configured to have a similar appearance, but I didn't want to spend a lot of time on making that happen. So, with that in mind, here's the yad dialog version of the same command, and it works just as well as the Zenity version.

P="$(yad --title="PDF Joiner" --entry --entry-label="Please enter the output file name:")"; pdfunite %N $P

Again, if you choose to use pdfjoin, you will have to slightly alter the one-line command.

P="$(yad --title="PDF Joiner" --entry --entry-label="Please enter the output file name:")"; pdfjoin %N -o $P

To set up the Thunar Custom Action, you might want to make your custom action dialog(s) look like mine.


images 2 and 3

The first dialog box, showing the "Basic" tab, should be fairly self explanatory. Under the "Appearance Conditions" tab, set the "File Pattern" field to "*.pdf;*.PDF" and put a checkmark in the "Other Files" selection.

To use your new Thunar Custom Action, press the CTRL key on the keyboard, and select the PDF files you want to join together. Then, right click your mouse over the last PDF file, and select "Join PDF Files" from the Thunar context menu. Your naming regimen will determine the order of appearance in the output PDF file, as the files appear to be "joined together" alphabetically or numerically by their file names. If you want to ensure that the individual PDF files appear in a specific order, rename the individual PDF files so that they are named in a sequential manner, using either numbers or letters of the alphabet to determine their proper order.

This custom action is F-A-S-T! In literally no time at all, you will have multiple PDF files joined into one PDF file. Plus, your original PDF files are left unscathed and unaltered (other than renaming them to ensure they appear in the correct order in the joined PDF).



Previous Page              Top              Next Page








Facebook-PCLinuxOS Magazine PCLinuxOS Fan Club Facebook Fan Club and PCLinuOS Magazine