Question

I am using PDFKit to render a partial view as a PDF file. However, I'm running into some issues when trying to use render_to_string (preferred method), rather than an external web request.

The pdf file when rendered using a url:

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

Proper Text

The pdf file when rendered using render_to_string:

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 (“<b>Agreement</b>”) is entered into...

Malformatted Text

What am I doing wrong?

Was it helpful?

Solution

You have curved quotes in the source partial. The console output shows this:

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

Curved quotes are UTF-8 characters, however PDFKit is parsing them as ASCII by default. See this question.


EDIT: Passing in a directly-rendered string requires the string to use UTF-8 encoding. Ruby 1.9 does this by default. In Ruby 1.8, try the following:

html = render_to_string :partial => "agreement", :encoding => "UTF-8"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top