我在渲染带有将report_Date参数传递到render_header方法的标头时遇到了问题.尽管每次传递不同的report_Date值,但PDF始终显示相同的日期,这不是预期的行为.

我的期望是PDF应该使用传递给render_header方法的report_Date值来呈现.然而,该方法中似乎没有正确使用report_Date参数.

      def render_pages(pdf)
        grouped_items = rows.group_by { |item| item[0] }

        grouped_items.each_with_index do |(report_date, booking_items), index|
          render_page(pdf, report_date, booking_items)
          pdf.start_new_page unless index == grouped_items.size - 1
        end
      end

      # Renders a page of the PDF
      #
      # @param pdf [Prawn::Document] The PDF object
      def render_page(pdf, report_date, booking_items)
        pdf.repeat(:all, dynamic: true) do
          pdf.bounding_box([pdf.bounds.left, pdf.bounds.top], width: pdf.bounds.width) do
            render_header(pdf, report_date, organisation_name)
          end
          pdf.bounding_box([pdf.bounds.left, pdf.bounds.bottom + 150 ], width: pdf.bounds.width) do
            render_footer(pdf)
          end
        end
        render_body(pdf, report_date , booking_items)
      end

      def render_header(pdf, report_date, organisation_name)
        pdf.image open("https://s3.amazonaws.com/www-inside-design/uploads/2019/05/woolmarkimagelogo-1024x576.png"), width: 80, height: 80, position: :center
        pdf.text "HEY #{report_date}"
        pdf.table(
          [
            ["#{"Sumanth Samala PVT LTD"} - #{format_report_date(report_date)} \n Attendance Register", "Customer to complete all sections below \n Generated on #{Time.now.strftime("%d/%m/%Y %H:%M")}"]
          ],
          width: pdf.bounds.width,
          row_colors: ["808080"],
          cell_style: header_cell_style
        ) do |table|
          table.column(0).width = 464
        end
      end
    ```

推荐答案

这是一个秘密,但太长了,无法发表 comments .

我们可以渲染所有页面,同时跟踪每个报告日期的开始页面和结束页面.

然后我们可以使用重复这些范围来生成标题.

def render_pages(pdf)
  grouped_items = rows.group_by(&:first)

  page_breaks = grouped_items.map.with_index(1) do |(report_date, booking_items), index|
    start_page = pdf.page_number
    render_page(pdf, report_date, booking_items)
    end_page = pdf.page_number 
    pdf.start_new_page unless index == grouped_items.size
    {pages: (start_page..end_page), report_date: report_date}
  end
  render_header_footer(pdf,page_breaks)
end

def render_page(pdf, report_date, booking_items)
  render_body(pdf, report_date , booking_items)
end

def render_header_footer(pdf,pages_headers)
  pages_headers.each do |page_data|
    pdf.repeat(page_data[:pages], dynamic: true) do
      pdf.bounding_box([pdf.bounds.left, pdf.bounds.top], width: pdf.bounds.width) do
        render_header(pdf, page_data[:report_date], organisation_name)
      end
      pdf.bounding_box([pdf.bounds.left, pdf.bounds.bottom + 150 ], width: pdf.bounds.width) do
        render_footer(pdf)
      end
    end
  end
end 

def render_header(pdf, report_date, organisation_name)
  pdf.image open("https://s3.amazonaws.com/www-inside-design/uploads/2019/05/woolmarkimagelogo-1024x576.png"), width: 80, height: 80, position: :center
  pdf.text "HEY #{report_date}"
  pdf.table(
    [
      ["#{"Sumanth Samala PVT LTD"} - #{format_report_date(report_date)} \n Attendance Register", "Customer to complete all sections below \n Generated on #{Time.now.strftime("%d/%m/%Y %H:%M")}"]
    ],
    width: pdf.bounds.width,
    row_colors: ["808080"],
    cell_style: header_cell_style
  ) do |table|
    table.column(0).width = 464
  end
end

Ruby相关问答推荐

使用 RSpec 测试嵌套哈希时随机排序数组的匹配

为什么在if语句中的赋值操作不会在条件判断之前发生?

碰撞检测测试中对象的 Nil 类

Ruby:如何将两个返回值连接到一行中的两个字符串

在Ruby中将嵌套哈希键从CamelCase转换为snake_case

Encoding::UndefinedConversionError

Ruby访问嵌套函数中的外部变量

Ruby 中是否有像 C 中一样的主要方法?

为什么在Ruby中用空格分隔的两个字符串连接在一起?

YAML 每个缩进多少个空格?

在现有 Jekyll 安装中切换主题

获取下周一(或一周中的任何一天)的日期的 Ruby 代码

如何在 Ruby 中获取命名空间中的所有类名?

野外的好黄瓜例子?

创建一个接受参数散列的 ruby​​ 方法

使用 RSpec 测试哈希内容

为什么在安装 gem 时出现权限被拒绝错误?

Ruby - 无法修改冻结的字符串 (TypeError)

Ruby:如何计算一个字符串在另一个字符串中出现的次数?

查找与给定条件匹配的元素的索引