Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

go_to_page does not restore margins #272

Closed
alexandergitter opened this Issue · 5 comments

3 participants

@alexandergitter

As shown in the following example, go_to_page does not restore the margins of the specified page.

Prawn::Document.generate('margins.pdf', :margin => [200, 200]) do
  stroke_bounds
  text "text on page 1"

  start_new_page(:margin => [100, 100])
  stroke_bounds
  text "text on page 2"

  go_to_page(1)
  text "this text is in a wrong position"
end

The problem seems to be in Prawn::Document.start_new_page. Rather than copying/cloning the current margins into the new page, the code passes a reference. This causes the margins of the current page to be overwritten.

Changing

last_page_margins = last_page.margins

into

last_page_margins = last_page.margins.dup

seems to fix this issue.

@alexandergitter

As it turns out my proposed change fixes the problem only partially. The following code still produces a wrong result

Prawn::Document.generate('margins.pdf', :margin => [200, 200]) do
  stroke_bounds
  text "text on page 1"

  start_new_page(:margin => [100, 100])
  stroke_bounds

  bounding_box([0, 500], :width => 300, :height => 300) do
    stroke_bounds
    text "text on page 2"

    start_new_page  # notice this page-break inside a bounding_box
    stroke_bounds
    text "text on page 3"
  end

  go_to_page(1)
  text "this text is in a wrong position"
end

Edit: Okay I found out how to "fix" this. In generate_margin_box replace

@bounding_box = @margin_box if old_margin_box == @bounding_box

with

@bounding_box = @margin_box

Now I'm sure there is a good reason for that if-statement. I just thought this might point someone in the right direction for a real fix.

@bradediger bradediger was assigned
@practicingruby

Re-opening because the problem does appear to exist. Unsure when I'll find time to investigate, though.

Research from others is welcome!

@practicingruby practicingruby added bug-confirmed and removed stale labels
@practicingruby practicingruby referenced this issue from a commit
@practicingruby practicingruby Restore margins on go_to_page()
We were not copying page margins when creating new pages, just
referencing the same hash used by previous pages. We should
really redesign this code to be mostly immutable, but this
fix will hopefully solve #272 for now.
c48af81
@practicingruby

Sorry for the loooong delay.@alexandergitter or anyone else who has experienced this problem, can you please confirm the fix in #794?

@alexandergitter

Looks good!

I tried both of my examples and they both work as expected. :+1: from me

@practicingruby

@alexandergitter Thanks! I'll try to get #794 merged soon, then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.