我正在开发一个Ruby on Rails应用程序,它使用prawn在用户点击链接时生成PDF。在localhost上运行应用程序时,该功能可以正常工作,但是当我将其部署到服务器并单击链接时,文件不会加载“无法加载PDF文档”消息。
我做过一些研究,并在其他地方看到有关生成宝石的其他文件的参考资料,但没有特定于Prawn。任何有关其他信息的请求都将很乐意满足。
我认为显示代码可能有所帮助,所以这里。它是财务应用程序,其中页面列出带有视图按钮和pdf图标的语句,以查看它们的pdf格式。当用户点击pdf图标大虾然后进入游戏。希望这有助于澄清我的问题。
控制器代码:
require 'json'
InternetBanking.controllers :statements do
get :index do
@today = Time.now
render 'statements/listing'
end
get :listing do
@today = Time.now
render 'statements/listing'
end
get :activity, provides: [:json] do
content_type :json
@customer.accounts.map{|x| {x.name => x.activity}}.to_json
end
get :show, map: "/statements/:id", provides: [:html, :pdf] do
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 from logged in customer, prevent crafted URLs.
@account = @customer.fetch_account(accountno)
raise Exception if @account.nil?
@statement = @account.statement(year, month)
# TODO: Deal with 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 an empty month field'
end
end
end
型号代码:
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 to keep the line measure with a 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') do
define_grid(columns: 4, rows: 16, gutter: 10)
header
transactions_table
footer
end
end
private
def header
grid([0,0],[1,0]).bounding_box do
image 'public/images/Statement/logo.png', width: 110
end
grid([0,1],[1,1]).bounding_box do
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 do
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 do
image 'public/images/statement-logo.png', width: 100
end
end
def transactions_table
grid([2,0], [13,3]).bounding_box do
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) do
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
查看代码:
- content_for :title, 'Full Statement Listing'
- content_for :toolbar do
%a.back(href='/balances') Back
%table
%thead
%tr
%th.year(rowspan=2) Year
%th.month(rowspan=2) Month
%th.accounts{colspan: @customer.accounts.size} Accounts
%tr
- @customer.accounts.each do |account|
%th= account.name
- @today.year.downto(@today.year - 3) do |year|
%tbody
- x = (@today.year == year) ? @today.month : 12
- x.downto(1) do |month|
%tr
- if month == x
%td{rowspan: x}= year
%td= Date::MONTHNAMES[month]
- @customer.accounts.each do |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