Creating ready-to-print photo  books  with  Ruby and TeX

Photo BookEngineering

© Lead Image © Eric Reis,

© Lead Image © Eric Reis,

Article from Issue 258/2022

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).

Figure 1: A photo book created by the author and printed online.

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

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


01 #!/usr/bin/ruby
02 require_relative "bm"
04 b =
05 = "mybook"
07 b.add "intro"
09 b.add "cover",
10   text: "Unstoppable Mike Schilli",
11   photos: ["ring.jpg"]
13 b.add "chapter"
15 b.add "single",
16   text: "On the Beach",
17   photos: ["ob-jump.jpg"]
19 b.add "twotowers",
20   text: "Planet of the Apes",
21   photos: ["icpf.jpg", "icp.jpg"]
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"]
30 b.add "chapter",
31   text: "The End"
33 b.add "outro"
35 b.print
Figure 2: A PDF viewer displays the finished photo book, which is ready to be printed.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Workspace: Netbook Photo Tools

    Find out how to turn your netbook into a fabulous photo tool.

  • Jekyll

    Most people starting a blog will almost automatically turn to WordPress, which calls for PHP, a database, and regular security updates … and thus a lot of attention. With the small Jekyll template engine, you can avoid all that.

  • FotoInsight Designer 4.5

    The newly released version 4.5 of the digital photo service is compatible with Linux and Mac operating systems and comes with geotagging and other new features.

  • Perl: Elasticsearch

    The Elasticsearch full-text search engine quickly finds expressions even in huge text collections. With a few tricks, you can even locate photos that have been shot in the vicinity of a reference image.

  • Books


comments powered by Disqus
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.

Learn More