gem 'prawn', :git => "git://github.com/sandal/prawn.git", :submodules => true
$ mkdir app/reports
config.autoload_paths << "#{Rails.root}/app/reports"
Mime::Type.register_alias "application/pdf", :pdf
require "prawn"
$ rails c Loading development environment (Rails 3.0.9) ruby-1.9.2-p180 :001 > Prawn::BASEDIR => "/home/kir/.rvm/gems/ruby-1.9.2-p180@pdfer/bundler/gems/prawn-1288242ddece"
rails g scaffold customer name:string amount:float
rake db:migrate
# encoding: utf-8 class CustomersReport < Prawn::Document # Widths = [200, 200, 120] # Headers = [' ', '', ''] def row(date, customer_name, amount) row = [date, customer_name, amount] make_table([row]) do |t| t.column_widths = Widths t.cells.style :borders => [:left, :right], :padding => 2 end end def to_pdf # font_families.update( "Verdana" => { :bold => "/home/kir/prawn_fonts/verdanab.ttf", :italic => "/home/kir/prawn_fonts/verdanai.ttf", :normal => "/home/kir/prawn_fonts/verdana.ttf" }) font "Verdana", :size => 10 text " #{Time.zone.now.strftime('%b %Y')}", :size => 15, :style => :bold, :align => :center move_down(18) # @customers = Customer.order('created_at') data = [] items = @customers.each do |item| data << row(item.created_at.strftime('%d/%m/%y %H:%M'), item.friendly_name, item.) end head = make_table([Headers], :column_widths => Widths) table([[head], *(data.map{|d| [d]})], :header => true, :row_colors => %w[cccccc ffffff]) do row(0).style :background_color => '000000', :text_color => 'ffffff' cells.style :borders => [] end # creation_date = Time.zone.now.strftime(" %e %b %Y %H:%M") go_to_page(page_count) move_down(710) text creation_date, :align => :right, :style => :italic, :size => 9 render end end
def download_pdf output = CustomersReport.new.to_pdf send_data output, :type => 'application/pdf', :filename => "customers.pdf" end
resources :customers do collection do get 'download_pdf' end end
namespace :customers_report do desc 'Generates report with customers' task :generate_pdf => :environment do output = CustomersReport.new.to_pdf filename = "report.pdf" File.open(Rails.root.join('public', filename), 'wb') do |f| f.write(output) end puts "Report was written to #{filename}" end end
rake customers_report:generate_pdf --trace
rails g mailer customers_mailer report_email
and change the code app / mailers / customers_mailer.rb: class CustomersMailer < ActionMailer::Base default :from => "me@yandexteam.ru" # ! def report_email(pdf_output, to) report_filename = Time.zone.now.strftime('Report %d-%m-%Y') attachments[report_filename] = { :mime_type => 'application/pdf', :content => pdf_output } mail(:to => to, :subject => report_filename.titleize) end end
namespace :customers_report do desc 'Generates report with customers' task :generate_pdf => :environment do #... end desc 'Send report by email' task :send_by_email => :environment do # # recipient = ["vasya@gmail.com", "petya@gmail.com"] recipient = 'me@yandexteam.ru' # ! output = CustomersReport.new.to_pdf CustomersMailer.report_email(output, recipient).deliver puts "Report was sent to #{recipient}" end end
rake customers_report:send_by_email --trace
Source: https://habr.com/ru/post/120388/
All Articles