我正在开发一个Ruby on Rails应用程序,客户可以点击PDF链接,将其声明显示为PDF。我正在使用Prawn gem生成PDF。这一切都可以在我的本地机器上正常工作但是当我在点击PDF链接时将其加载到实时环境中时出现错误"无法加载PDF文档"。我已将此问题本地化为PDF中显示的图像,但我不知道为什么有证据有问题。
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],[2,0]).bounding_box do
image 'public/images/Statement/logo.png', width: 110
font_size 10
text 'Company Name', font_weight: 'bold'
font_size 9
text 'Company Address'
move_down 20
text 'Phone:'
text 'Fax:'
end
#font 'public/fonts/interstate_light_cond-webfont.ttf'
grid([0,2],[1,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
# grid([2,0],[2,3]).bounding_box do
# font_size(14)
# text @balance, color: '#0044AA', :align => :right, :valign => :bottom
# end
font_size(12)
end
def footer
grid([15,1],[15,2]).bounding_box do
text_box 'Powered by ', color: "#999", :align => :center, :valign => :bottom
end
end
def transactions_table
grid([3,0], [14,3]).bounding_box do
data = [%w(Date Description Amount)]
data += @rows.map{|r| [r.value_date, r.description, r.amount]}
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 = BLACK
row(0).font_weight = 'bold'
row(0).border_color = BLACK
column(2).align = :right
end
end
end
end
我尝试了多种路径但无济于事。网站结构如下;
对此的任何回答都将非常感激,因为我无法在线找到有关此特定问题的任何帮助。
答案 0 :(得分:0)
尝试以下方法:
检查文件系统中是否存在该文件,您的应用有权阅读该文件Rails.root.join('public','images','Statement','logo.png')
使用绝对路径product, category, product_has_category