Class: OpenPrint
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#lp_cmd ⇒ Object
Returns the value of attribute lp_cmd.
Class Method Summary collapse
-
.pdf_join(files, outfile) ⇒ Object
Input: 1..n files Output: one PDF-file.
-
.pdf_nup_duplex(files, base = nil) ⇒ Object
Input: 1..n files with 2 pages, front and back Output: one 2x2-nup PDF front one 2x2-nup PDF back to be printed “long edge duplex”.
- .replace(doc, fields) ⇒ Object
Instance Method Summary collapse
- #get_content ⇒ Object
-
#initialize(file, dir = nil) ⇒ OpenPrint
constructor
A new instance of OpenPrint.
- #make_pdf(fields = [], counter = nil, name = nil) ⇒ Object
- #print(fields = [], counter = nil, name = nil) ⇒ Object
- #print_file(pdf_file) ⇒ Object
- #print_hash(fields, counter = nil, name = nil) ⇒ Object
- #print_join(files, outfile = nil) ⇒ Object
- #replace_accents(str) ⇒ Object
Constructor Details
#initialize(file, dir = nil) ⇒ OpenPrint
Returns a new instance of OpenPrint.
13 14 15 16 17 18 19 |
# File 'lib/qooxview/helpers/open_print.rb', line 13 def initialize(file, dir = nil) @file = file @dir = dir @base = File.basename file @lp_cmd = nil @counter = 0 end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
11 12 13 |
# File 'lib/qooxview/helpers/open_print.rb', line 11 def file @file end |
#lp_cmd ⇒ Object
Returns the value of attribute lp_cmd.
11 12 13 |
# File 'lib/qooxview/helpers/open_print.rb', line 11 def lp_cmd @lp_cmd end |
Class Method Details
.pdf_join(files, outfile) ⇒ Object
Input: 1..n files Output: one PDF-file
144 145 146 147 |
# File 'lib/qooxview/helpers/open_print.rb', line 144 def self.pdf_join(files, outfile) files.class == Array or files = [files] System.run_bool "pdfjam --quiet --landscape --outfile #{outfile} #{files.join(' ')}" end |
.pdf_nup_duplex(files, base = nil) ⇒ Object
Input: 1..n files with 2 pages, front and back Output: one 2x2-nup PDF front
one 2x2-nup PDF back
to be printed "long edge duplex"
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/qooxview/helpers/open_print.rb', line 116 def self.pdf_nup_duplex(files, base = nil) Dir.mktmpdir { |dir| files.class == Array or files = [files] allpages = "#{dir}/allpages.pdf" cmd = "pdfjam --quiet --landscape --outfile #{allpages} " + files.join(" '1,2' ") + " '1,2,{},{}' " dputs(3) { "Running allpages-command #{cmd}" } %x[ #{cmd} ] pages_all = ((files.count + 1) & ~1).times documents = {:front => pages_all.collect { |i| (i ^ 1) * 2 + 1 }.join(','), :back => pages_all.collect { |i| i * 2 + 2 }.join(',')} base ||= File.basename files.first, '.pdf' documents.collect { |suffix, pages| pages_file = "/tmp/#{base}-#{suffix}.pdf" cmd = 'pdfnup --quiet --landscape --nup 2x2 ' + "#{allpages} #{pages} --outfile #{pages_file}" dputs(3) { "Running pdfnup-command #{cmd}" } %x[ #{cmd} ] pages_file } } end |
.replace(doc, fields) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/qooxview/helpers/open_print.rb', line 55 def self.replace(doc, fields) fields.each { |old, new| doc.gsub!(old, new.to_s) # For every -TAG- there can be a _TAG_ which is replaced with the # uppercase-version of 'new' old_up = old.inspect.sub(/^.-(.*)-.$/, '_\1_') doc.gsub!(old_up, new.to_s.upcase) } doc end |
Instance Method Details
#get_content ⇒ Object
37 38 39 40 41 42 |
# File 'lib/qooxview/helpers/open_print.rb', line 37 def get_content return unless File.exists? @file Zip::File.open(@file) { |z| z.read('content.xml').force_encoding(Encoding::UTF_8) } end |
#make_pdf(fields = [], counter = nil, name = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/qooxview/helpers/open_print.rb', line 66 def make_pdf(fields = [], counter = nil, name = nil) dputs(3) { "New print for -#{@file.inspect}-" } if name tmp_file = "/tmp/#{replace_accents(name)}.#{@base.sub(/.*\./, '')}" else counter ||= @counter tmp_file = "/tmp/#{counter}-#{@base}" @counter += 1 end pdf_file = tmp_file.sub(/[^\.]*$/, 'pdf') dputs(3) { "Copying to -#{tmp_file.inspect}-" } FileUtils::cp(@file, tmp_file) Zip::File.open(tmp_file) { |z| doc = z.read('content.xml').force_encoding(Encoding::UTF_8) OpenPrint.replace(doc, fields) # Replace dates that need to be calculated with a '_' doc.gsub!(/(table:formula=[^<>]*date-value=").*?"/, '\1_\2"') z.get_output_stream('content.xml') { |f| f.write(doc) } z.commit } if ConfigBase.openprint_simul == %w(false) Docsplit.extract_pdf tmp_file, :output => '/tmp' #FileUtils::cp( tmp_file, pdf_file ) dputs(5) { 'Finished docsplit' } else FileUtils::cp(tmp_file, pdf_file) end @dir and FileUtils::cp(pdf_file, @dir) pdf_file end |
#print(fields = [], counter = nil, name = nil) ⇒ Object
101 102 103 104 |
# File 'lib/qooxview/helpers/open_print.rb', line 101 def print(fields = [], counter = nil, name = nil) file = make_pdf(fields, counter, name) print_file(file) end |
#print_file(pdf_file) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/qooxview/helpers/open_print.rb', line 44 def print_file(pdf_file) if @lp_cmd dputs(2) { "Printing with --#{@lp_cmd} #{pdf_file}--" } System.run_bool("#{@lp_cmd} #{pdf_file}") return true else # Download PDF return "#{pdf_file}" end end |
#print_hash(fields, counter = nil, name = nil) ⇒ Object
33 34 35 |
# File 'lib/qooxview/helpers/open_print.rb', line 33 def print_hash(fields, counter = nil, name = nil) print(fields.collect { |k, v| [/--#{k}--/, v] }, counter, name) end |
#print_join(files, outfile = nil) ⇒ Object
106 107 108 109 110 |
# File 'lib/qooxview/helpers/open_print.rb', line 106 def print_join(files, outfile = nil) outfile ||= files.first.sub(/.pdf$/, '-joined.pdf') OpenPrint.pdf_join(files, outfile) print_file(outfile) end |
#replace_accents(str) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/qooxview/helpers/open_print.rb', line 21 def replace_accents(str) str = str.downcase.gsub(/ /, '_') accents = Hash[*%w( a àáâä e éèêë i ìíîï o òóôöœ u ùúûü c ç ss ß )] dputs(4) { "str was #{str}" } accents.each { |k, v| str.gsub!(/[#{v}]/, k) } str.gsub!(/[^a-z0-9_-]/, '_') dputs(4) { "str is #{str}" } str end |