Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'Prawn'
- require 'tempfile'
- require 'csv'
- abort "Usage: #{$0} <csv file> <start label number>" unless ARGV[1]
- def generate_avery_8366_labels(output_filename)
- pdf = Prawn::Document.new( page_size: 'LETTER', left_margin: 15, bottom_margin: 35 )
- labels_high = 10
- labels_wide = 3
- label_height = 72
- label_width = 189
- horizontal_margin = 0
- vertical_margin = 0
- column_gutter = 9
- row_gutter = 0
- label_pad = 0
- for row_position in 0..labels_high-1
- for column_position in 0..labels_wide-1
- top_left_x = horizontal_margin + (label_width + column_gutter)*column_position
- top_left_y = 720 - (label_height + row_gutter)*(row_position)
- pdf.bounding_box( [top_left_x,top_left_y], :width => label_width, :height => label_height) do
- pdf.stroke_color '000000'
- pdf.fill_color '000000'
- pdf.stroke_bounds
- pdf.bounding_box( [label_pad, label_height-label_pad], :width => label_width-2*label_pad, :height => label_height-2*label_pad) do
- pdf.stroke_color '000000'
- pdf.fill_color '000000'
- yield pdf,(row_position*labels_wide + column_position)+1
- end
- end
- end
- end
- pdf.render_file output_filename
- end
- skip = ARGV[1].to_i
- labels = []
- 1.upto(skip) { labels << nil }
- CSV.foreach(ARGV[0]) do |row|
- next if row[0] == ''
- labels << row
- end
- puts "need at least #{labels.count-1} labels"
- output_files = []
- while not labels.empty?
- filename = Tempfile.new
- generate_avery_8366_labels( filename.path ) do |pdf,number|
- label = labels.shift
- next unless label
- # channel label background color
- pdf.fill_color '00ff00'
- pdf.fill_rectangle [0, 72], 18, 72
- pdf.fill_color '000000'
- # color label background color
- pdf.fill_color 'ff0000'
- pdf.fill_rectangle [18, 36], 76.5, 36
- pdf.fill_color '000000'
- pdf.horizontal_line 94.5,150, :at => 18
- pdf.horizontal_line 18,189, :at => 36
- pdf.horizontal_line 18,189, :at => 54
- pdf.vertical_line 0, 72, :at => 18
- pdf.vertical_line 0, 54, :at => 94.5
- pdf.vertical_line 0, 36, :at => 150
- pdf.text_box "Inst Type & Access", :at => [18, 72], :align => :center, :valign => :center, :width => 171, :height => 18, :size => 17, :overflow => :shrink_to_fit
- pdf.text_box "Load", :at => [18, 54], :align => :center, :valign => :center, :width => 76.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
- pdf.text_box "Color", :at => [18, 36], :align => :center, :valign => :center, :width => 76.5, :height => 36, :size => 32, :overflow => :shrink_to_fit
- pdf.text_box "Pos.", :at => [94.5, 54], :align => :center, :valign => :center, :width => 94.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
- pdf.text_box "Dim", :at => [94.5, 36], :align => :center, :valign => :center, :width => 55.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
- pdf.text_box "Addr", :at => [94.5, 18], :align => :center, :valign => :center, :width => 55.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
- pdf.text_box "UN", :at => [150, 36], :align => :center, :valign => :center, :width => 39, :height => 36, :size => 17, :overflow => :shrink_to_fit
- pdf.text_box "Chan", :at => [3,0], :align => :center, :valign => :center, :width => 72, :height => 17, :size => 17, :overflow => :shrink_to_fit, :rotate => 90
- end
- output_files << filename.path
- end
- %x(pdftk #{output_files.join ' '} cat output prawn_test.pdf)
- puts "output files is #{output_files}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement