【问题标题】:ruby prawn gem, table position红宝石对虾宝石,餐桌位置
【发布时间】:2011-11-26 13:54:26
【问题描述】:

使用 prawn gem 我在 pdf 文件中显示表格.. 现在我想设置表格的位置,请指导我如何处理..

我使用以下代码生成 pdf 报告,

pdftable = Prawn::Document.new

pdftable.table([["Name","Login","Email"]],
:column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80}, :row_colors => ["d5d5d5"])

 @users.each do|u|
 pdftable.table([["#{u.name}","#{u.login}","#{u.email}"]],
 :column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80 }, :row_colors => ["ffffff"])

谢谢

【问题讨论】:

  • 您希望它如何定位?

标签: ruby-on-rails pdf prawn


【解决方案1】:

您可以将函数 indent() 与函数 move_down 和 move_up 结合使用, 因此,例如在位置(50,20)(相对于您的光标位置)设置表格将如下所示:

move_down 20
indent(50) do #this is x coordinate
    pdftable.table([["Name","Login","Email"]],
    :column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80}, :row_colors => ["d5d5d5"])

    @users.each do|u|
    pdftable.table([["#{u.name}","#{u.login}","#{u.email}"]],
    :column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80 }, :row_colors => ["ffffff"])
end

`

【讨论】:

    【解决方案2】:

    您可能需要在桌子周围创建一个bounding_box。请参阅documentation for bounding boxes

    另外:您是否意识到您正在为标题和每个用户创建一个新表?

    【讨论】:

      【解决方案3】:

      您需要在您的Gemfile 中包含对 prawn-layout 的依赖项(除了 prawn):

      # Gemfile
      ...
      gem 'prawn'
      gem 'prawn-layout'
      ...
      

      然后运行一个

      bundle install
      

      从控制台,让 bundler 下载新的 gem。

      在此之后,除了 prawn 要求以及 prawn/layout 要求之外,您还必须包括要生成 PDF 的位置:

      # your_pdf_builder_lib.rb
      require 'prawn'
      require 'prawn/layout'
      

      做完这个,你唯一需要写的就是让你的桌子居中是这样的:

      # your_pdf_builder_lib.rb
      require 'prawn'
      require 'prawn/layout'
      ...
      def build_pdf
        p = Prawn::Document.new(:page_size => "A4")
        ...
        data = [["header 1", "header 2"], [data_col1, data_col2], ...] # your content for table
        ...
        p.table data, :position => :center # :position => :center will do the trick.
        ...
        p.render
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-09
        相关资源
        最近更新 更多