5

已解决,请参阅底部的编辑。


在我的 3.1 rails 应用程序中,我正在生成这样的 pdf:

def show
  @contributor = Contributor.find(params[:id])

   respond_to do |format|
   format.pdf { 
    html = render_to_string(:action => "show.html.erb") 
    kit = PDFKit.new(html) 
    kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
   thepdf = send_data kit.to_pdf, :filename => "blah.pdf", :type => 'application/pdf' 
redirect_to :action => save_to_s3      
}
end

然后我尝试通过使用 Paperclip 上传将生成的 PDF 存储在 S3 上:

def save_to_s3
     html = render_to_string(:action => "show.html.erb") 
      kit = PDFKit.new(html) 
      kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
      roy = Royarchive.new(:client_id => @contributor.client_id) 
      f = File.open("blah.pdf", 'w+')
      f.write kit.to_pdf
      roy.pdf = f
      roy.save!
end 

但这给了我"\x9C" from ASCII-8BIT to UTF-8

如何使用 Paperclip 将此生成的 pdf 上传到 S3?我正在使用 Heroku,所以我无法在服务器上保存临时文件然后上传它。

////解决了

哦,我的错,您可以 在会话期间将文件存储在 root

所以这有效:

   def show
  @contributors = Contributor.where(:client_id => current_user.client_id).paginate(:page => params[:page])
  @contributor = Contributor.where(:client_id => current_user.client_id).find(params[:id])


   respond_to do |format|
   format.html 
    format.pdf { 
    html = render_to_string(:action => "show.html.erb") 
    kit = PDFKit.new(html) 
    kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
    send_data kit.to_pdf, :filename => "name.pdf", :type => 'application/pdf' 
    @thepdf = kit.to_file("#{Rails.root}/tmp/name.pdf")

roy = Royarchive.new(:client_id => @contributor.client_id) 
roy.pdf = @thepdf
roy.save!     
  }   

end
end
4

1 回答 1

3

哦,我的错,您可以 在会话期间将文件存储在 root

所以这有效:

def save_to_s3
  html = render_to_string(:action => "show.html.erb") 
   kit = PDFKit.new(html) 
   kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css" 
  thepdf = kit.to_file("#{Rails.root}/tmp/name.pdf")

     roy = Royarchive.new(:client_id => @contributor.client_id) 
     roy.pdf = thepdf
     roy.save!
     redirect_to :action => index
end 
于 2011-09-01T21:29:18.483 回答