Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.58 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'Prawn'
  3. require 'tempfile'
  4. require 'csv'
  5.  
  6. abort "Usage: #{$0} <csv file> <start label number>" unless ARGV[1]
  7.  
  8. def generate_avery_8366_labels(output_filename)
  9.   pdf = Prawn::Document.new( page_size: 'LETTER', left_margin: 15, bottom_margin: 35 )
  10.  
  11.   labels_high = 10
  12.   labels_wide = 3
  13.  
  14.   label_height = 72
  15.   label_width = 189
  16.  
  17.   horizontal_margin = 0
  18.   vertical_margin = 0
  19.  
  20.   column_gutter = 9
  21.   row_gutter = 0
  22.  
  23.   label_pad = 0
  24.  
  25.   for row_position in 0..labels_high-1
  26.     for column_position in 0..labels_wide-1
  27.       top_left_x = horizontal_margin + (label_width + column_gutter)*column_position
  28.       top_left_y = 720 - (label_height + row_gutter)*(row_position)
  29.       pdf.bounding_box( [top_left_x,top_left_y], :width => label_width, :height => label_height) do
  30.         pdf.stroke_color '000000'
  31.         pdf.fill_color '000000'
  32.         pdf.stroke_bounds
  33.  
  34.         pdf.bounding_box( [label_pad, label_height-label_pad], :width => label_width-2*label_pad, :height => label_height-2*label_pad) do
  35.           pdf.stroke_color '000000'
  36.           pdf.fill_color '000000'
  37.           yield pdf,(row_position*labels_wide + column_position)+1
  38.         end
  39.       end
  40.     end
  41.   end
  42.  
  43.  
  44.   pdf.render_file output_filename
  45. end
  46.  
  47. skip = ARGV[1].to_i
  48.  
  49. labels = []
  50.  
  51. 1.upto(skip) { labels << nil }
  52.  
  53. CSV.foreach(ARGV[0]) do |row|
  54.   next if row[0] == ''
  55.   labels << row
  56. end
  57.  
  58. puts "need at least #{labels.count-1} labels"
  59.  
  60.  
  61. output_files = []
  62.  
  63. while not labels.empty?
  64.   filename = Tempfile.new
  65.   generate_avery_8366_labels( filename.path ) do |pdf,number|
  66.     label = labels.shift
  67.     next unless label
  68.    
  69.     # channel label background color
  70.     pdf.fill_color '00ff00'
  71.     pdf.fill_rectangle [0, 72], 18, 72
  72.     pdf.fill_color '000000'
  73.  
  74.     # color label background color
  75.     pdf.fill_color 'ff0000'
  76.     pdf.fill_rectangle [18, 36], 76.5, 36
  77.     pdf.fill_color '000000'
  78.  
  79.     pdf.horizontal_line 94.5,150, :at => 18
  80.     pdf.horizontal_line 18,189, :at => 36
  81.     pdf.horizontal_line 18,189, :at => 54
  82.    
  83.     pdf.vertical_line 0, 72, :at => 18
  84.     pdf.vertical_line 0, 54, :at => 94.5
  85.     pdf.vertical_line 0, 36, :at => 150
  86.    
  87.  
  88.     pdf.text_box "Inst Type & Access", :at => [18, 72], :align  => :center, :valign => :center, :width  => 171, :height => 18, :size => 17, :overflow => :shrink_to_fit
  89.     pdf.text_box "Load", :at => [18, 54], :align  => :center, :valign => :center, :width  => 76.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
  90.     pdf.text_box "Color", :at => [18, 36], :align  => :center, :valign => :center, :width  => 76.5, :height => 36, :size => 32, :overflow => :shrink_to_fit
  91.     pdf.text_box "Pos.", :at => [94.5, 54], :align  => :center, :valign => :center, :width  => 94.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
  92.     pdf.text_box "Dim", :at => [94.5, 36], :align  => :center, :valign => :center, :width  => 55.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
  93.     pdf.text_box "Addr", :at => [94.5, 18], :align  => :center, :valign => :center, :width  => 55.5, :height => 18, :size => 17, :overflow => :shrink_to_fit
  94.     pdf.text_box "UN", :at => [150, 36], :align  => :center, :valign => :center, :width  => 39, :height => 36, :size => 17, :overflow => :shrink_to_fit
  95.     pdf.text_box "Chan", :at => [3,0], :align  => :center, :valign => :center, :width  => 72, :height => 17, :size => 17, :overflow => :shrink_to_fit, :rotate => 90
  96.    
  97.  
  98.    
  99.   end
  100.   output_files << filename.path
  101.  
  102. end
  103.  
  104. %x(pdftk #{output_files.join ' '} cat output prawn_test.pdf)
  105.  
  106. puts "output files is #{output_files}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement