lib/pdfmult.rb in pdfmult-1.2.0 vs lib/pdfmult.rb in pdfmult-1.3.0

- old
+ new

@@ -25,29 +25,33 @@ # # == Options # # -n, --number:: Number of copies to put on one page: 2 (default), 4, 8, 9, 16. # -# -f, --force:: Do not prompt before overwriting. +# -f, --[no-]force:: Do not prompt before overwriting. # # -l, --latex:: Create a LaTeX file instead of a PDF file (default: infile_NUMBER.tex). # # -o, --output:: Output file (default: infile_NUMBER.pdf). +# Use - to output to stdout. # # -p, --pages:: Number of pages to convert. # If given, +pdfmult+ does not try to obtain the page count from the source PDF. # +# -s, --[no-]silent:: Do not output progress information. +# # -h, --help:: Prints a brief help message and exits. # # -v, --version:: Prints a brief version information and exits. # # == Examples # # pdfmult sample.pdf # => sample_2.pdf (2 copies) # pdfmult -n 4 sample.pdf # => sample_4.pdf (4 copies) # pdfmult sample.pdf -o outfile.pdf # => outfile.pdf (2 copies) # pdfmult sample.pdf -p 3 # => processes 3 pages +# pdfmult sample.pdf -o - | lpr # => sends output via stdout to print command # # == Author # # Copyright (C) 2011-2012 Marcus Stollsteimer # @@ -56,21 +60,22 @@ require 'optparse' require 'tempfile' require 'fileutils' +require 'open3' -# This module contains the classes for the +pdfmult+ tool +# This module contains the classes for the +pdfmult+ tool. module Pdfmult PROGNAME = 'pdfmult' - VERSION = '1.2.0' - DATE = '2012-04-15' + VERSION = '1.3.0' + DATE = '2012-09-22' HOMEPAGE = 'https://github.com/stomar/pdfmult/' COPYRIGHT = "Copyright (C) 2011-2012 Marcus Stollsteimer.\n" + - "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" + + "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law." PDFLATEX = '/usr/bin/pdflatex' KPSEWHICH = '/usr/bin/kpsewhich' @@ -92,10 +97,11 @@ :force => false, :infile => nil, :latex => false, :number => 2, :outfile => nil, + :silent => false, :pages => nil } opt_parser = OptionParser.new do |opt| opt.banner = "Usage: #{PROGNAME} [options] file" @@ -136,30 +142,34 @@ opt.on('-n', '--number NUMBER', ['2', '4', '8', '9', '16'], Integer, 'Number of copies to put on one page: 2 (default), 4, 8, 9, 16.') do |n| options[:number] = n end - opt.on('-f', '--force', 'Do not prompt before overwriting.') do - options[:force] = true + opt.on('-f', '--[no-]force', 'Do not prompt before overwriting.') do |f| + options[:force] = f end opt.on('-l', '--latex', 'Create a LaTeX file instead of a PDF file (default: file_2.tex).') do options[:latex] = true end opt.on('-o', '--output FILE', String, - 'Output file (default: file_2.pdf).') do |f| + 'Output file (default: file_2.pdf). Use - to output to stdout.') do |f| options[:outfile] = f end opt.on('-p', '--pages NUMBER', Integer, 'Number of pages to convert.', "If given, #{PROGNAME} does not try to obtain the page count from the source PDF.") do |p| raise(OptionParser::InvalidArgument, p) unless p > 0 options[:pages] = p end + opt.on('-s', '--[no-]silent', 'Do not output progress information.') do |s| + options[:silent] = s + end + opt.separator '' end opt_parser.parse!(argv) # only input file should be left in argv @@ -196,11 +206,11 @@ CONTENT = "\\includepdf[pages={PAGES},nup=GEOMETRY]{FILENAME}%\n" FOOTER = - '\end{document}' + "\\end{document}\n" # Initializes a LaTeXDocument instance. # # +infile+ - input file name # +number+ - number of pages to put on one page @@ -228,11 +238,14 @@ geometry = '3x3' when 16 geometry = '4x4' end - content_template = CONTENT.gsub(/PAGES/, page_string).gsub(/GEOMETRY/, geometry).gsub(/FILENAME/, @infile) + content_template = CONTENT.gsub(/PAGES|GEOMETRY|FILENAME/, + 'PAGES' => page_string, + 'GEOMETRY' => geometry, + 'FILENAME' => @infile) content = HEADER.gsub(/CLASSOPTIONS/, class_options) @page_count.times do |i| content << content_template.gsub(/PAGE/,"#{i+1}") end @@ -297,10 +310,12 @@ usage_fail(e.message) end infile = options[:infile] outfile = options[:outfile] + use_stdout = (outfile == '-') + silent = options[:silent] # test for pdflatex installation unless options[:latex] message = 'seems not to be installed (you might try using the -l option)' general_fail("`#{PDFLATEX}' #{message}") unless command_available?("#{PDFLATEX} --version") @@ -310,11 +325,11 @@ # test input file usage_fail("no such file: `#{infile}'") unless File.exist?(infile) usage_fail("specified input not of the type `file'") unless File.ftype(infile) == 'file' # test for existing output file - if File.exist?(outfile) and !options[:force] + if !use_stdout and !options[:force] and File.exist?(outfile) overwrite_ok = ask("File `#{outfile}' already exists. Overwrite?") exit unless overwrite_ok end # set page number (get PDF info if necessary) @@ -324,21 +339,34 @@ # create LaTeX document document = LaTeXDocument.new(infile, options[:number], pages) if options[:latex] - open(outfile, 'w') {|f| f.write(document.to_s) } + if use_stdout + puts document.to_s + else + warn "Writing on #{outfile}." unless silent + open(outfile, 'w') {|f| f.write(document.to_s) } + end else Dir.mktmpdir('pdfmult') do |dir| - open("#{dir}/pdfmult.tex", 'w') do |f| - pdfpath = "#{dir}/pdfmult.pdf" - f.write(document.to_s) - f.flush - system("#{PDFLATEX} -output-directory #{dir} pdfmult.tex") - puts "Writing on #{outfile}." - FileUtils::mv(pdfpath, outfile) + texfile = 'pdfmult.tex' + pdffile = 'pdfmult.pdf' + open("#{dir}/#{texfile}", 'w') {|f| f.write(document.to_s) } + command = "#{PDFLATEX} -output-directory #{dir} #{texfile}" + Open3.popen3(command) do |stdin, stdout, stderr| + stdout.each_line {|line| warn line.chomp } unless silent # redirect progress messages to stderr + stderr.read # make sure all streams are read (and command has finished) end + if use_stdout + File.open("#{dir}/#{pdffile}") do |f| + f.each_line {|line| puts line } + end + else + warn "Writing on #{outfile}." unless silent + FileUtils::mv("#{dir}/#{pdffile}", outfile) + end end end end # Asks for yes or no (y/n). @@ -346,14 +374,14 @@ # +question+ - string to be printed # # Returns +true+ if the answer is yes. def self.ask(question) # :nodoc: while true - print "#{question} [y/n] " + $stderr.print "#{question} [y/n] " reply = $stdin.gets.chomp.downcase # $stdin: avoids gets / ARGV problem return true if reply == 'y' return false if reply == 'n' - puts "Please answer `y' or `n'." + warn "Please answer `y' or `n'." end end # Prints an error message and exits. def self.general_fail(message) # :nodoc: