通过它的html表示在Prawn中创建一个表格 [英] Create a table in Prawn by its html representation

查看:100
本文介绍了通过它的html表示在Prawn中创建一个表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前 Prawn gem允许通过其 html 表示形式创建表格(将html表格字符串作为输入参数,如< table class =abc> ....< / table> )。现在我没有在手册中找到这个设施。

现在有可能吗?如果不是,那么是否还有其他选择呢?

TL; DR:如果您的用例是1)和PDF数据(如在线发票等),以及2)确保两者看起来相同,那么Prawn并不是真正的最佳解决方案(这是Prawn自述文件中的相同建议)。



就你而言,你可以使用Nokogiri或 Upton 解析HTML并提取数据从HTML表格中输入,然后用它通过Prawn生成PDF表示。 HTML样式可能不会直接转换为Prawn使用的样式,因此即使有很多代码缠绕,也可能无法实现样式中的一致性 - 我假设它来自 royalGhost ,是你想要的结果。另外,如果您的HTML表格嵌套并且解析代码不适合,那么简单的Nokogiri解析解决方案将不起作用。例如,考虑这一点:

 < table> 
< tr>
< td>第一列,第一行< / td>
< td>第二列,第一行< / td>
< / tr>
< tr>
< table>
< tr>
< td>第一列,第二列< / td>
< td>第二列,第二列< / td>
< td>第三列,第二列< / td>
< / tr>
< / table>
< / tr>
< / table>

然后,在Ruby解析片段中,您应该确保内部< ; table> ...< / table> 被解析为 Prawn :: Table 对象,而不是一行 Prawn :: Table :: Cell 对象。

任何基于 wkhtmltopdf rel =nofollow> WickedPDF PDFKit 提供了更简洁的方法来实现HTML PDF转换解决方案。



您有两种选择:


  1. 比较喜欢上面的解决方案。
  2. 使用Prawn通过Nokogiri / Upton从HTML中提取数据并生成PDF,而不用担心样式与HTML表示中的样式相同。
  3. li>


Earlier Prawn gem allowed to create a table by its html representation (having an html table string as an input argument like <table class="abc"> .... </table>). Now I didn't find this facility in the manual.

So is it possible now? If not, is there any other option then?

解决方案

TL;DR: if your use-case is 1) generating both HTML and PDF data (like online invoices etc.), and 2) making sure both look the same, then Prawn is not really the best solution (which is the same suggestion in the Prawn Readme).

In your case, you could parse the HTML using Nokogiri or Upton and extract the data from the HTML table and then use it to generate the PDF representation via Prawn. The HTML styles may not directly translate into the ones used by Prawn and so, even with a lot of code-wrangling, you might not achieve the consistency in styling — which I assume, from the comments on the answer by royalGhost, is the result you want. Also, a simple Nokogiri parsing solution won't work if your HTML table is nested and the parsing code does not cater to that. For example, consider this:

<table>
  <tr>
    <td>First Column, First Row</td>
    <td>Second Column, First Row</td>
  </tr>
  <tr>
    <table>
      <tr>
        <td>First Column, Second Row</td>
        <td>Second Column, Second Row</td>
        <td>Third Column, Second Row</td>
      </tr>
    </table>
  </tr>
</table>

Then, in the Ruby parsing snippet, you should ensure that the inner <table>...</table> is parsed into a Prawn::Table object and not a row of Prawn::Table::Cell objects.

Any wkhtmltopdf based options such as WickedPDF or PDFKit offer much cleaner way of achieving the HTML to PDF conversion solution.

You have two options:

  1. Ditch Prawn entirely and prefer the solution above.
  2. Use Prawn by extracting the data from the HTML via Nokogiri/Upton and generate the PDF and not worry about styling being the same as that in the HTML representation.

这篇关于通过它的html表示在Prawn中创建一个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆