spree-contrib / spree_print_invoice

Create a PDF invoice for Spree orders.
http://guides.spreecommerce.org
BSD 3-Clause "New" or "Revised" License
91 stars 239 forks source link

Generate invoice to string for e-mail attachment #60

Open lilith opened 10 years ago

lilith commented 10 years ago

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

davekiss commented 10 years ago

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
futhr commented 9 years ago

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

lilith commented 9 years ago

Thanks, this helped us a lot!

paulhuisman commented 9 years ago

Nice one, thanks!

alepore commented 9 years ago

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

tvdeyen commented 9 years ago

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.

alepore commented 9 years ago

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

tvdeyen commented 9 years ago

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

alepore commented 9 years ago

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.

tvdeyen commented 9 years ago

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 ;)

alepore commented 9 years ago

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

tvdeyen commented 9 years ago

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

tvdeyen commented 9 years ago

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

rposborne commented 9 years ago

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

tvdeyen commented 9 years ago

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