Creating ready-to-print photo books with Ruby and TeX
Photo BookEngineering
Instead of composing photo books online, Mike Schilli prefers to use Ruby and TeX to program an application that generates print-ready PDF books.
I thought I had actually already given up printed books. Today, when I buy a paper book, typically used, I immediately send it to the paper cutting guillotine. The individual pages are then fed to my scanner and ultimately end up digitalized on my iPad, where I then read them.
But there is one type of paper book that I still cherish and even find preferable to digital formats: thick hardcover photography tomes that you might find scattered haphazardly on a little table in a hotel lobby or a vacation home. You just plop down in an armchair next to them, lean back, put your feet up, and start leafing through the high quality pages.
These books are known as coffee table books, probably because they are usually found on coffee tables. You can even create such works yourself with your own photos! Various online providers offer browser tools for uploading photos and laying them out across the pages of a virtual paper book. If you then press the send button and pay the bill, after roughly a week, your mailperson will deliver a high-quality book with a hard cover directly to your home (Figure 1).
Do it Yourself, Instead
The design options, however, are limited online. And, without versioning, you always worry that a poorly programmed application will drop the work you've invested so far into a black hole at any moment, forcing you to start all over again. How about a do-it-yourself program for home use that creates photo books as PDFs, which you can easily and cheaply have printed and bound in one fell swoop by print services like Lulu.com
?
In addition, why should a human being have to mess around with a mouse while arranging the pictures to get them all in line, when a typesetting program like TeX will automatically put the photos where they belong with a simple command? Of course, you can't expect users to battle with the quirky TeX syntax and type photo books in the text editor, using dangerous-looking commands that start with backslashes and require you to put any text body in curly braces.
Ruby Glue
With a little glue code in Ruby, even non-programmers can create print templates for their coffee table books. Listing 1 shows the definition of a photo book, and the contact print in Figure 2 shows you the PDF generated from it. You need to create one TeX template per page type (such as cover
for the cover and four
for a page with four pictures) and then add the associated photos as photos
and the captioning text as text
. Boom, the print()
method in line 35 outputs a PDF with the name defined in line 5 ("mybook"
), which a PDF viewer such as Evince will render on screen after you type:
evince mybook.pdf
Listing 1
mkbook.rb
01 #!/usr/bin/ruby 02 require_relative "bm" 03 04 b = Bm.new 05 b.name = "mybook" 06 07 b.add "intro" 08 09 b.add "cover", 10 text: "Unstoppable Mike Schilli", 11 photos: ["ring.jpg"] 12 13 b.add "chapter" 14 15 b.add "single", 16 text: "On the Beach", 17 photos: ["ob-jump.jpg"] 18 19 b.add "twotowers", 20 text: "Planet of the Apes", 21 photos: ["icpf.jpg", "icp.jpg"] 22 23 b.add "four", 24 text: "All Along the Coast", 25 photos: ["thornton-jump.jpg", 26 "beach-rock.jpg", 27 "heron.jpg", 28 "beach-trunk.jpg"] 29 30 b.add "chapter", 31 text: "The End" 32 33 b.add "outro" 34 35 b.print
Any online printing service will turn this into an impressive hardcover book in next to no time. Not bad!
Under the hood, the Ruby interface generates TeX snippets based on the previously defined templates and adds explanatory text and paths to the files of the photos to be embedded in the TeX code using variables. The print()
method in line 35 finally bundles everything together and calls the XeLaTeX program, which generates a professional-looking PDF from this. You can install XeLaTeX on Ubuntu, for example, by typing
sudo apt install texlive-xetex
XeLaTeX accepts TeX files, typesets the text, and keeps empty rectangles for the photos, matching their formats and typesetting specifications. The PDF driver then drops the JPEG photos into the rectangles, and you're done.
The advantage? Now the book description is available as code in a text file (new tech buzzword: CoC or Coffeebook as Code), which you can retrieve at any time, modify, or reset to a historical state using standard version control tools like git
.
Old-Timers: TeX and LaTeX
Of course, what TeX does under the hood is so complicated that it is often difficult to predict whether the photo layout in the template is actually going to produce what the author ordered. Besides, no one (except perhaps TeX author Knuth himself) writes TeX directly to typeset books anymore – that's usually done by a macro package like LaTeX, which is based on TeX. But finding the right magic formula in LaTeX to have it render a nice-looking page without making uninformed decisions often requires nerve-wracking tinkering with the code to finally get the TeX compiler to the point of producing the intended layout.
But once a template is set up and the kinks have been worked out, it will work until the end of time. The original book by TeX inventor Knuth [1] and the work of his LaTeX followers [2] are still worth reading today, if you can find them in an antiquarian bookstore. Experts still publish mathematical papers in LaTeX, and I once wrote a Perl book in LaTeX more than 20 years ago [3]. As I can tell you from experience, this particular choice definitely sets an author off on a path with an extremely steep learning curve, but if you value version control and repeatability without manual steps, you have very little choice – even today. Professional typesetting programs like Adobe InDesign or QuarkXPress still can't do anything automatically, even decades later.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.