简体   繁体   中英

Rails saving a pdf to Amazon S3

I have a Rails 3.2 app that uses gem 'wicked_pdf', and gem 'combine_pdf'. They both work and I can create PDFs which get emailed.

But, I have run into a situation where the email would be too big.

So, I'm trying to save the created pdf to Amazon S3. The app already has the gem 'aws-sdk'.

This is my code:

  def self.saveallpdf
    @costprojects = Costproject.where("client_id = 2")
    pdf = CombinePDF.new
    @costprojects.each do |costproject|
      @costproject = costproject
      controller = CostprojectsController.new
      controller.instance_variable_set(:"@costproject", @costproject)
      pdf2 = controller.render_to_string(pdf: "Captital Projects.pdf",
                                         template: "costprojects/viewproject",
                                         encoding: "UTF-8")
      pdf << CombinePDF.parse(pdf2)
    end
    @s3 = AWS::S3.new
    @bucket = @s3.buckets['ndeavor3-pdf']
    @obj = @bucket.objects['filename'].write(pdf, acl: :public_read)
  end

The error I'm getting is:

:data must be provided as a String, Pathname, File, or an object that responds to #read and #eof?
/app/vendor/bundle/ruby/1.9.1/gems/aws-sdk-1.8.3.1/lib/aws/s3/data_options.rb:125:in `validate_data!'
/app/vendor/bundle/ruby/1.9.1/gems/aws-sdk-1.8.3.1/lib/aws/s3/data_options.rb:32:in `compute_write_options'
/app/vendor/bundle/ruby/1.9.1/gems/aws-sdk-1.8.3.1/lib/aws/s3/s3_object.rb:594:in `write'
/app/app/models/costproject.rb:167:in `saveallpdf

'

I guess was-sdk doesn't like the "pdf" as the file??

PS - I can email the "pdf" - if it was smaller in size.

Thanks for your help!

At this point in time:

@obj = @bucket.objects['filename'].write(pdf, acl: :public_read)

pdf is a CombinePDF object and not a File , String , or Pathname

pdf.to_s might work, or you will have to create a new file from the CombinePDF object

File.new(CombinePDF) # pseduo code only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM