Permalink
Browse files

Fix boxes for multiple pages.

  • Loading branch information...
1 parent 33feca8 commit 5044af30c26216aa7c10fabe3e10d677bc1d0b06 @smathieu smathieu committed Jul 18, 2011
Showing with 31 additions and 17 deletions.
  1. +19 −13 lib/pdf/label/batch.rb
  2. +1 −1 lib/pdf/version.rb
  3. +11 −3 test/test_pdf_label_page.rb
View
@@ -109,7 +109,7 @@ def add_many_labels(options = {})
=begin rdoc
To facilitate aligning a printer we give a method that prints the outlines of the labels
=end
- def draw_boxes(write_coord = true, draw_markups = true)
+ def do_draw_boxes(write_coord = true, draw_markups = true)
@layout.nx.times do |x|
@layout.ny.times do |y|
box_x, box_y = get_x_y(x, y)
@@ -124,29 +124,36 @@ def draw_boxes(write_coord = true, draw_markups = true)
end
if draw_markups
- @label.markupMargins.each {|margin|
+ @label.markupMargins.each do |margin|
size = margin.size.as_pts
@pdf.rounded_rectangle(box_x + size,
box_y - margin.size.as_pts,
@label.width.as_pts - 2*size,
@label.height.as_pts - 2*size,
@label.round.as_pts).stroke
- }
- @label.markupLines.each {|line|
+ end
+ @label.markupLines.each do |line|
@pdf.line(box_x + line.x1.as_pts,
box_y + line.y1.as_pts,
box_x + line.x2.as_pts,
box_y + line.y2.as_pts).stroke
- }
-=begin TODO Draw cirles
- @label.markupCircles.each {|cicle|
- @pdf.
-=end
+ end
end
-
end
end
end
+
+ private :do_draw_boxes
+
+ def draw_boxes(write_coord = true, draw_markups = true)
+ pdf.open_object do |heading|
+ pdf.save_state
+ do_draw_boxes(write_coord, draw_markups)
+ pdf.restore_state
+ pdf.close_object
+ pdf.add_object(heading, :all_pages)
+ end
+ end
=begin rdoc
add_label takes an argument hash.
@@ -225,10 +232,9 @@ def setup_add_label_options(options)
if position = options[:position]
# condition to handle multi-page PDF generation. If true, we're past the first page
if position > @zero_based_labels_per_page
+ position = position % @labels_per_page
# if remainder is zero, we're dealing with the first label of a new page
- @pdf.new_page if ((position) % @labels_per_page) == 0
- # Translate the position to a value between 1 and the number of labels for a given page
- position = position - (position/@labels_per_page)*@labels_per_page
+ @pdf.new_page if position.zero?
end
label_x, label_y = position_to_x_y(position)
elsif((label_x = options[:x]) && (label_y = options[:y]))
View
@@ -1,3 +1,3 @@
module PDF_LABELS
- VERSION = "2.0.4"
+ VERSION = "2.0.5"
end
@@ -77,20 +77,28 @@ def test_add_label_3_by_10
def test_add_label_3_by_10_multi_page
p = Pdf::Label::Batch.new("Avery 8160") # label is 2 x 10
+ p.draw_boxes(false, true)
p.add_label() # should add to col 1, row 1
p.add_label(:position => 1) # should add col 1, row 2
- p.add_label(:text => "Positoin 15", :position => 15) # should add col 2, row 1
+ p.add_label(:text => "Position 15", :position => 15) # should add col 2, row 1
#does the use_margin = true work?
p.add_label(:use_margin => true, :position => 4)
#with out the margin?
p.add_label(:text => 'No Margin', :position => 5, :use_margin => false)
p.add_label(:position => 48, :text => "This should be on a new page")
p.add_label(:position => 30, :text => "This should be first a page 2")
- p.draw_boxes(false, true)
#TODO Anybody out there think of a better way to test this?
p.save_as("#{ROOT}/test_add_multi_page.pdf")
end
+ def test_multipage_2
+ p = Pdf::Label::Batch.new("Avery 8160") # label is 2 x 10
+ p.draw_boxes(false, true)
+ 100.times do |i|
+ p.add_label(:position => i, :text => "Position #{i}") # should add col 1, row 2
+ end
+ p.save_as("#{ROOT}/test_add_multi_page2.pdf")
+ end
def test_add_many_labels
p = Pdf::Label::Batch.new("Avery 8160") # label is 2 x 10
@@ -140,7 +148,7 @@ def test_add_barcode_label
end
p.save_as("#{ROOT}/test_barcode_output.pdf")
end
-
+
def test_code39
p = Pdf::Label::Batch.new("Avery 8160") # label is 2 x 2
assert_equal "*HELLO123*", p.code39("hellO123")

0 comments on commit 5044af3

Please sign in to comment.