大虾PDF和空数据库单元格?

发布于 2024-09-12 19:08:29 字数 1282 浏览 3 评论 0原文

我正在使用 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 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    九歌凝 2024-09-19 19:08:29

    pdf边界框(无耻地从大虾文档中拉出来)。

       pdf.bounding_box([100,400], :width => 400) do
         pdf.text("The height of this box is #{pdf.bounds.height}")
         pdf.text('this is some text')
         pdf.text('this is some more text')
         pdf.text('and finally a bit more')
         pdf.text("Now the height of this box is #{pdf.bounds.height}")
       end
    

    如果属性可用,可以轻松修改为打印。

    pdf bounding box (shamelessly yanked from the prawn documentation).

       pdf.bounding_box([100,400], :width => 400) do
         pdf.text("The height of this box is #{pdf.bounds.height}")
         pdf.text('this is some text')
         pdf.text('this is some more text')
         pdf.text('and finally a bit more')
         pdf.text("Now the height of this box is #{pdf.bounds.height}")
       end
    

    can be easily modified to print if the attribute is available.

    护你周全 2024-09-19 19:08:29

    使用当前地址行的 y 坐标计数器。如果添加一行,请将其减少 10。如果您的行为空,则不要添加它,也不要增加 y 坐标。

    y = 673
    if @line1 then 
      pdf.text_box "#{@line1}", :size => 9, :at => [100,y]
      y+= 10
    end 
    if @line2 then 
      pdf.text_box "#{@line2}", :size => 9, :at => [100,y]
      y+= 10
    end 
    

    提示:如果它按您想要的方式工作 - 将其重构为一个添加行并增加计数器的函数)

    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.

    y = 673
    if @line1 then 
      pdf.text_box "#{@line1}", :size => 9, :at => [100,y]
      y+= 10
    end 
    if @line2 then 
      pdf.text_box "#{@line2}", :size => 9, :at => [100,y]
      y+= 10
    end 
    

    (hint: if it works the way you want - refactor it into a function that adds the line and increases your counter)

    萌逼全场 2024-09-19 19:08:29

    您应该能够使用 Ruby 的“空?”方法,该方法可用于大多数数据结构和字符串,以确定您的字符串或数据结构是否有任何内容。类似的东西(我不太了解虾,但是这个公司名称看起来像是一个字符串,所以我认为你不需要转换它):

    pdf.text_box @kase.company.companyname, :size => 9, :at => [100,673] unless @kase.company.companyname.empty?
    

    当然,你可以将其变成一个辅助方法

    def my_text_box (var, x, y)
      pdf.text_box var, :size => 9, :at => [x,y] if unless var.empty?
    end
    

    并调用你的大虾文档

    my_text_box @kase.company.companyname, 100, 673
    

    哎呀,忘记添加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):

    pdf.text_box @kase.company.companyname, :size => 9, :at => [100,673] unless @kase.company.companyname.empty?
    

    of course, you could make that into a helper method

    def my_text_box (var, x, y)
      pdf.text_box var, :size => 9, :at => [x,y] if unless var.empty?
    end
    

    and call in your prawn document

    my_text_box @kase.company.companyname, 100, 673
    

    oops, forgot to add the ref to empty?
    http://apidock.com/ruby/String/empty%3F

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文