ruby on rails - Prawn generating PDF on localhost but not on server -
i working on ruby on rails application uses prawn generate pdf's when user clicks on link. functionality works when running application on localhost when deploy server , click on link file doesnt load message "failed load pdf document".
i have done research , seen references in other places regarding other documents generating gems none specific prawn. requests additional information gladly met.
i think showing code might here goes. financial application page lists statements view button , pdf icon view them pdf format. when user clicks on pdf icon prawn comes play. hope clarify problem somewhat.
controller code:
require 'json' internetbanking.controllers :statements :index @today = time.now render 'statements/listing' end :listing @today = time.now render 'statements/listing' end :activity, provides: [:json] content_type :json @customer.accounts.map{|x| {x.name => x.activity}}.to_json end :show, map: "/statements/:id", provides: [:html, :pdf] begin m = params[:id].force_encoding('utf-8').match(/(\d+)-(\w+)-(\d+)/) accountno = m[1].to_i month = date::monthnames.index(m[2]) year = m[3].to_i raise exception if month.nil? or year.nil? # fetch account_id logged in customer, prevent crafted urls. @account = @customer.fetch_account(accountno) raise exception if @account.nil? @statement = @account.statement(year, month) # todo: deal no transactions if content_type == :pdf content_type 'application/pdf' @statement.to_pdf else # html render 'statements/show' end rescue flash[:error]= 'you cannot have empty month field' end end end
model code:
statement.rb
class statement attr_accessor :opening_balance, :closing_balance def initialize(acct, year = nil, month = nil) @db = database.instance @account = acct @year = year @month = month @month_name = date::monthnames[@month] end def to_s "#{@account.number}-#{@month_name}-#{@year}" end def to_pdf title = @account.name subtitle = "account statement: #{@month_name} #{@year}" statementpdf.new(title, subtitle, transactions).render end def transactions return @transactions if @transactions start = date.civil(@year, @month).strftime('%y-%m-%d') finish = date.civil(@year, @month, -1).strftime('%y-%m-%d') rows = @db.call('ibstatement2', @account.account_id, start, finish) @transactions = rows.map {|txn| transaction.new(txn)} end end
statement_pdf.rb
require 'prawn' class statementpdf < prawn::document box_margin = 36 # additional indentation keep line measure reasonable size inner_margin = 30 # vertical rhythm settings rhythm = 10 leading = 2 # colors black = "000000" light_gray = "f2f2f2" gray = "dddddd" dark_gray = "333333" brown = "a4441c" orange = "f28157" light_gold = "fbfbbe" dark_gold = "ebe389" blue = "0000d0" grey = "cccccc" def initialize(title, subtitle, rows) @rows = rows @title = title @subtitle = subtitle super(page_size: 'a4') define_grid(columns: 4, rows: 16, gutter: 10) header transactions_table footer end end private def header grid([0,0],[1,0]).bounding_box image 'public/images/statement/logo.png', width: 110 end grid([0,1],[1,1]).bounding_box font_size 10 text 'yada', font_weight: 'bold' font_size 9 text 'yada' text 'yada' text 'yada' text 'yada' text 'yada' text 'yada' end grid([0,2],[0,3]).bounding_box font_size(20) text @title, color: '#0044aa', :align => :right font_size(14) text @subtitle, color: '#0044aa', :align => :right, :valign => :bottom end font_size(12) end def footer grid([15,0],[15,3]).bounding_box image 'public/images/statement-logo.png', width: 100 end end def transactions_table grid([2,0], [13,3]).bounding_box data = [%w(date description amount balance)] data += @rows.map{|r| [r.value_date, r.description, r.amount, r.balance]} options = { header: true, width: 520, column_widths: {0 => 100, 2 => 100}, row_colors: ['eeeeee', 'ffffff']} table(data, options) cells.padding = 5 cells.border_width = 0.5 cells.border_color = grey row(0).font_weight = 'bold' row(0).border_color = black row(1).border_top_color = black column(2).align = :right column(3).align = :right end end end end
view code:
- content_for :title, 'full statement listing' - content_for :toolbar %a.back(href='/balances') %table %thead %tr %th.year(rowspan=2) year %th.month(rowspan=2) month %th.accounts{colspan: @customer.accounts.size} accounts %tr - @customer.accounts.each |account| %th= account.name - @today.year.downto(@today.year - 3) |year| %tbody - x = (@today.year == year) ? @today.month : 12 - x.downto(1) |month| %tr - if month == x %td{rowspan: x}= year %td= date::monthnames[month] - @customer.accounts.each |acct| - if acct.activity[year] && acct.activity[year][month] %td{'data-transactions' => acct.activity[year][month]} - stmt = acct.statement(year, month) = link_to 'view', url(:statements, :show, id: stmt.to_s) = link_to image_tag('/icons/document-pdf.png', alt: 'download pdf'), url(:statements, :show, id: stmt, format: :pdf), class: :pdf - else %td
Comments
Post a Comment