Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Delayed::Job.enqueue PdfReceiptJob.new(@order.id)
- class PdfReceiptJob < Struct.new(:order_id)
- def perform
- order = Order.find(order_id)
- # create an instance of ActionView, so we can use the render method outside of a controller
- view = ActionView::Base.new(ActionController::Base.view_paths, {})
- html_template = view.render(file: "order/receipt_attachment.html.erb", locals:{order:order})
- # use wicked_pdf gem to create PDF from the HTML receipt template
- pdf_receipt = WickedPdf.new.pdf_from_string(html_template, :page_size => 'Letter')
- # save PDF to disk. Later, to be stored in fog, sent to S3, than saved back to our DB
- pdf_path = Rails.root.join("tmp", "marmoset-receipt#{order.id}.pdf") #ex: will be at tmp/marmoset-receipt215.pdf
- File.open(pdf_path, 'wb') do |file|
- file << pdf_receipt
- end
- end
- end
- class ReceiptUploader < CarrierWave::Uploader::Base
- storage :fog
- # Override the directory where uploaded files will be stored.
- # This is a sensible default for uploaders that are meant to be mounted:
- def store_dir
- "uploads/#{model.class.to_s.underscore}/#{mounted_as}/marmoset-receipt#{model.id}"
- end
- #set headers for pdf file
- def fog_attributes
- {'Content-Disposition' => "attachment;"}
- end
- # Add a white list of extensions which are allowed to be uploaded.
- def extension_white_list
- %w(pdf)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement