执行失败:错误:从ASCII-8BIT到UTF-8的"\ xFE"

ser*_*erg 6 encoding ruby-on-rails utf-8 wicked-pdf

Rails 4 *Mac OSX 10.8.4*


我正在使用以下Gem来生成wicked_pdf pdf:

gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
Run Code Online (Sandbox Code Playgroud)

渲染视图作为pdf工作正常,谷歌正确显示它的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
Run Code Online (Sandbox Code Playgroud)

这将在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
Run Code Online (Sandbox Code Playgroud)

但是我收到了错误:

Failed to execute:

Error: "\xFE" from ASCII-8BIT to UTF-8
Run Code Online (Sandbox Code Playgroud)

Mar*_* T. 6

试试这个:

   File.open(save_path, 'w:ASCII-8BIT') do |file|
     file << pdf
   end
Run Code Online (Sandbox Code Playgroud)

在内存中呈现为字符串的PDF似乎是ASCII格式,因此请将其保存为:)