首页 文章

Prawn Gem - 无法加载PDF文档

提问于
浏览
0

我正在开发一个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

我尝试了多种路径但无济于事 . 场地结构如下;

  • 个文件

  • 字体

  • 图标

  • images(这是Statement文件夹,这是我想要使用的图像的位置

  • javascripts

  • META-INF

  • 样式表

  • WEB-INF

任何答案都将非常感激,因为我无法在网上找到任何有关此特定问题的帮助 .

1 回答

  • 0

    请尝试以下方法:

    • 检查文件系统中是否存在文件,您的应用程序有权阅读它 ll public/images/Statement/

    • 使用绝对路径 Rails.root.join('public','images','Statement','logo.png')

相关问题