Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. Delayed::Job.enqueue PdfReceiptJob.new(@order.id)
  2.  
  3. class PdfReceiptJob < Struct.new(:order_id)
  4.  
  5. def perform
  6.  
  7. order = Order.find(order_id)
  8.  
  9. # create an instance of ActionView, so we can use the render method outside of a controller
  10. view = ActionView::Base.new(ActionController::Base.view_paths, {})
  11. html_template = view.render(file: "order/receipt_attachment.html.erb", locals:{order:order})
  12.  
  13. # use wicked_pdf gem to create PDF from the HTML receipt template
  14. pdf_receipt = WickedPdf.new.pdf_from_string(html_template, :page_size => 'Letter')
  15.  
  16. # save PDF to disk. Later, to be stored in fog, sent to S3, than saved back to our DB
  17. pdf_path = Rails.root.join("tmp", "marmoset-receipt#{order.id}.pdf") #ex: will be at tmp/marmoset-receipt215.pdf
  18.  
  19. File.open(pdf_path, 'wb') do |file|
  20. file << pdf_receipt
  21. end
  22.  
  23. end
  24. end
  25.  
  26. class ReceiptUploader < CarrierWave::Uploader::Base
  27.  
  28. storage :fog
  29.  
  30. # Override the directory where uploaded files will be stored.
  31. # This is a sensible default for uploaders that are meant to be mounted:
  32. def store_dir
  33. "uploads/#{model.class.to_s.underscore}/#{mounted_as}/marmoset-receipt#{model.id}"
  34. end
  35.  
  36. #set headers for pdf file
  37. def fog_attributes
  38. {'Content-Disposition' => "attachment;"}
  39. end
  40.  
  41. # Add a white list of extensions which are allowed to be uploaded.
  42. def extension_white_list
  43. %w(pdf)
  44. end
  45.  
  46.  
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement