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?

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.

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

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

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
GitHub - ssoroka/scheduler_daemon: a Rails 2.3, Rails 3, and Ruby compatible scheduler daemon. Replaces cron/rake pattern of periodically running rake tasks to perform maintenance tasks, only loading the environment ONCE! to do the scheduling

cheers
Walther

Den 02/07/2014 kl. 18.57 skrev Walther [email protected]:

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!