thinreports / thinreports-generator

A Ruby library for Thinreports
https://github.com/thinreports/thinreports
MIT License
297 stars 48 forks source link

Generate multi times header ? #97

Closed TranBaVinhSon closed 5 years ago

TranBaVinhSon commented 5 years ago

I want to generate my pdf header (with title and subtitle ) just once but How can I do it with thinreport ? This is my current code

def generate_pdf_report
    report = Thinreports::Report.new layout: File.join(Rails.root, 'app', 'reports', 'template.tlf')
    pages = 0
    report.start_new_page

    report.page.item(:name).value("name")
    report.page.item(:title).value("title")
    report.page.item(:subtitle).value("subtitle")
    report.page.item(:dateFrom).value("date_from")
    report.page.item(:dateTo).value("date_to")
    report.page.item(:page).value(pages)

    datas.each do |data|
      logger.debug "data: #{data[0]}"
      report.list.add_row do |row|
        row.values col1: data[0],
                   col2: data[1],
                   col3: data[2],
                   col4: data[3],
                   col5: data[4],
                   col6: data[5]
      end
    end

    report.generate
  end

And my result report.pdf

Screen Shot 2019-05-13 at 16 22 38

I just want title and subtitle appear at the first page. Any advice is welcomed

hidakatsuya commented 5 years ago

Currently, thinreports has no feature of solving your problems. However, you may be able to solve in the following way.

First, you need to create two templates:

  1. template_a.tlf: With Header in list
  2. template_b.tlf: Without Header in list

Then, the code looks like this:

report = Thinreports::Report.new layout: `template_a.tlf`

datas.each do |data|
  report.start_new_page layout: `template_b.tlf` if report.list.overflow?

  report.list.add_row do |row|
    :
  end
end

See also https://github.com/thinreports/thinreports-generator/tree/master/test/features/list_manually

Hope this help.

TranBaVinhSon commented 5 years ago

@hidakatsuya thanks man, 👍