Wednesday, February 13, 2019

Printing PDFs for tiny books

It's great being a programmer, would highly recommend it. So many things one can do when you leverage the power of scripting. The skillset helps you everything. For example, even with book binding! Say I totally, hypothetically, have some random PDF that you'd like to turn into a tiny book. Cool! How so?

STEP 1: PDF JAM!

The key to all of this is the pdfjam tool written by others far more talented than myself. That alone will let you craft your book, if you want to do it manually. Go to the linked page, follow the instructions, download & install it.

STEP 2: Grab my terrible script(s)

I have 2 sizes I like for my books... Both scripts print signatures that are 4 sheets to a signature, 8 pages folded, 16 pages front & back. Hopefully that make sense.

Option 1 is a 2x2 layout (so each page is 1/4th the sheet of paper) that can fold & nestle neatly. I like this and often don't cut the top pages apart until later. Often I'll wait till I sand/trim the top to separate the pages... Unfortunately I often find this to be "too big" for my tastes. But it's probably the easiest/most legible. [script].

Option 2 is a 4x4 layout (so that's a full signature per page) and really requires a tabloid size sheet of paper (11x17). I run my font really tiny, I suspect this is probably too small for most folks. The books I print this way are laid out specifically to be small, your average PDF wont be legible with this, even on tabloid. [script].

I'm just going to assume whoever is reading this is on a Mac because I can't help you with your Windows... but save the script (as a .sh file) and then make sure you can execute it. All this is going to happen on the terminal so... I hope you know how to work with that. I tend to choose "chmod 755 my_script_name.sh" to give it the correct permissions.

The script is going to ask for the PDF file name and how many pages it has. It'll then create a directory named "output_small" and dump all the individual (2 sided) pages created. If there was already a directory there, that'll get nuked. I wrote a terrible script and it can't handle page counts that don't line up with its exact layout so.... there's a very good chance it'll barf at the very end. It spews content onto the terminal and that last command will probably fail and you'll need to tweak it manually and execute it.... (Terrible, I know) Here's an example:


19:48 ~/directory/of/stuff>
19:48 ~/directory/of/stuff>chmod 755 ./small_book_pdf_jam.sh 
19:49 ~/directory/of/stuff>./small_book_pdf_jam.sh 

PDF file name:
jbic.pdf
PDF page count:
466
... looking at the following file:
-rw-r--r--@ 1 sithel  staff  1404186 Jan 26 16:22 jbic.pdf

Checking...
You will have 14 blank page(s) at the end
There will be 15 signatures
Dumping all old content from the directory 'output_small'
          ----
  pdfjam: This is pdfjam version 2.08.
  pdfjam: Reading any site-wide or user-specific defaults...
          (none found)
  pdfjam: Effective call for this run of pdfjam:
          /Library/TeX/texbin/pdfjam --suffix rotated180 --angle '180' --fitpaper 'true' --outfile output_small/temp_flipped.pdf -- jbic.pdf - 
  pdfjam: Calling pdflatex...
  pdfjam: Finished.  Output was to 'output_small/temp_flipped.pdf'.
Signature : 1 of 15
pdfjam --nup 4x4 output_small/temp_flipped.pdf '1,16,17,32' output_small/temp.pdf '4,13,20,29' output_small/temp_flipped.pdf '5,12,21,28' output_small/temp.pdf '8,9,24,25' output_small/temp_flipped.pdf '31,18,15,2' output_small/temp.pdf '30,19,14,3' output_small/temp_flipped.pdf '27,22,11,6' output_small/temp.pdf '26,23,10,7' --outfile output_small/temp_sig_0.pdf
          ----
 .... goes like this for a while, successfully, UNTIL!!! 
          ----
  pdfjam: This is pdfjam version 2.08.
  pdfjam: Reading any site-wide or user-specific defaults...
          (none found)
  pdfjam: Effective call for this run of pdfjam:
          /Library/TeX/texbin/pdfjam --nup '4x4' --outfile output_small/temp_sig_448.pdf -- output_small/temp_flipped.pdf 449,464,465,480 output_small/temp.pdf 452,461,468,477 output_small/temp_flipped.pdf 453,460,469,476 output_small/temp.pdf 456,457,472,473 output_small/temp_flipped.pdf 479,466,463,450 output_small/temp.pdf 478,467,462,451 output_small/temp_flipped.pdf 475,470,459,454 output_small/temp.pdf 474,471,458,455 
  pdfjam: Calling pdflatex...
  pdfjam: FAILED.
          The call to 'pdflatex' resulted in an error.
          If '--no-tidy' was used, you can examine the
          log file at
                  /var/tmp/pdfjam-sk2QxI/a.log
          to try to diagnose the problem.
  pdfjam ERROR: Output file not written
19:50 ~/directory/of/stuff>

You gotta' take that last command and then put {} for each invalid page number... given that I only have 466 of them it'd then look like: /Library/TeX/texbin/pdfjam --nup '4x4' --outfile output_small/temp_sig_448.pdf -- output_small/temp_flipped.pdf 449,464,465,{} output_small/temp.pdf 452,461,4{},{} output_small/temp_flipped.pdf 453,460,{},{} output_small/temp.pdf 456,457,{},{} output_small/temp_flipped.pdf {},466,463,450 output_small/temp.pdf {},467,462,451 output_small/temp_flipped.pdf {},{},459,454 output_small/temp.pdf {},{},458,455 (Gross, I know. Very embarrassing)

Now that you've got those signatures you can just print each file out individually OR you can pull one smaller script and run that to glue them together [script]. Just does some basic math for you and sticks 'em into one file called 'book.pdf' in that same directory.

See... it's... not that bad... I printed several books this way. There's a whole 'nother step where you wrestle with your printer quality, paper quality, text legibility and all that... but that's for another day.