ruby on rails - Save a Prawn PDF as a Paperclip attachment? -


i'm using prawn , prawnto display pdf-based reports user, in circumstances, i'd save pdf attachment 1 of models. i'm using paperclip of attachments. have suggestions on how this?

thanks!

when using prawnto need eval variables in .pdf.prawn template. second step mimic real file paperclip.

  1. generating pdf:

    #find prawwnto template want template = file.read("#{rails_root}/app/views/reports/your_report.pdf.prawn")  pdf = prawn::document.new(:page_size => 'a4', :your_options => :etc)  pdf.instance_eval   @report = find_report #put here, local variables pdf template needs   eval(template) #this evaluates template variables end  attachment = pdf.render 
  2. save pdf paperclip:

    file = stringio.new(attachment) #mimic real upload file file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's paperclip needs file.original_filename = "your_report.pdf" file.content_type = "application/pdf"   #now use file object save paperclip association.   # assuming paperclip association named "pdf_report" @report_store.pdf_report = file @report_store.save! 

hope helps.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -