动态设置整个Prawn PDF文档的高度 [英] Set dynamically height for entire Prawn PDF Document
问题描述
在尝试使用大虾 gem生成文档时,我遇到了问题导轨
I'm stuck with a problem when trying to generate a document using Prawn gem for Rails
我想做的是为我的pdf设置一个可变的高度,因此,根据数据库中的某些查询,PDF的高度会改变.之所以这样做,是因为我需要一页PDF文档.
What I'm trying to do is to set a variable height for my pdf, so depending on some queries in the database, the PDF height will change. I'm doing this because I need a single page PDF document.
当前,我的代码如下:
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")
有人可以帮我吗?谢谢.
Can anyone help me with this? Thanks.
推荐答案
在不知道您到底要调整什么的情况下,我不得不在这里做出一些猜测.
Without knowing what exactly you're adjusting for, I'll have to makes some guesses here.
因此,我将为您返回的数据和最小文档高度确定某种行高.
So I would establish some sort of line height for your returned data and a min document height.
line_height = 14
min_height = 419.53
然后,我将运行查询并计算结果.然后,我将弄清楚可变高度是多少,并将其添加到最小高度.
Then I would run the queries and count the results. Then I would figure out what the variable height would be and add it to the min height.
variable_height = results.length * line_height
height = min_height + variable_height
最后:
pdf = Prawn::Document.new(page_size: [297.64, height], margin: 0)
类似的事情应该可以通过调整来满足您的特定需求.
Something like this should work with tweaks for your specific needs.
这篇关于动态设置整个Prawn PDF文档的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!