我正在使用PDFKit將部分視圖呈現爲PDF文件。但是,在嘗試使用render_to_string(首選方法)時遇到了一些問題,而不是外部Web請求。PDFKit with Rails使用render_to_string創建格式錯誤的文本
使用URL時,渲染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 (「<b>Agreement</b>」) is entered into...
html = "#{root_url}/contracts/#{@contract.id}"
pdf = PDFKit.new(html, page_size: 'Letter').to_pdf
PDF文件使用render_to_string渲染時
我在做什麼錯?
您可能試圖在具有ASCII編碼的頁面中顯示unicode符號(引號)。這個問題與'pdf'沒有任何關係,在你的html頭部添加''。 – mudasobwa 2013-02-16 09:09:35
我試過了,這似乎並沒有訣竅。在這兩種情況下,PDFKit都提供了相同的html,但只有一個具有較差的編碼。 – 2013-02-16 09:20:03