gpt4 book ai didi

ruby-on-rails - 为整个 Prawn PDF 文档动态设置高度

转载 作者:行者123 更新时间:2023-12-04 06:13:10 24 4
gpt4 key购买 nike

尝试使用 Prawn 生成文档时遇到问题Rails 的 gem

我想要做的是为我的 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")

谁能帮我这个?谢谢。

最佳答案

不知道你到底在调整什么,我必须在这里做一些猜测。

所以我会为您返回的数据和最小文档高度建立某种行高。

line_height = 14
min_height = 419.53

然后我会运行查询并计算结果。然后我会弄清楚可变高度是多少并将其添加到最小高度。
variable_height = results.length * line_height
height = min_height + variable_height

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

像这样的事情应该可以根据您的特定需求进行调整。

关于ruby-on-rails - 为整个 Prawn PDF 文档动态设置高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31856860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com