Rails 4 *Mac OSX 10.8.4*
<小时/>我使用以下 Gem 来生成 wicked_pdf pdf:
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
将 View 渲染为 pdf 效果很好,并且 Google 可以正确显示其 PDF 查看器。我的 PDF 看起来正是我想要的样子。
当我尝试将 pdf 保存到光盘上,以便通过电子邮件发送给用户时,问题就出现了。
例如,这工作正常:
def command
@event = Event.find(params[:id])
@client = Contact.find(@event.client_id)
@organizer = Contact.find(@event.organizer_id)
render layout: 'command',
pdf: 'Event Command',
show_as_html: params[:debug].present?,
dpi: 300,
print_media_type: true,
margin: {
top: 0,
bottom: 0,
left: 0,
right: 0
}
end
这将在 Google Chrome PDF 查看器中呈现 pdf。
但这里是我想要生成 PDF 并保存到文件的地方。
def send_email
@event = Event.find(params[:id])
@client = Contact.find(@event.client_id)
@organizer = Contact.find(@event.organizer_id)
proforma = render_to_string(
pdf: 'proforma.pdf',
template: 'events/proforma',
layout: 'proforma'
)
pdf = WickedPdf.new.pdf_from_string(
proforma
)
save_path = Rails.root.join('public','proforma.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
end
但我收到错误:
Failed to execute:
Error: "\xFE" from ASCII-8BIT to UTF-8
最佳答案
试试这个:
File.open(save_path, 'w:ASCII-8BIT') do |file|
file << pdf
end
在内存中呈现为字符串的 PDF 似乎是 ASCII 格式,因此将其保存为这样:)
关于ruby-on-rails - 执行失败: Error: "\xFE" from ASCII-8BIT to UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18566713/