You are here: Forums Ask a Rails expert Send dynamicly generated pdf a...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 69
Sign in to rate this post
|
Hi, I’ve got a form and I’d like to send the value of that form as an attachement (pdf) to me. I’d like to only make it possible that only the send action could access/generate that pdf. How could I do this? |
|
Authority 25
Posting Rating 96
Sign in to rate this post
|
Refer to Rails API of ActionMailer::Base about sending mails with attachments. According to the document that you could just pass the content of generated PDF as params of the mailer class, looks like this:
def generated_pdf(recipient, pdf)
attachment "application/pdf" { |a| a.body = pdf }
end
FYI, to get the content of generated PDF, the “render_as_string” method and read the response.body after rendering automatically will be both useful in this situation. |
Authority 0
Posting Rating 69
Sign in to rate this post
|
And how can I then generate the pdf document? |
|
Authority 25
Posting Rating 0
Sign in to rate this post
|
You can move the logic that creates the pdf from the .prawn file to your controller or even better to a model. You will want to use pdf = Prawn::Document.new and the render method to create a string version of the pdf which can be attached to the email. Check out http://prawn.majesticseacreature.com/docs/prawn-core/classes/Prawn/Document.html#M000097 and http://prawn.majesticseacreature.com/docs/prawn-core/classes/Prawn/Document.html#M000101. |
|
Authority 25
Posting Rating 96
Sign in to rate this post
|
You do not need the real PDF file as attachment. Actually, only the generated content (binary stream) of this pdf document is necessary. You can access to this stream via response object. Your codes of controller should looks like the following: def show # just a example render # force to generate pdf using prawnto so we can access to the response object pdf = response.body YourMailer.deliver_generated_pdf(current_user, pdf) end |
Authority 0
Posting Rating 69
Sign in to rate this post
|
I’m sorry for my late response, I had some last exams. On topic : I’ve tried to generate a pdf in my controller (that didn’t go so fine), but recently I found this site (http://dizzy.co.uk/ruby_on_rails/cheatsheets/action-mailer#multipart_messages), and I quote ActionMailer will automatically detect and use multipart templates, where each template is named after the name of the method, followed by the content type. Each such detected template will be added as a separate part to the message. For example: signup_notification.text.plain.erb Each would be rendered and added as a separate part to the message with the corresponding content type. The same body hash is passed to each template. So in my notifier view folder I created a send_mail.html.erb template and a send_mail.pdf.prawn, but when I send the email, the pdf attachement isn’t added. Could anyone tell me if this info is correct and what I’m doing wrong? |
Authority 12
Posting Rating 75
Sign in to rate this post
|
Hi Wouter, How about this? In your controller, you generate the PDF file:
In your notifier action mailer:
Hope that helps. Something wrong with format here, so I paste the code in here Sonny |
Authority 0
Posting Rating 69
Sign in to rate this post
|
Thanks Sonny, that’s working fine but the problem is that I have 200 and more lines of code for generating the pdf and I’d like to keep it out of my controller so that it can stay clean. |
Authority 0
Posting Rating 0
Sign in to rate this post
|
Wouter, Have you ever managed to get this working? I’m attempting to do the same thing. Basically what I want to do is have my model access the prawn pdf so I can easily attach the file when I send the email out. I found that you do it from the controller using: But apparently you can’t use render_to_string in the model or in a rake task. For a model and rake task I found these articles following those I figured in my Invoice Model I could do something like this
My ideas was in my mailer I could attach the file using something like this.
But in the end I’m stuck with a weird prawn error ActionView::TemplateError: I know I’m not offering any solution but maybe some of this might point you into the right direction. Let me know if you ever get it working. |
Authority 0
Posting Rating 69
Sign in to rate this post
|
Austin, I didn’t manage to fix this problem, I keep sending the mails in plain text rather than in pdf. |
Authority 12
Posting Rating 0
Sign in to rate this post
|
Austin, Thanks to the start you gave me, I’ve managed to get this working today: Here’s the code from my model that generates a PDF from a Prawnto view and saves it to a file:
This is a first draft of a working version, so can probably be cleaned up a litte (or lot!). Of course, YMMV, and this is a total cludge of the MVC principles – but sometimes you’ve just got to… Hope this helps out there. |
Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController
Ask a Rails expert : Best way to structure a database for a large/static dataset
Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)
Ask a Rails expert : Above Ground Pool Supplies
Ask a Rails expert : How to get url params in observer or model in Rails 3.1
Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?
Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?
Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )
Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob
Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )