0

PDFKitを使用して、部分的なビューをPDFファイルとしてレンダリングしています。ただし、外部のWebリクエストではなく、render_to_string(推奨されるメソッド)を使用しようとすると、いくつかの問題が発生します。

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エンコーディングを使用する文字列が必要です。Ruby1.9はデフォルトでこれを行います。Ruby 1.8では、次のことを試してください。

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