在 S3 上存储系统生成的 PDF

发布于 2024-12-02 11:17:39 字数 2008 浏览 1 评论 0原文

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


在我的 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

然后我尝试通过使用回形针上传来将生成的 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" 从 ASCII-8BIT 到 UTF-8

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

////已解决

哦,我的错,你可以在会话期间将文件存储在根目录

所以这有效:

   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

Solved, see edit at bottom.


In my 3.1 rails app I'm generating a pdf like this:

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

Then I'm trying to store that generated PDF on S3 by uploading with Paperclip:

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 

But that's giving me "\x9C" from ASCII-8BIT to UTF-8

How can I use Paperclip to upload this generated pdf to S3? I'm using Heroku so I can't save a temp file on the server then upload it.

////solved

Oh, my bad, you can store files for the duration of the session at root.

So this works:

   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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2024-12-09 11:17:39

哦,我的错,你可以在会话期间将文件存储在根目录

所以这有效:

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 

Oh, my bad, you can store files for the duration of the session at root.

So this works:

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文