Module: PdfHelper::AssetSupport

Defined in:
lib/princely/pdf_helper.rb

Instance Method Summary collapse

Instance Method Details

#asset_file_path(asset) ⇒ Object



79
80
81
82
# File 'lib/princely/pdf_helper.rb', line 79

def asset_file_path(asset)
  # Remove /assets/ from generated names and try and find a matching asset
  Rails.application.assets.find_asset(asset.gsub(/\/assets\//, "")).try(:pathname) || asset
end

#localize_html_string(html_string) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/princely/pdf_helper.rb', line 69

def localize_html_string(html_string)
  # Make all paths relative, on disk paths...
  html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
  html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| "src=\"#{asset_file_path($1)}\"" } # re-route absolute paths
  
  # Remove asset ids on images with a regex
  html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| 'src="' + $1.split('?').first + '"' }
  html_string
end