imgtopdf.rb / last modification: 2020-01-30 14:16
#!/usr/bin/env ruby

# program   : newimgtopdf
# copyright : PRAGMA Advanced Document Engineering
# version   : 2002-2006
# author    : Hans Hagen
#
# project   : ConTeXt / eXaMpLe
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-ade.com

unless defined? ownpath
  ownpath = $0.sub(/[\\\/]\w*?\.rb/i,'')
  $: << ownpath
end

require &#39;base/switch'
require &#39;base/logger'

require &#39;graphics/magick'

banner = [&#39;ImgToPdf', 'version 1.1.2', '2002-2006', 'PRAGMA ADE/POD']

class Commands

    include CommandBase

    # nowadays we would force a directive, but
    # for old times sake we handle default usage

    def main
        filename = @commandline.argument(&#39;first')

        if filename.empty? then
            help
        else
            convert
        end
    end

    # actions

    def convert

        magick = Magick.new(session)

        [&#39;compression','depth','colorspace','quality','inputpath','outputpath&#39;].each do |v|
            magick.setvariable(v,@commandline.option(v))
        end

        @commandline.arguments.each do |fullname|
            magick.setvariable(&#39;inputfile',fullname)
            magick.setvariable(&#39;outputfile',fullname.gsub(/(\..*?$)/io, '.pdf'))
            if @commandline.option(&#39;auto') then
                magick.autoconvert
            else
                magick.convert
            end
        end
    end

end

logger      = Logger.new(banner.shift)
commandline = CommandLine.new

commandline.registerflag(&#39;auto')

commandline.registervalue(&#39;compression')
commandline.registervalue(&#39;depth')
commandline.registervalue(&#39;colorspace')
commandline.registervalue(&#39;quality')

commandline.registervalue(&#39;inputpath')
commandline.registervalue(&#39;outputpath')


commandline.registeraction(&#39;help')
commandline.registeraction(&#39;version')

commandline.registeraction(&#39;convert', 'convert image into pdf')

commandline.expand

Commands.new(commandline,logger,banner).send(commandline.action || &#39;main')