设置 Prawn 表上的单元格/列宽度

发布于 2024-09-27 23:38:29 字数 946 浏览 9 评论 0原文

我正在用 ruby​​ 制作一个小脚本,它会生成一周时间表 PDF 文件,使用 Prawn 作为 PDF 库,并且我正在努力设计表格的样式。我想为表中的所有列设置静态宽度,以便宽度不依赖于单元格的内容。

我已经阅读了 Prawn 项目网站的文档(那里有很大的改进空间),并用谷歌搜索了几个小时,但我不知道如何设置表格中的列或单元格的宽度,或者如何设置列的样式/细胞以任何方式。我确实得到了一个具有网格布局的 PDF 文件,但单元格的大小变化很大,看起来不太整齐。

这不起作用:

Prawn::Document.generate(@filename, :page_size => 'A4', :page_layout => :landscape) do
  table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee'], :column_widths => 50)
end

这是我生成 PDF 的方法的当前版本,但它也不对单元格进行样式化:

def produce_pdf
  course_matrix = DataParser.new.parse_for_pdf

  Prawn::Document.generate(@filename, :page_size => 'A4', :page_layout => :landscape) do
    table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee']) do |table|
      table.cells.style { |cell| cell.width = 50 }
    end
  end
end

I'm making a little script with ruby which produces a week schedule PDF file, using Prawn as a PDF library and I'm struggling with styling the table. I'd like to set a static width for all the columns in the table so that the widths wouldn't depend on the contents of the cells.

I've read the documentation (lot of room for improvement there) from the Prawn project site and googled for a few hours, but I'm lost at how to set width for columns or cells in a table, or how to style the columns/cells in any way whatsoever. I do get a PDF file which has a grid layout though, the cells just vary in size a lot, which doesn't look that neat.

This didn't work:

Prawn::Document.generate(@filename, :page_size => 'A4', :page_layout => :landscape) do
  table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee'], :column_widths => 50)
end

Here's the current version of my method to generate PDF, but it doesn't stylize the cells either:

def produce_pdf
  course_matrix = DataParser.new.parse_for_pdf

  Prawn::Document.generate(@filename, :page_size => 'A4', :page_layout => :landscape) do
    table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee']) do |table|
      table.cells.style { |cell| cell.width = 50 }
    end
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-10-04 23:38:29

我做了这样的事情:

pdf = Prawn::Document.new(
  :page_size => 'A4',
  :page_layout => :landscape,
  :margin => [5.mm])
  ....
  .... 
  pdf.table(tbl_data) do
    row(0).style(:background_color => 'dddddd', :size => 9, :align => :center, :font_style => :bold)
    column(0).style(:background_color => 'dddddd', :size => 9, :padding_top => 20.mm, :font_style => :bold)
    row(1).column(1..7).style(:size => 8, :padding => 3)
    cells[0,0].background_color = 'ffffff'
    row(0).height = 8.mm
    row(1..3).height = 45.mm
    column(0).width = 28.mm
    column(1..7).width = 35.mm
    row(1..3).column(6..7).borders = [:left, :right]
    row(3).column(6..7).borders = [:left, :right, :bottom]
  ....
 pdf.render()

更多信息此处

I do something like this:

pdf = Prawn::Document.new(
  :page_size => 'A4',
  :page_layout => :landscape,
  :margin => [5.mm])
  ....
  .... 
  pdf.table(tbl_data) do
    row(0).style(:background_color => 'dddddd', :size => 9, :align => :center, :font_style => :bold)
    column(0).style(:background_color => 'dddddd', :size => 9, :padding_top => 20.mm, :font_style => :bold)
    row(1).column(1..7).style(:size => 8, :padding => 3)
    cells[0,0].background_color = 'ffffff'
    row(0).height = 8.mm
    row(1..3).height = 45.mm
    column(0).width = 28.mm
    column(1..7).width = 35.mm
    row(1..3).column(6..7).borders = [:left, :right]
    row(3).column(6..7).borders = [:left, :right, :bottom]
  ....
 pdf.render()

More info here.

梦毁影碎の 2024-10-04 23:38:29

要为所有列设置静态宽度,我会执行以下操作:

REPORT_FIELDS = %w[DESCRIPTION PRICE DATE NOTE].freeze
A4_SIZE = 200.freeze

data = []
data << REPORT_FIELDS
... things happen ...
table(data, column_widths: (A4_SIZE/REPORT_FIELDS.size).mm))

在本例中,我想将表格设置为适合整个页面,并且单元格具有相同的宽度。

To set a static width for all the columns I do something like this:

REPORT_FIELDS = %w[DESCRIPTION PRICE DATE NOTE].freeze
A4_SIZE = 200.freeze

data = []
data << REPORT_FIELDS
... things happen ...
table(data, column_widths: (A4_SIZE/REPORT_FIELDS.size).mm))

In this case I wanted to set the table to fit the full page and with the cells with the same width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文