Re: [Rails] Re: Generate a PDF of a view in delayed job


I’m not that familiar with S3 services but I’d give this code a go:

class SavePdfJob < Struct.new
  
  def perform
    [list_of_reports].each do |report|
      file = save_pdf report 
      save_file_to_s3 file, 'reports'
    end
  end
  
  def save_file_to_s3 file, bucket
    obj = s3.buckets[bucket].objects['key']

    # files (by path)
    obj.write(file.path)
  
    # throw the file away
    file.delete
  end
  
  def save_pdf report
    kit = PDFKit.new(html, :page_size => report.page_size)
    kit.stylesheets << (report.style_sheet || '/path/to/default/css/file')

    # Get an inline PDF
    pdf = kit.to_pdf

    # Save the PDF to a file
    file = kit.to_file('/path/to/save/#{report.name}.pdf')
  end

end

Delayed::Job.enqueue SavePdfJob.new

You’d probably use something like https://github.com/ssoroka/scheduler_daemon to do the scheduling

cheers
Walther

Den 02/07/2014 kl. 18.57 skrev Walther <Walther@diechmann.net>:

Cool!

I'll ponder over this 'till the morning (it's 7pm in Denmark and I'm late for my 30Rock) ;)

Cheers
Walther

Den 02/07/2014 kl. 18.36 skrev John Baycroft <lists@ruby-forum.com>:

Gotcha.

Currently I am just using the PDFKit gem which, using middleware,
generates pdfs on the fly for all of my reports. However, I need to make
a background task(delayed job) in ruby that can go retrieve all of the
pdfs and save them to Amazon s3.

Trying to find the most straight forward solution.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4e5da14b44e72b0eaf5a92e0fb86595a%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3B3F9DF8-E608-420F-9DCA-1C1D2AC52F6D%40diechmann.net.
For more options, visit https://groups.google.com/d/optout.