7

导轨 4 * Mac OSX 10.8.4 *


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

gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'

将视图呈现为 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
4

1 回答 1

7

尝试这个:

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

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

于 2015-03-13T14:28:47.883 回答