我使用虾来创建包含表格格式和一些列表中的大量数据的pdf。列表的问题是,我只是使用文本作为列表,因为没有语义等同于ul> li列表,就像我在webfrointend中使用它们一样。所以名单是没有道理的。因为我不适合列表图标,所以使用多条线的列表看起来很滑稽。我怎样才能实现不喜欢废话的大虾列表?在对虾中使用列表
在对虾中使用列表
回答
虾是一个很好的PDF库,但问题是它自己的视图系统。有Prawn-format,但不再维护。
我建议使用WickedPDF,它允许您在您的PDF中包含简单的ERB代码。
使用虾:另一个肮脏和丑陋解决方案是一个两列的表无边框,第一列包含列表的子弹,第二列文本:
table([ ["•", "First Element"],
["•", "Second Element"],
["•", "Third Element"] ])
我只是有一个类似的问题并在虾内解决了一个稍微不同的方式比使用表:
["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()而不是每个())编号列表。它还允许在边界框中使用任意内容(这在表格中是不允许的)。
当PDF文档具有多个页面时,此解决方案存在问题:当文本进入下一页时,它显示在底部。 –
一个复飞就是创建一个类似于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")
随意重构。
要创建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
该虾手册有一个支持的字形的完整列表。
为什么这个答案没有得到更多的认可? @Powers - 我自己就用这个,它工作得很好。应该说你的语法假设用双引号封装。 – Tass
刚刚为客户做了这个。对于大家谁愿意来渲染包含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
谢谢你对我真的很有用! 可能的改进:'String#blank?'不是纯粹的Ruby。使用'.strip =='''作为快速修复。 当渲染子弹项目时使用'#{item.strip}',否则我的子弹结束后会换行(由于我的HTML标记中的换行符可能) –
尊重光标位置以及渲染就像一个小的几行代码的真实列表中的优秀的解决方案是:
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渲染截图:
- 1. 如何在虾中心对齐表?
- 2. 对虾+ WickedPDF:在wkhtmltopdf/WickedPDF上使用虾生成PDF的
- 3. 从列表中的顶部文本对虾表
- 4. 虾表
- 5. 大虾视图中的无序列表
- 6. 虾我如何填写表单字段使用虾?
- 7. 在Heroku上使用虾
- 8. 用RMagick对虾gmagick conflit
- 9. 使用Nori和虾
- 10. 试图使用大虾定位表
- 11. 在对虾中设置字体颜色
- 12. 图形与对虾
- 13. 对虾PDF表格混合格式
- 14. 虾,虾和模板
- 15. 如何在使用表单提交时下载对虾pdf文件?
- 16. 使用列表理解删除对称对(在列表理解中使用notin)
- 17. 如何在磁悬浮上使用虾
- 18. 在对象列表中使用IN
- 19. 对虾pdf和分页符
- 20. Ruby on Rails&对虾PDF
- 21. 使用外部TTF字体使用虾
- 22. 保存对虾PDF Rails应用程序
- 23. 设置对虾,在heroku上的prawnto
- 24. 对虾不会粘在边缘
- 25. 列表使用:对::
- 26. 在一个页面上与对虾的多个表
- 27. 铁路,虾和动态表
- 28. 虾 - 表 - ascii码 - pdf一代
- 29. 在Rails 3.2中使用pdf = pdfname.new时对虾PDF格式不正确
- 30. 使用LINQ对象列表中的对象反向列表
那你试试? – tmaximini
我只是尝试了补充列表作为文本,但它看起来尽可能多的一行评论crapy。 – davidb