#!/usr/bin/ruby -w # jpdf.rb v.0.2 # Fri Dec 28 13:59:05 CET 2007 # (c)2007 Andrea Ganduglia # GPL 2.0 Software # CONFIG PRINTER = 'virtual' SPOOL = '/home/alibi/PDF' TMPDIR = '/tmp' #REQUIRES require 'optparse' require 'ostruct' require 'fileutils' require 'tempfile' require 'pp' # TODO # DEFAULT Opzione -f forza il fatto che se il file out e` esistente viene sovrascritto # altrimenti il vecchio file viene fatto routare. # # 3. Opzione --page-ranges e --page-up (low) # # 6. Scrivere breve howto su come impostare una stampante cups-pdf funzionante da linea di # comando: http://cups.org/doc-1.1/sam.html#4_2_1 #################################### ### FUNCTIONS ###################### #################################### def checkfiles(arr) arr.each_with_index do |file,i| i=i+1 if(File.file?(file[0]) == false) puts "**ERROR: file n. #{i} `#{file[0]}' does not exist or it isn't a regular file. Stop." exit 1 end end return true end # UniqID: please don't use '.' def uuniqid() now = Time.new id = now.to_i.to_s + '-' + now.usec.to_s + '-' + rand(1000).to_s return id end # Make the tmp file def maketempfile() out = Tempfile.new("jpdf",TMPDIR) out << "Nel mezzo del cammin di nostra vita\nmi ritrovai per una selva oscura\nche la diritta via era smarrita\n" out.close return out.path end def waiter() tmpfile = maketempfile() label = uuniqid() outfile = "#{SPOOL}/#{label}.pdf" system("lpr -P#{PRINTER} -T#{label} #{tmpfile}") until(File.exist?(outfile) != false) sleep 0.2 end system("rm -f #{outfile}") return true end #################################### ### OPTIONS ######################## #################################### options = OpenStruct.new options.title = nil options.files = [] options.files_list = [] options.files_temp = [] options.joinpdf = false options.outdir = `pwd`.strip.gsub(/\/+/,"/") opts = OptionParser.new do |opts| opts.banner = "Usage: #{$0.split("/")[-1]} [options] [file(s)]" opts.on("-T","--title","output file name") do |title| options.title = ARGV[0] ARGV.shift end opts.on("--out-dir","output directory name") do |outdir| options.outdir = ARGV[0] ARGV.shift end opts.on("-h", "--help", "print this help") do puts opts puts "" puts "Spiegazione" exit 1 end end # OPTIONS DEBUG if (ARGV.length == 0) || (ARGV.include? "-h") || (ARGV.include? "--help") opts.parse(["-h"]) exit 1 end begin opts.parse!(ARGV) rescue Exception => e puts e, "", opts.parse!(["-h"]) exit 1 end if ARGV.size == 0 opts.parse!(["-h"]) exit 1 end #################################### ### BUILDING AND CHECKS ############ #################################### # FILES ARGV.each do |file| file = file.split("::") if (file[1] != nil) && (options.joinpdf == false) options.joinpdf = true end options.files << [ file[0].to_s, file[1].to_s, nil ] end # FILES First check checkfiles(options.files) # Check outdir will be writable unless (File.directory?(options.outdir) == true) && (File.writable_real?(options.outdir) == true) puts "**ERROR: Sorry, `#{options.outdir}' is not writable! Stop." exit 1 end # Check title if options.title == nil options.title = ARGV[0].split("/")[-1].to_s if options.title == nil || options.title == "" puts "**ERROR: I cannot determ output file. Stop." exit 1 end end # Reforming title according to cups-pdf options (please check /etc/cups/cups-pdf.conf) options.title = options.title.strip if options.title =~ /.{1,}\..{1,}/ options.title = options.title.split(".")[0..-2].join("_") else options.title = options.title.gsub(".","_") end # Build outfile and finalfile options.outfile = "#{SPOOL}/#{options.title}.pdf".gsub(/\/+/,"/") options.finalfile = "#{options.outdir}/#{options.title}.pdf".gsub(/\/+/,"/") if ("#{`pwd`.strip}/#{ARGV[0].split("/")[-1].to_s}" == options.finalfile) puts "**ERROR: Sorry input and output file are the same file. Stop." exit 1 end ################################# ##### PRINTING ################## ################################# # Now print on tmp files pages of chains if options.joinpdf == true puts " " puts "=> Now print temp files" options.files.each_with_index do |file,i| if file[1] != nil label = uuniqid() outfile = "#{SPOOL}/#{label}.pdf" system("lpr -P#{PRINTER} -T#{label} -o page-ranges=#{file[1]} #{file[0]}") print "* Pages #{file[1]} of `#{file[0]}' " until(waiter() != false) print "." sleep 0.1 end print " Done.\n" options.files[i][0] = outfile options.files_temp << outfile end end # Re-check file(s) checkfiles(options.files) end # Build final list of files options.files.each do |file| options.files_list << file[0] end # Notify begin of final print puts " " puts "=> Now print final file." sleep 1 # Print final file system("lpr -P#{PRINTER} -T#{options.title} #{options.files_list.join(" ")}") print "* #{options.outfile} " until(waiter() != false) print "." sleep 0.1 end print " Done.\n" # Clear from tmp files if options.joinpdf == true && options.files_temp.size > 0 puts " " puts "=> Now deleting tmp files" options.files_temp.each do |clear| File.unlink(clear) puts "* Removed `#{clear}'" end end # Move final file if (FileUtils.move(options.outfile,options.finalfile) == 0) puts " " puts "=> Your file is ready: #{options.finalfile}" end