大虾PDF和空数据库单元格?
我正在使用 Prawn PDF 在 Rails 应用程序中动态创建 PDF 文件。
我最近遇到了 Rails cell.blank?
函数,事实证明它非常方便,如果没有什么可隐藏的,我可以隐藏任何我想要的
行显示 - 我一直想知道的事情!是否可以在 Prawn 中做同样的事情?如果空行隐藏一行?
目前,如果我有以下情况:
Contact Name
Address Line 1
Address Line 2
Address Line 3
Postcode
Phone Number
Fax Number
例如,如果用户仅输入联系人姓名和邮政编码,则地址行 1、2 和 3 应该存在的位置将会有很大的差距。
如果我可以忽略这些行,因为它们是空的,并将其显示为:
Contact Name
Postcode
它将整理我的 PDF,并让用户不必使用不正确的字段来确保地址在 PDF 中看起来正确。
这就是我的 Prawn 代码的样子:
pdf.text_box "Client", :size => 9, :at => [50,673]
pdf.text_box "#{@kase.company.companyname}", :size => 9, :at => [100,673]
pdf.text_box "#{@kase.company.companyaddressline1}", :size => 9, :at => [100,665]
pdf.text_box "#{@kase.company.companyaddressline2}", :size => 9, :at => [100,655]
pdf.text_box "#{@kase.company.companyaddressline3}", :size => 9, :at => [100,645]
pdf.text_box "#{@kase.company.companyaddresscity}", :size => 9, :at => [100,635]
pdf.text_box "#{@kase.company.companyaddresspostcode}", :size => 9, :at => [100,625]
我已经搜索了 Prawn google 用户组网站,但似乎找不到我需要的东西。
提前致谢!
丹尼
I am using Prawn PDF to create PDF files on the fly in my Rails application.
I recently came across the rails cell.blank?
function, which has proved to be really handy, I can hide any <li>
rows I want if there is nothing to display - something I have been wondering about for a while!
Is it possible to do the same in Prawn? Hide a line if it's empty?
At the moment if I have the following:
Contact Name
Address Line 1
Address Line 2
Address Line 3
Postcode
Phone Number
Fax Number
If, for example, the user only entered the contact name and postcode there would be a significant gap where the Address Line's 1, 2 and 3 should be.
If I could ignore those rows because they are empty and display it as:
Contact Name
Postcode
it would tidy up my PDF and save the user having to use the incorrect field to ensure the address looks right in the PDF.
This is what my Prawn code looks like:
pdf.text_box "Client", :size => 9, :at => [50,673]
pdf.text_box "#{@kase.company.companyname}", :size => 9, :at => [100,673]
pdf.text_box "#{@kase.company.companyaddressline1}", :size => 9, :at => [100,665]
pdf.text_box "#{@kase.company.companyaddressline2}", :size => 9, :at => [100,655]
pdf.text_box "#{@kase.company.companyaddressline3}", :size => 9, :at => [100,645]
pdf.text_box "#{@kase.company.companyaddresscity}", :size => 9, :at => [100,635]
pdf.text_box "#{@kase.company.companyaddresspostcode}", :size => 9, :at => [100,625]
I have searched the Prawn google user group site but can't seem to find what I need.
Thanks in advance!
Danny
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pdf边界框(无耻地从大虾文档中拉出来)。
如果属性可用,可以轻松修改为打印。
pdf bounding box (shamelessly yanked from the prawn documentation).
can be easily modified to print if the attribute is available.
使用当前地址行的 y 坐标计数器。如果添加一行,请将其减少 10。如果您的行为空,则不要添加它,也不要增加 y 坐标。
(提示:如果它按您想要的方式工作 - 将其重构为一个添加行并增加计数器的函数)
use a counter for the y coordinate of the current address line. If you add a line decrease it by 10. if your line is empty don't add it and don't increase the y coordinate.
(hint: if it works the way you want - refactor it into a function that adds the line and increases your counter)
您应该能够使用 Ruby 的“空?”方法,该方法可用于大多数数据结构和字符串,以确定您的字符串或数据结构是否有任何内容。类似的东西(我不太了解虾,但是这个公司名称看起来像是一个字符串,所以我认为你不需要转换它):
当然,你可以将其变成一个辅助方法
并调用你的大虾文档
哎呀,忘记添加ref为空了?
http://apidock.com/ruby/String/empty%3F
You should be able to use Ruby's 'empty?' method, which is available for most data structures and strings to determine if your string or data structure has any content. Something like (I don't know prawn that well, but this company name looks like it would be a string so I dont think you'd need to cast it):
of course, you could make that into a helper method
and call in your prawn document
oops, forgot to add the ref to empty?
http://apidock.com/ruby/String/empty%3F