为整个Prawn PDF文档设置动态高度

thi*_*non 4 pdf ruby-on-rails prawn

当我尝试使用Prawn gem for Rails 生成文档时,我遇到了问题

我要做的是为我的pdf设置一个可变高度,因此根据数据库中的一些查询,PDF高度将会改变.我这样做是因为我需要单页PDF文档.

目前,我的代码如下所示:

pdf = Prawn::Document.new(page_size: [297.64, 419.53], margin: 0)

....

data = [ ["Header1", "Header2", "Header3", "Header4", "Header5", "Header6"] ]

// here is the variable data
cart.cart_products.each do |cp|
  arr = [
    cp.product_code,
    cp.product_description,
    cp.amount,
    cp.product_metric,
    cp.product_unit_value,
    cp.total_value
  ]

  data.push(arr)
end

// populating the table with data
pdf.table(data, :cell_style => {:border_width => 0}, :column_widths => [45, 80, 30, 42.36, 50, 50]) do |table|
  table.row(0).border_width = 0.1.mm
  table.row(0).font_style = :bold
  table.row(0).borders = [:bottom]
end

....

pdf.render_file("path/to/dir/document.pdf")
Run Code Online (Sandbox Code Playgroud)

谁能帮我这个?谢谢.

Cha*_*iam 5

如果不知道你究竟在调整什么,我将不得不在这里做出一些猜测.

因此,我会为返回的数据和最小文档高度建立某种行高.

line_height = 14
min_height = 419.53
Run Code Online (Sandbox Code Playgroud)

然后我会运行查询并计算结果.然后我会弄清楚变量高度是多少并将其添加到最小高度.

variable_height = results.length * line_height
height = min_height + variable_height
Run Code Online (Sandbox Code Playgroud)

最后:

pdf = Prawn::Document.new(page_size: [297.64, height], margin: 0)
Run Code Online (Sandbox Code Playgroud)

这样的事情可以根据您的特定需求进行调整.