Creating ready-to-print photo books with Ruby and TeX
Photo BookEngineering
![© Lead Image © Eric Reis, 123RF.com © Lead Image © Eric Reis, 123RF.com](/var/linux_magazin/storage/images/issues/2022/258/programming-snapshot-ruby-and-tex/123rf-erik_reis_headphones-images.png/803644-1-eng-US/123RF-Erik_Reis_headphones-images.png_medium.png)
© Lead Image © Eric Reis, 123RF.com
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.