Generate a PDF of a view in delayed job

54 views
Skip to first unread message

John Baycroft

unread,
Jul 1, 2014, 8:27:13 PM7/1/14
to rubyonra...@googlegroups.com
My goal is the create a model method that will generate a pdf of a
html.erb form as a delayed job task.

What I have tried:
I have tried using PDFKit with the middleware to render a PDF from the
url of the view. AKA

pdf = PDFKit.new("www.mysite.com/models/1/othermodels/1.pdf")

However, my site rests behind authentication. So in this case the pdf
that is generated is just my authentication challenge.

I attempted to use WickedPDF, PDFKit, and Prawn to generate the pdf in
the background using templates (require 'erb') etc. However, this
results in issues with converting each do blocks into strings.

ie.

content = File.read("#{Rails.root}/path/to/file.html.erb")
template = ERB.new(content)
kit = PDFKit.new(template.result(binding))

which results in error

..(( @model.othermodelss.each do |om| ).to_s); _erbout.concat
"\n\t\...
... ^
(erb):311: syntax error, unexpected end-of-input, expecting ')'
; _erbout.force_encoding(__ENCODING__)


I am just trying to get on the right track. Any pointers or advice would
be greatly appreciated.

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

Walther

unread,
Jul 2, 2014, 6:53:49 AM7/2/14
to rubyonra...@googlegroups.com
Use flying saucer with itext - that, and a html view does the trick for me :)

Med venlig hilsen
Walther
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d9eabd612bbf9ba529c4130ad1155c78%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

John Baycroft

unread,
Jul 2, 2014, 7:53:51 AM7/2/14
to rubyonra...@googlegroups.com
Walther, Thanks for your response. So just so I am clear, I'd use
"acts_like_flying_saucer" gem and itext?

The one catch to this is I am using Heroku for my production environment
so I need to have the files saves to Amazon S3. Is this possible with
your proposed solutIon?

Walther

unread,
Jul 2, 2014, 12:30:33 PM7/2/14
to rubyonra...@googlegroups.com
Oh - didn't know about that 'acts_like...'

I built it myself (6-7 years ago on Rails 2.0.2 and Ruby 1.87-ish) and I don't store the pdf's - I build them on-the-zipper...ehh...fly ;)

Cheers
Walther
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/82ccd426e6971037884f55b89a44f1c9%40ruby-forum.com.

John Baycroft

unread,
Jul 2, 2014, 12:37:38 PM7/2/14
to rubyonra...@googlegroups.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.

Walther

unread,
Jul 2, 2014, 12:58:08 PM7/2/14
to rubyonra...@googlegroups.com
Cool!

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

Cheers
Walther
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4e5da14b44e72b0eaf5a92e0fb86595a%40ruby-forum.com.

Walther Diechmann

unread,
Jul 4, 2014, 12:53:12 PM7/4/14
to rubyonra...@googlegroups.com

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

John Baycroft

unread,
Jul 4, 2014, 3:34:54 PM7/4/14
to rubyonra...@googlegroups.com
This is certainly a new approach. I really appreciate the advice. I will
give this a go first thing monday morning and see where I end up.

I'll let you know how things turn out.

Thanks again!
Reply all
Reply to author
Forward
0 new messages