spree_print_invoice icon indicating copy to clipboard operation
spree_print_invoice copied to clipboard

Generate invoice to string for e-mail attachment

Open lilith opened this issue 10 years ago • 15 comments

How can this system be called to generate a binary string of the PDF from an arbitrary ActionMailer?

lilith avatar Oct 03 '14 20:10 lilith

This probably isn't the best way of doing it, but here's what I have implemented as a decorator.

  def confirm_email(order, resend = false)
    @order = order.respond_to?(:id) ? order : Spree::Order.find(order)

    filename = "Invoice_#{@order.number}_#{Time.now.strftime('%Y%m%d')}.pdf"

    admin_controller = Spree::Admin::OrdersController.new
    invoice = admin_controller.render_to_string(:layout => false , :template => "spree/admin/orders/invoice.pdf.prawn", :type => :prawn, :locals => {:@order => @order})

    attachments[filename] = {
      mime_type: 'application/pdf',
      content: invoice
    }

    subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
    subject += "#{Spree::Config[:site_name]} #{Spree.t('order_mailer.confirm_email.subject')} ##{@order.number}"
    headers = {to: @order.email, from: from_address, subject: subject}
    mail(headers)
  end

davekiss avatar Nov 06 '14 00:11 davekiss

You welcome to add it to the wiki if you found a good solution.

futhr avatar Dec 17 '14 18:12 futhr

Thanks, this helped us a lot!

lilith avatar Dec 17 '14 18:12 lilith

Nice one, thanks!

paulhuisman avatar Mar 02 '15 11:03 paulhuisman

:+1: on wiki, but this could also become an optional built-in feature with a enabling preference!

alepore avatar Apr 03 '15 12:04 alepore

I am currently working on a feature that has an option to store pdf as file on disk. This can then be attached to an email.

tvdeyen avatar Apr 03 '15 12:04 tvdeyen

nice, but it will need to stay in sync with order changes (like a cache..)

alepore avatar Apr 03 '15 12:04 alepore

What do you exactly mean by that? Could you provide an example? That would be great. Thanks

tvdeyen avatar Apr 03 '15 12:04 tvdeyen

i mean.. orders can change after completion (think of admins manual intervention, or payment states..), and the pdf should always reflect those changes, so the old file should be invalidated and a new one created.

alepore avatar Apr 03 '15 12:04 alepore

An Invoice must never change after sending it to the user. So the invoice may only send to the user after it is finished (the user has payed it). After that it must not change. Only if a refund happens, then the original invoice needs to be invalidated and a refund invoice has to be send to the user. German laws ;)

tvdeyen avatar Apr 03 '15 13:04 tvdeyen

Yes, makes sense. I was thinking using it just like the pdf render of the order details and not as a legal document...

alepore avatar Apr 03 '15 13:04 alepore

Yes. It makes definitely sense to have two different emails.

tvdeyen avatar Apr 03 '15 13:04 tvdeyen

@alepore I published my PR, please review and provide feedback. Thanks

tvdeyen avatar Apr 07 '15 14:04 tvdeyen

@tvdeyen How did you all ensure that these files were available over multiple nodes? I need to comply with german laws as well :(

rposborne avatar May 07 '15 16:05 rposborne

@rposborne Do you mean multiple app servers? Then, I would prefer to use an NFS mount to store the files on.

tvdeyen avatar May 07 '15 20:05 tvdeyen