2

I have a pdf being generated by prawn, it contains a header, a table and a footer. Both header and footer are repeatable, with :dynamic => true, the table is not.

The problem I'm having is that all content on the header and footer is displaying as if it's being rendered twice in the same place, and this seems to mess with the anti aliasing function so the text appears to be choppy and bold (see image below). However, when I zoom in or print it, it looks right.

Here's some code of the specific part where I start the repeater blocks:

pdf = Prawn::Document.new :page_size => "A4", :margin => [28, 20, 7, 20]

(...)

pdf.repeat :all, :dynamic => true do
  pdf.bounding_box([0, 803], :width => 555, :height => 60) do
    pdf.stroke_bounds

    (...)
  end
end

(...)

pdf.render
# End of file

As far as I know, this is the default way to declare a repeater block, I almost mirrorred it from the manual.

Here's the image: the top container is the header, inside the repeater, it looks like everything is bold, but it's the exact same font and line width as the section below, which is from the table, and looks fine. Sorry I can't post a larger part of the pdf, it's for confidentiality reasons.

Division between wrong and correct sections

If I remove the repeater, the header will look correct, as it should, but when I add it again it looks like this.

Has anyone else encountered this issue before? How do I fix it?

EDIT: Added some more code above. Also added the code below, which is inside a Rails template file, "historico.pdf.prawn", and this is how I call it on the controller to render the PDF and return it to the user:

rendered_pdf = render_to_string :template => "reports/historico.pdf"
send_data rendered_pdf, :filename => "Historico.pdf", :type => "application/pdf"
Rodrigo Castro
  • 1,573
  • 14
  • 38

1 Answers1

2

When you use pdf.repeat :all the pdf is 'reopened' after the document is already created and the data within the repeat block is added. This can cause a myriad of odd behavior, I believe this is what is causing your issue.

There is a related issue with a solution that utilizes Prawn's canvas method. If you adapt this solution to your problem (use canvas to generate your header and footer, rather than repeat :all) , you should no longer have these text-on-top-of-text problems.

Note:

It may be important for you to know that Prawn has officially stated that they are, "not in a good position to support templating features (I bring this up because you appear to be creating a template), " so similar workarounds may be needed if you continue to use this tool for these types of needs. Depending on your dependance on these types of templating features, you may want to look into using a different tool.

Community
  • 1
  • 1
Ecnalyr
  • 5,792
  • 5
  • 43
  • 89
  • I'm using a .pdf.prawn Rails template, but I'm not calling it the same way the guy in the related issue is. I'll try this canvas method and see if it'll work, thanks. I'll also edit the question to include how I'm calling it on the controller – Rodrigo Castro Jan 13 '15 at 17:27
  • I tried this canvas method, but without using it with `repeat :all`, it only prints its content on the first page, it doesn't replicate on the other pages, so it doesn't solve my problem. I don't know how @Bramski did it, but the way he put in his own answer shouldn't even fix the problem for him. – Rodrigo Castro Jan 13 '15 at 17:47
  • This may seem silly at this point, but it may be worth trying to use `repeat :all` wrapped around a canvas method, like this: http://stackoverflow.com/a/2919309/1026898 . – Ecnalyr Jan 14 '15 at 15:18
  • Tried it, got the same results. Wasn't expecting any difference anyway, I read the source code and found out that canvas is just an absolute positioned bounding box, the processing and rendering should be the same. Thank you anyway, at least I learned some new things about Prawn – Rodrigo Castro Jan 15 '15 at 18:10