2012-05-09 80 views
10

我使用虾来创建包含表格格式和一些列表中的大量数据的pdf。列表的问题是,我只是使用文本作为列表,因为没有语义等同于ul> li列表,就像我在webfrointend中使用它们一样。所以名单是没有道理的。因为我不适合列表图标,所以使用多条线的列表看起来很滑稽。我怎样才能实现不喜欢废话的大虾列表?在对虾中使用列表

+0

那你试试? – tmaximini

+0

我只是尝试了补充列表作为文本,但它看起来尽可能多的一行评论crapy。 – davidb

回答

23

虾是一个很好的PDF库,但问题是它自己的视图系统。有Prawn-format,但不再维护。

我建议使用WickedPDF,它允许您在您的PDF中包含简单的ERB代码。

使用虾:另一个肮脏和丑陋解决方案是一个两列的表无边框,第一列包含列表的子弹,第二列文本:

table([ ["•", "First Element"], 
     ["•", "Second Element"], 
     ["•", "Third Element"] ]) 
+0

谢谢我认为这对我有用,... – davidb

+1

太棒了!你也可以使用'cell_style:{borders:[]}'作为一个选项来摆脱边界,使它看起来像一个实际的列表。 – Diego

9

我只是有一个类似的问题并在虾内解决了一个稍微不同的方式比使用表:

["Item 1","Item 2","Item 3"].each() do |list-item| 

    #create a bounding box for the list-item label 
    #float it so that the cursor doesn't move down 
    float do 
    bounding_box [15,cursor], :width => 10 do 
     text "•" 
    end 
    end 

    #create a bounding box for the list-item content 
    bounding_box [25,cursor], :width => 600 do 
    text list-item 
    end 

    #provide a space between list-items 
    move_down(5) 

end 

这可能显然是范nded(例如,您可以使用each_with_index()而不是每个())编号列表。它还允许在边界框中使用任意内容(这在表格中是不允许的)。

+1

当PDF文档具有多个页面时,此解决方案存在问题:当文本进入下一页时,它显示在底部。 –

0

一个复飞就是创建一个类似于crm答案的方法。不同之处在于,文本转到另一页时不会中断,而且您也可以有多个级别。

def bullet_item(level = 1, string) 
    indent (15 * level), 0 do 
     text "• " + string 
    end 
end 

直接让喜欢所以这种方法:

bullet_item(1, "Text for bullet point 1") 
bullet_item(2, "Sub point") 

随意重构。

4

要创建Adobe内置字体的项目符号,请使用\ u2022。

\u2022 This will be the first bullet item 
\u2022 blah blah blah 

虾支持具有WinAnsi的代码和这些必须被编码为UTF-8码元(又名字形)。看到这篇文章的更多细节:https://groups.google.com/forum/#!topic/prawn-ruby/axynpwaqK1g

该虾手册有一个支持的字形的完整列表。

+1

为什么这个答案没有得到更多的认可? @Powers - 我自己就用这个,它工作得很好。应该说你的语法假设用双引号封装。 – Tass

4

刚刚为客户做了这个。对于大家谁愿意来渲染包含UL/OL列出了预先格式化的html:

def render_html_text(text, pdf) 
    #render text (indented if inside ul) 
    indent = 0 #current indentation (absolute, e.g. n*indent_delta for level n) 
    indent_delta = 10 #indentation step per list level 
    states = [] #whether we have an ol or ul at level n 
    indices = [] #remembers at which index the ol list at level n, currently is 

    #while there is another list tag do 
    # => starting position of list tag is at i 
    # render everything that comes before the tag 
    # cut everything we have rendered from the whole text 
    #end 
    while (i = text.index /<\/?[ou]l>/) != nil do 
    part = text[0..i-1] 
    if indent == 0 #we're not in a list, but at the top level 
     pdf.text part, :inline_format => true 
    else 
     pdf.indent indent do 
     #render all the lis 
     part.gsub(/<\/li>/, '').split('<li>').each do |item| 
      next if item.blank? #split may return some ugly start and end blanks 

      item_text = if states.last == :ul 
         "• #{item}" 
         else # :ol 
         indices[indices.length-1] = indices.last + 1 
         "#{indices.last}. #{item}" 
         end 

      pdf.text item_text, :inline_format => true 
     end 
     end 
    end 

    is_closing = text[i+1] == '/' #closing tag? 
    if is_closing 
     indent -= indent_delta 
     i += '</ul>'.length 

     states.pop 
     indices.pop 
    else 
     pdf.move_down 10 if indent == 0 

     type_identifier = text[i+1] #<_u_l> or <_o_l> 
     states << if type_identifier == 'u' 
        :ul 
       elsif type_identifier == 'o' 
        :ol 
       else 
        raise "what means type identifier '#{type_identifier}'?" 
       end 
     indices << 0 

     indent += indent_delta 
     i += '<ul>'.length 
    end 

    text = text[i..text.length-1] #cut the text we just rendered 
    end 

    #render the last part 
    pdf.text text, :inline_format => true unless text.blank? 
end 
+1

谢谢你对我真的很有用! 可能的改进:'String#blank?'不是纯粹的Ruby。使用'.strip =='''作为快速修复。 当渲染子弹项目时使用'#{item.strip}',否则我的子弹结束后会换行(由于我的HTML标记中的换行符可能) –

3

尊重光标位置以及渲染就像一个小的几行代码的真实列表中的优秀的解决方案是:

items = ["first","second","third"] 
def bullet_list(items) 
    start_new_page if cursor < 50 
    items.each do |item| 
    text_box "•", at: [13, cursor] 
    indent(30) do 
     text item 
    end 
    end 
end 

start_new_page子句涵盖了项目符号行项目可能需要进入下一页的情景。这保持与子弹内容的子弹。

例PDF渲染截图:

Example Rendered List