Last active
August 29, 2015 14:01
-
-
Save ilake/3ab5cbba2ab8ae74f89c to your computer and use it in GitHub Desktop.
princely study
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/princely/asset_support.rb | |
# Rails.application.assets.find_asset is useful to find asset. | |
# you could get the content through | |
# Rails.application.assets.find_asset('analytics.css.sass').body | |
# get pathname through | |
# Rails.application.assets.find_asset('analytics.css.sass').pathname | |
def asset_file_path(asset) | |
# Remove /assets/ from generated names and try and find a matching asset | |
Rails.application.assets.find_asset(asset.gsub(%r{/assets/}, "")).try(:pathname) || asset | |
end | |
# IO.popen is useful. | |
# http://www.ruby-doc.org/core-2.1.1/IO.html#method-c-popen | |
# You could use other tool, give them data or take data from them. | |
def initialize_pdf_from_string(string, output_file, options = {}) | |
options = {:log_command => true, :output_to_log_file => true}.merge(options) | |
path = exe_path | |
# Don't spew errors to the standard out...and set up to take IO | |
# as input and output | |
path << " --silent - -o #{output_file}" | |
path << " >> '#{log_file}' 2>> '#{log_file}'" if options[:output_to_log_file] | |
log_command path if options[:log_command] | |
# Actually call the prince command, and pass the entire data stream back. | |
pdf = IO.popen(path, "w+") | |
pdf.puts string | |
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. | |
# | |
# http://www.ruby-doc.org/core-2.1.1/IO.html#method-i-close_read | |
# http://www.ruby-doc.org/core-2.1.1/IO.html#method-i-close_write | |
# http://www.ruby-doc.org/core-2.1.1/IO.html#method-i-gets | |
# | |
def pdf_from_string(string, output_file = '-') | |
pdf = initialize_pdf_from_string(string, output_file, {:output_to_log_file => false}) | |
pdf.close_write | |
result = pdf.gets(nil) # nil parameters will make it read the entire contents. | |
pdf.close_read | |
result.force_encoding('BINARY') if RUBY_VERSION >= "1.9" | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment