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 using Paperclip for all of my attachments. Does anyone have any suggestions on how to do this?
我正在使用Prawn和Prawnto向用户显示基于PDF的报告,但在某些情况下,我还想将PDF保存为我的某个模型的附件。我正在使用Paperclip来处理我的所有附件。有没有人对如何做到这一点有任何建议?
Thanks!
谢谢!
5 个解决方案
#1
26
When using prawnto you will need to eval the variables in the .pdf.prawn template. Second step is to mimic a real file for paperclip.
使用prawnto时,您需要评估.pdf.prawn模板中的变量。第二步是模仿回形针的真实文件。
-
Generating the PDF:
生成PDF:
#find the prawwnto template you 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 do @report = find_report #put here, all local variables that the pdf template needs eval(template) #this evaluates the template with your variables end attachment = pdf.render
-
Save PDF with paperclip:
使用回形针保存PDF:
file = StringIO.new(attachment) #mimic a real upload file file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's that paperclip needs file.original_filename = "your_report.pdf" file.content_type = "application/pdf" #now just use the file object to save to the Paperclip association. # assuming your Paperclip association is named "pdf_report" @report_store.pdf_report = file @report_store.save!
Hope this helps.
希望这可以帮助。
#2
8
It should work if you just pass a File reference to that PDF to Paperclip.
如果您只是将对该PDF的文件引用传递给Paperclip,它应该可以工作。
require 'prawn'
pdf = Prawn::Document.new
pdf.text("Prawn Rocks")
pdf.render_file('/path/to/prawn.pdf')
pdf_file = File.open('/path/to/prawn.pdf')
# assuming your Paperclip association is named "pdf_attachment"
my_model.pdf_attachment = pdf_file
#3
2
I got it working without instance eval by turning it the other way around : generate the PDF in your model and render it in you controller
我通过相反的方式使它在没有实例eval的情况下工作:在模型中生成PDF并在控制器中呈现它
In a model :
在模型中:
def generate_pdf
Prawn::Document.new(:page_size => 'A4', :top_margin => 0, :left_margin => 0) do |pdf|
<your pdf code here>
<copy paste from your template>
end.render
end
You can then send it as a mail attachment :
然后,您可以将其作为邮件附件发送:
attachment = generate_pdf
mail = Notifier.send_pdf(attachment)
mail.deliver
Or render it in your browser windows in your controller :
或者在控制器的浏览器窗口中呈现它:
send_data your_model.generate_pdf, :type => "application/pdf", :disposition => 'inline'
#4
1
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
#5
0
@Adam Albrecht,You will be saving image as a attachment but for saving pdf as an attachment you need to add one more validation -
@Adam Albrecht,您将保存图像作为附件,但为了将pdf保存为附件,您需要再添加一个验证 -
****validates_attachment :document, content_type: { content_type: 'application/pdf' }****
**** validates_attachment:document,content_type:{content_type:'application / pdf'} ****
#1
26
When using prawnto you will need to eval the variables in the .pdf.prawn template. Second step is to mimic a real file for paperclip.
使用prawnto时,您需要评估.pdf.prawn模板中的变量。第二步是模仿回形针的真实文件。
-
Generating the PDF:
生成PDF:
#find the prawwnto template you 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 do @report = find_report #put here, all local variables that the pdf template needs eval(template) #this evaluates the template with your variables end attachment = pdf.render
-
Save PDF with paperclip:
使用回形针保存PDF:
file = StringIO.new(attachment) #mimic a real upload file file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's that paperclip needs file.original_filename = "your_report.pdf" file.content_type = "application/pdf" #now just use the file object to save to the Paperclip association. # assuming your Paperclip association is named "pdf_report" @report_store.pdf_report = file @report_store.save!
Hope this helps.
希望这可以帮助。
#2
8
It should work if you just pass a File reference to that PDF to Paperclip.
如果您只是将对该PDF的文件引用传递给Paperclip,它应该可以工作。
require 'prawn'
pdf = Prawn::Document.new
pdf.text("Prawn Rocks")
pdf.render_file('/path/to/prawn.pdf')
pdf_file = File.open('/path/to/prawn.pdf')
# assuming your Paperclip association is named "pdf_attachment"
my_model.pdf_attachment = pdf_file
#3
2
I got it working without instance eval by turning it the other way around : generate the PDF in your model and render it in you controller
我通过相反的方式使它在没有实例eval的情况下工作:在模型中生成PDF并在控制器中呈现它
In a model :
在模型中:
def generate_pdf
Prawn::Document.new(:page_size => 'A4', :top_margin => 0, :left_margin => 0) do |pdf|
<your pdf code here>
<copy paste from your template>
end.render
end
You can then send it as a mail attachment :
然后,您可以将其作为邮件附件发送:
attachment = generate_pdf
mail = Notifier.send_pdf(attachment)
mail.deliver
Or render it in your browser windows in your controller :
或者在控制器的浏览器窗口中呈现它:
send_data your_model.generate_pdf, :type => "application/pdf", :disposition => 'inline'
#4
1
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
#5
0
@Adam Albrecht,You will be saving image as a attachment but for saving pdf as an attachment you need to add one more validation -
@Adam Albrecht,您将保存图像作为附件,但为了将pdf保存为附件,您需要再添加一个验证 -
****validates_attachment :document, content_type: { content_type: 'application/pdf' }****
**** validates_attachment:document,content_type:{content_type:'application / pdf'} ****