Save a Prawn PDF as a Paperclip attachment?

后端 未结 5 1190
悲&欢浪女
悲&欢浪女 2021-02-04 01:24

I\'m using Prawn and Prawnto to display a PDF-based reports to the user, but in some circumstances, I\'d also like to save the PDF as an attachment to one of my models. I\'m usi

5条回答
  •  难免孤独
    2021-02-04 01:57

    This worked for me

    pdf = Prawn::Document.new(:page_size => "LETTER", :page_layout => :landscape)
    pdf.render_file File.join(Rails.root, "app/pdfs", "x.pdf")
    current_user.certificate = File.open("#{Rails.root}/app/pdfs/x.pdf")
    current_user.save!
    

    Where certificate is what my paperclip attachment is saved as in the model:

    class User < ActiveRecord::Base
      has_attached_file :certificate
    

提交回复
热议问题