In a controller I have a respond_to
block which creates a PDF with wicked_pdf
like this:
respond_to do |format|
format.html
format.pdf do
render pdf: "pdf",
template: 'users/show.pdf.haml',
print_media_type: true,
orientation: 'Portrait',
page_size: 'A4',
disposition: 'attachment'
end
end
This works fine, but I want to use delayed_job
to move this to a background job. I have setup delayed_job
and it works fine. For instance, delivering a notification email is in the background now by adding delay
:
Notifier.new(@user).delay.send_email
How can I do this for the PDF? I tried to move the render block to a separate method and call delay
on it, but then it is not executed as a delayed job:
respond_to do |format|
format.html
format.pdf do
render_pdf.delay
end
end
end
Rendering the response from the controller is instant and blocking I/O. You can not achieve this as you think. Instead you can move pdf generation to background job and give the user notice like 'PDF will be sent soon by email'. Once PDF generation done in delayed job you can just email the PDF from the delayed job.
respond_to do |format|
format.html
format.pdf do
render_pdf.delay
redirect_to some_path, 'PDF will be sent soon by email'
end
end
Logic need to be added in delayed job to deliver PDF.