Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

column widths not working #151

Open
raquelhortab opened this issue Apr 18, 2023 · 1 comment
Open

column widths not working #151

raquelhortab opened this issue Apr 18, 2023 · 1 comment

Comments

@raquelhortab
Copy link

Hi! I got a table with subtables inside. I can successfully control the subtables' column widths but not the main table ones.
Here's the code and below the result. If TLDR, important code is towards the end.

tmp_dir = Rails.root.join("tmp")
path = tmp_dir + "work_hours_test.pdf"

pdf = Prawn::Document.new(:page_size => PDF::Core::PageGeometry::SIZES["A4"])

title = [[{content: I18n.t('ubiquo.work_hours_month_validation.pdf.title').upcase, colspan: 3}]]

signature_table = pdf.make_table(
  [
    ["#{I18n.t('ubiquo.work_hours_month_validation.pdf.signature')}:", {image: '/home/raquel/Documents/signature_example.png', :image_height => 70}],
    [{content: "#{I18n.t('ubiquo.work_hours_month_validation.pdf.date')}: #{"???data firma"}", colspan:2}]
  ]
) do |table|
  table.cell_style = {border_width: 0}
end

headers = [
  [{content: pdf.make_table([
                              [
                                pdf.make_table([ [{content: I18n.t('ubiquo.work_hours_month_validation.pdf.company'), colspan: 2}],
                                                 [I18n.t('ubiquo.work_hours_month_validation.pdf.name_or_company_name'), "??"],
                                                 [I18n.t('ubiquo.work_hours_month_validation.pdf.nif_cif'), "??"],
                                                 [I18n.t('ubiquo.work_hours_month_validation.pdf.ccc'), "??"],
                                               ], width: pdf.bounds.width/2){|table|
                                  table.row(0).font_style = :bold
                                },
                                pdf.make_table([ [{content: I18n.t('ubiquo.work_hours_month_validation.pdf.worker'), colspan: 2}],
                                                 [I18n.t('ubiquo.work_hours_month_validation.pdf.name'), ubiquo_user.full_name],
                                                 [I18n.t('ubiquo.work_hours_month_validation.pdf.nif'), "??"],
                                                 [I18n.t('ubiquo.work_hours_month_validation.pdf.social_security_number'), "??"],
                                               ], width: pdf.bounds.width/2){|table|
                                  table.row(0).font_style = :bold
                                },
                              ],
                              [{content: "#{I18n.t('ubiquo.work_hours_month_validation.pdf.month')}: #{"??"}", colspan: 2}],
                              [{content: signature_table, colspan: 2}],
                            ], width: pdf.bounds.width, cell_style: {:background_color => "F1F1F1"}), colspan: 3}],
  [{content: "", colspan: 3}],
  [I18n.t('ubiquo.work_hours_month_validation.pdf.date'), I18n.t('ubiquo.work_hours_month_validation.pdf.schedule'), I18n.t('ubiquo.work_hours_month_validation.pdf.total_day_hours')]
]


month_hours = 0
table_data = work_hours_ranges.group_by(&:date).map do |date, ranges|
  # (some unimportant code ...)
  [date, hour_ranges, total_hours]
end

footer_data = [[{content: "", colspan: 3}],
               [{content: pdf.make_table([
                [I18n.t('ubiquo.work_hours_month_validation.pdf.total_hours'), month_hours],
                [I18n.t('ubiquo.work_hours_month_validation.pdf.expected_hours'), "??"],
                [I18n.t('ubiquo.work_hours_month_validation.pdf.hour_bag'), "??"],
                [I18n.t('ubiquo.work_hours_month_validation.pdf.hour_bag_spent'), "??"],
                [I18n.t('ubiquo.work_hours_month_validation.pdf.total_balance'), "??"],
              ]){|table|
                 table.width = pdf.bounds.width
                 table.column_widths = {1 => 50}
                 table.cell_style = {:background_color => "F1F1F1"}
                 table.row(-1).font_style = :bold
               }, colspan: 3}]]

pdf.table(title + headers + table_data + footer_data) do |table|

  table.header = 4

  # blank separators:
  table.row(2).border_left_width = 0
  table.row(2).border_right_width = 0
  table.row(-2).border_left_width = 0
  table.row(-2).border_right_width = 0

  # title style
  table.row(0).font_style = :bold
  table.row(0).align = :center

  # column headers style:
  table.row(3).font_style = :bold
  table.row(3).font_style = :bold

  # ---> ---> IMPORTANT CODE HERE <--- < ---
  # table.column_widths = {0 => 50, 2 => 50} # I have also tried this way
  table.column(0).width = 50
  table.column(2).width = 50

end

page_numbering_options = {
  at: [pdf.bounds.right - 150, 0],
  width: 150,
  align: :right,
  start_count_at: 1,
}
pdf.number_pages I18n.t('ubiquo.work_hours_month_validation.pdf.page'), page_numbering_options

pdf.render_file path

You can see the footer table respects the widths I've set, but the main content's columns have equal widths.

image

Thanks in advance!

@dacook
Copy link

dacook commented Jun 2, 2023

This is a duplicate of #152 (which has more comment history), so this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants