简体   繁体   English

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

[英]Failed to execute: Error: “\xFE” from ASCII-8BIT to UTF-8

Rails 4 * Mac OSX 10.8.4 * Rails 4 * Mac OSX 10.8.4 *


I'm using the following Gem for wicked_pdf pdf generation: 我正在使用以下Gem来生成wicked_pdf pdf:

gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'

Rendering Views as pdfs works fine and Google displays it's PDF viewer correctly. 渲染视图作为pdf工作正常,谷歌正确显示它的PDF查看器。 My PDFs look exactly how I want them to. 我的PDF看起来就像我想要的那样。

The problem arises when I try to save a pdf to disc, for the purpose of emailing them to a user. 当我尝试将pdf保存到光盘时,问题就出现了,目的是通过电子邮件将它们发送给用户。

For example, this works fine: 例如,这工作正常:

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

That will render the pdf in the Google Chrome PDF viewer. 这将在Google Chrome PDF查看器中呈现pdf。

But here, is where I want to generate a PDF and save to file. 但在这里,我想生成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

But I'm getting the error: 但是我收到了错误:

Failed to execute:

Error: "\xFE" from ASCII-8BIT to UTF-8

Try this: 试试这个:

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

The PDF rendered as a string in memory seems to be in ASCII, so save it as such :) 在内存中呈现为字符串的PDF似乎是ASCII格式,因此请将其保存为:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM