User:Horndude77/scripts

makepdf.rb

Convert a bunch of tiff files to a pdf. Uses the libtiff tools tiffcp and tiff2pdf.

#!/usr/bin/env ruby

#first - tiff which will be first page of pdf
#last - tiff while will be last page of pdf
#prefix - prefix of all tiff files
#filename - name of pdf file (no spaces)
def make_pdf(first, last, prefix, filename)
    unless(File.exists?(filename+'.pdf'))
        puts filename
        files = (first..last).to_a.map{|i| prefix+("%04d"%i)+'.tiff'}
        `tiffcp #{files.join(' ')} #{filename}.tiff`
        `tiff2pdf #{filename}.tiff -t"#{filename.gsub('_', ' ')}" -z -o #{filename}.pdf`
        `rm #{filename}.tiff`
    end
end

#Example:
#Files are named BookIV-0001.tiff through BookIV-0040.tiff
make_pdf(1, 40, 'BookIV-', "Schantl-School_for_the_Horn_Book_IV_a")

pdf2tiffs

Convert a pdf file to a bunch of tiffs. With this method you need to manually figure out the dpi.

#!/bin/sh
#usage $0 <input> <prefix> [other options]
PREFIX=$2
DPI=300x300
pdfimages $1 $PREFIX
for i in $PREFIX*.ppm
do
    cmd="convert -density $DPI -units PixelsPerInch $i $3 -monochrome -compress Group4 `echo $i | sed -e 's_\.[^.]*$_.tiff_'`"
    echo $cmd
    eval $cmd
done
for i in $PREFIX*.pbm
do
    cmd="convert -density $DPI -units PixelsPerInch $i $3 -compress Group4 `echo $i | sed -e 's_\.[^.]*$_.tiff_'`"
    echo $cmd
    eval $cmd
done
rm $PREFIX*.pbm $PREFIX*.ppm