Generate a PDF of a view in delayed job

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.

Use flying saucer with itext - that, and a html view does the trick for me :slight_smile:

Med venlig hilsen Walther

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?

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 :wink:

Cheers Walther

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.

Cool!

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

Cheers Walther

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

class SavePdfJob < Struct.new

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!