Sha256: fd2aebf26a9199fbb696f2516f3ef22fd7ba194e086e1b685c686508c75c51a6

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

# encoding: utf-8
# Prince XML Ruby interface. 
# http://www.princexml.com
#
# Library by Subimage Interactive - http://www.subimage.com
#
#
# USAGE
# -----------------------------------------------------------------------------
#   prince = Prince.new()
#   html_string = render_to_string(:template => 'some_document')
#   send_data(
#     prince.pdf_from_string(html_string),
#     :filename => 'some_document.pdf'
#     :type => 'application/pdf'
#   )
#

class Prince
  
  attr_accessor :exe_path, :style_sheets, :log_file

  # Initialize method
  #
  def initialize()
    # Finds where the application lives, so we can call it.
    @exe_path = defined?(PRINCE_PATH) ? PRINCE_PATH : `which prince`.chomp
  	@style_sheets = ''
  	@log_file = "#{RAILS_ROOT}/log/prince.log"
  end
  
  # Sets stylesheets...
  # Can pass in multiple paths for css files.
  #
  def add_style_sheets(*sheets)
    for sheet in sheets do
      @style_sheets << " -s #{sheet} "
    end
  end
  
  # Returns fully formed executable path with any command line switches
  # we've set based on our variables.
  #
  def exe_path
    # Add any standard cmd line arguments we need to pass
    @exe_path << " --input=html --server --log=#{@log_file} "
    @exe_path << @style_sheets
    return @exe_path
  end
  
  # Makes a pdf from a passed in string.
  #
  # Returns PDF as a stream, so we can use send_data to shoot
  # it down the pipe using Rails.
  #
  def pdf_from_string(string)
    path = self.exe_path()
    # Don't spew errors to the standard out...and set up to take IO 
    # as input and output
    path << ' --silent - -o -'
    
    # Show the command used...
    #logger.info "\n\nPRINCE XML PDF COMMAND"
    #logger.info path
    #logger.info ''
    
    # Actually call the prince command, and pass the entire data stream back.
    pdf = IO.popen(path, "wb+")
    pdf.puts(string)
    pdf.close_write
    output = pdf.gets(nil)
    pdf.close_read
    return output
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
tournament-3.3.2 webgui/lib/prince.rb
tournament-3.3.1 webgui/lib/prince.rb
tournament-3.3.0 webgui/lib/prince.rb
tournament-3.2.2 webgui/lib/prince.rb
tournament-3.2.1 webgui/lib/prince.rb
tournament-3.2.0 webgui/lib/prince.rb
tournament-3.1.1 webgui/lib/prince.rb
tournament-3.1.0 webgui/lib/prince.rb
tournament-3.0.3 webgui/lib/prince.rb