0

我正在使用 PDFKit 将部分视图呈现为 PDF 文件。但是,我在尝试使用 render_to_string (首选方法)而不是外部 Web 请求时遇到了一些问题。

使用 url 呈现时的 pdf 文件:

html = "#{root_url}/contracts/#{@contract.id}"
pdf = PDFKit.new(html, page_size: 'Letter').to_pdf

正确的文本

使用 render_to_string 渲染时的 pdf 文件:

html = render_to_string :partial => "agreement"
pdf = PDFKit.new(html , page_size: 'Letter').to_pdf
*from the console*
html => "\n\n<style>\n  #contract h2{text-align: center; margin-bottom: 10px;}\n  #contract em{font-weight: bold; text-decoration: underline; font-style: normal;}\n  #contract p.tab{margin-left: 25px;}\n  #contract ol{list-style:lower-alpha;}\n  #contract ol li{margin-left: 25px; font-size: 1em; line-height: 1.615em; color: #555; margin-bottom: 5px;}\n  #contract b{font-weight: bold;}\n  #contract p p{margin-left: 10px;}\n</style>\n<div id=\"contract\">\n <p>This agreement (“&lt;b>Agreement</b>”) is entered into...

格式错误的文本

我究竟做错了什么?

4

1 回答 1

3

您在源部分中有弯曲的引号。控制台输出显示:

...<p>This agreement (“&lt;b>Agreement</b>”)...

弯引号是 UTF-8 字符,但 PDFKit 默认将它们解析为 ASCII。看到这个问题


编辑:传入直接呈现的字符串需要字符串使用 UTF-8 编码。Ruby 1.9 默认执行此操作。在 Ruby 1.8 中,尝试以下操作:

html = render_to_string :partial => "agreement", :encoding => "UTF-8"
于 2013-02-16T09:20:51.877 回答