在自定义DelayedJob中创建一个(Prawn)PDF并将其上传到S3?

时间:2015-04-13 01:44:19

标签: amazon-s3 paperclip delayed-job prawn rails-activejob

使用:Rails 4.2,Prawn,Paperclip,DelayedJobs通过ActiveJobs,Heroku。

我的PDF非常大,需要在后台处理。在自定义作业中,我想创建它,将其上传到S3,然后在准备好时用网址向用户发送电子邮件。我通过PdfUpload模型来促进这一点。

我的方法/代码有什么问题吗?我正在使用我发现的示例中概述的File.open(),但这似乎是我的错误的根源(TypeError:没有将FlightsWithGradesReport隐式转换为String)。

  class PdfUpload < ActiveRecord::Base
    has_attached_file :report,
      path: "schools/:school/pdf_reports/:id_:style.:extension"
  end

/pages_controller.rb

  def flights_with_grades_report
    flash[:success] = "The report you requested is being generated.  An email will be sent to '#{ current_user.email }' when it is ready."
    GenerateFlightsWithGradesReportJob.perform_later(current_user.id, @rating.id)
    redirect_to :back
    authorize @rating, :reports?
  end

/工作

class GenerateFlightsWithGradesReportJob < ActiveJob::Base
  queue_as :generate_pdf

  def perform(recipient_user_id, rating_id)
    rating = Rating.find(rating_id)
    pdf = FlightsWithGradesReport.new( rating.id )
    pdf_upload = PdfUpload.new
    pdf_upload.report = File.open( pdf )
    pdf_upload.report_processing = true
    pdf_upload.report_file_name = "report.pdf"
    pdf_upload.report_content_type = "application/pdf"
    pdf_upload.save!
    PdfMailer.pdf_ready(recipient_user_id, pdf_upload.id)
  end
end

这会导致错误:

 TypeError: no implicit conversion of FlightsWithGradesReport into String

1 个答案:

答案 0 :(得分:1)

更改此内容:

pdf_upload.report = File.open( pdf )

到此:

pdf_upload.report = StringIO.new(pdf.render)

解决了我的问题。