Save a Prawn PDF as a Paperclip attachment?

后端 未结 5 1188
悲&欢浪女
悲&欢浪女 2021-02-04 01:24

I\'m using Prawn and Prawnto to display a PDF-based reports to the user, but in some circumstances, I\'d also like to save the PDF as an attachment to one of my models. I\'m usi

5条回答
  •  星月不相逢
    2021-02-04 02:10

    I got it working without instance eval by turning it the other way around : generate the PDF in your model and render it in you controller

    In a model :

      def generate_pdf
        Prawn::Document.new(:page_size => 'A4', :top_margin => 0, :left_margin => 0) do |pdf|
            
            
        end.render
      end
    

    You can then send it as a mail attachment :

    attachment = generate_pdf
    mail = Notifier.send_pdf(attachment)
    mail.deliver
    

    Or render it in your browser windows in your controller :

    send_data your_model.generate_pdf, :type => "application/pdf", :disposition => 'inline'
    

提交回复
热议问题