0
votes

I am working on a Ruby on Rails application whereby customers can click on a PDF link that will display their statement as a PDF. I am using the Prawn gem to generate the PDF. This all works fine on my local machine however when i load it into the live environment when clicking on the PDF link the error "Failed to load PDF document". I have localised this problem to images displayed in the PDF however i have no clue as to why there are proving problematic.

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

I have tried multiple variations of the path but to no avail. The site structure is as follows;

  • files
  • fonts
  • icons
  • images (within this is Statement folder which is the location of the image i wish to use
  • javascripts
  • META-INF
  • stylesheets
  • WEB-INF

Any answer to this would be much appreciated as i cant find any help online with this specific problem.

1

1 Answers

0
votes

Try the following:

  • Check the file exists in the filesystem and your app has rights to read it ll public/images/Statement/

  • Use an absolute path Rails.root.join('public','images','Statement','logo.png')