ddengler / prawn-grouping

A gem to add a more flexible grouping option to the prawn gem
https://github.com/ddengler/prawn-grouping
MIT License
14 stars 21 forks source link

Getting values two times #28

Closed janpeterka closed 8 months ago

janpeterka commented 9 months ago

Hi, I just started with prawn and prawn-grouping, and I'm having this problem:

When I use group, everything inside it get's rendered twice. Here's minimal example code and screenshot:

 def pdf
    event = @event
    Prawn::Document.new do
      event.daily_plans.each do |day|
        text day.date.to_s, size: 20, style: :bold
        group do
          day.recipes.each do |recipe|
            text recipe.id.to_s, size: 16, style: :bold
          end
        end
      end
    end
  end

Screenshot from 2023-12-13 18-50-38

woodwardjd commented 8 months ago

I ran into a similar issue today and then noticed this section of the README. I think you're looking for that, like:

require 'bundler/setup'
require 'ostruct'
require 'prawn'
require 'prawn/grouping'

def pdf(event)
  Prawn::Document.new do
    event.daily_plans.each do |day|
      text day.date.to_s, size: 20, style: :bold
      group do |group_pdf|
        day.recipes.each do |recipe|
          group_pdf.text recipe.id.to_s, size: 16, style: :bold
        end
      end
    end
  end
end

event = OpenStruct.new({
  daily_plans: [
    OpenStruct.new({ date: '2024-08-30', recipes: [ OpenStruct.new({id: 158}) ] }),
    OpenStruct.new({ date: '2024-08-31', recipes: [ OpenStruct.new({id: 236}), OpenStruct.new({id: 220}) ] }),
    OpenStruct.new({ date: '2024-09-01', recipes: [ OpenStruct.new({id: 168}), OpenStruct.new({id: 221}), OpenStruct.new({id: 144}), OpenStruct.new({id: 131}) ] })
  ]
})
pdf(event).render_file 'out.pdf'
janpeterka commented 8 months ago

Thanks @woodwardjd, didn't notice this in README!