Module: ChiliPdfHelper

Defined in:
app/helpers/chili_pdf_helper.rb

Constant Summary collapse

PLUGIN_JS_DIR =

Public directory of ChiliPDF plugin JavaScript files

Rails.root.join('public','plugin_assets', 'chili_pdf', 'javascripts')
PLUGIN_CSS_DIR =

Public directory of ChiliPDF plugin stylesheets

Rails.root.join('public','plugin_assets', 'chili_pdf', 'stylesheets')
PROTOTYPE_SCRIPT_TAG =

Standard <script>-tag for Prototype JavaScript file

"#{Rails.root}/public/javascripts/prototype.js"

Instance Method Summary collapse

Instance Method Details

#chili_pdf_javascripts(wants_html) ⇒ Object

Public: Generate JS <script> tags for all JavaScript files in the

plugin_assets/chili_pdf/stylesheets directory, formatting
the 'src' attribute value appropriately for HTML or PDF request
types

wants_html - specifies whether the ‘src’ attribute should be formatted

for HTML or PDF requests (local vs. relative paths). Added to
keep excessive boolean logic out of views.

Returns: string of <script> tags separated by a newline character



39
40
41
42
43
# File 'app/helpers/chili_pdf_helper.rb', line 39

def chili_pdf_javascripts(wants_html)
  file_type_list(:js, PLUGIN_JS_DIR, wants_html) {|asset_path|
    "<script src='#{asset_path}' type='text/javascript'></script>"
  }.join("\n")
end

#chili_pdf_stylesheets(wants_html) ⇒ Object

Public: Generates <link> tags for all CSS files in the

plugin_assets/chili_pdf/stylesheets directory, formatting
the 'href' attribute value appropriately for HTML or PDF
request types.

wants_html - specifies whether the ‘src’ attribute should be formatted

for HTML or PDF requests (local vs. relative paths). Added to
keep excessive boolean logic out of views.

Returns: String of link tags separated by a newline character.



22
23
24
25
26
# File 'app/helpers/chili_pdf_helper.rb', line 22

def chili_pdf_stylesheets(wants_html)
  file_type_list(:css, PLUGIN_CSS_DIR, wants_html) {|asset_path|
    "<link href='#{asset_path}' rel='stylesheet' type='text/css' />\n"
  }.join("\n")
end

#logo_img_tag(wants_html) ⇒ Object

Public: Generates an image tag with the appropriate mark-up for the ChiliPDF

'logo'.

wants_html - specifies whether the ‘src’ attribute should be formatted

for HTML or PDF requests (local vs. relative paths). Added to
keep excessive boolean logic out of views.

Returns an ‘<img>’ tag for your view template with a properly formatted ‘src’

attribute, depending on whether the user is requesting an HTML or PDF
format. If no logo is defined in the plugin configuration, nothing is
rendered in the view tempalate.


108
109
110
111
112
113
# File 'app/helpers/chili_pdf_helper.rb', line 108

def logo_img_tag(wants_html)
  if ChiliPDF::Config.logo_url?
    img_tag = image_tag(ChiliPDF::Config.logo_url, :id => "chili-pdf-logo")
    update_img_src_tags_of(img_tag, wants_html)
  end
end

#normalize_custom_js_src_tags_in(content, wants_html = false) ⇒ Object

Public: Converts ‘src’ attributes of any <script> tags (with a ‘src’

attribute) to be compatible with the `wkhtmltopdf` executable
requirements, using "file://"-format for all local assets.

content - the String of HTML content to normalize/update wants_html - specifies whether the ‘src’ attribute should be formatted

for HTML or PDF requests (local vs. relative paths). Added to
keep excessive boolean logic out of views.

Returns content un-modified if wants_html is true. Otherwise returns

the content string with the modified 'src' attribute on all
locally-hosted <script>-tags in content.


75
76
77
# File 'app/helpers/chili_pdf_helper.rb', line 75

def normalize_custom_js_src_tags_in(content, wants_html = false)
  update_tag_attribute(:script, :src, content, wants_html)
end

Public: Converts ‘href’ attributes of any <link> tags

tags to be compatible with the `wkhtmltopdf` executable
requirements, using "file://"-format for all local assets.

content - the String of HTML content to normalize/update wants_html - specifies whether the ‘src’ attribute should be formatted

for HTML or PDF requests (local vs. relative paths). Added to
keep excessive boolean logic out of views.

Returns content un-modified if wants_html is true. Otherwise returns

the content string with the modified 'href' attribute on all
locally-hosted <link>-tags in content.


58
59
60
# File 'app/helpers/chili_pdf_helper.rb', line 58

def normalize_custom_link_href_tags_in(content, wants_html = false)
  update_tag_attribute(:link, :href, content, wants_html)
end

#update_a_hrefs(content) ⇒ Object

Public: Converts ‘href’ attributes of any <a> tags to be compatible with

the `wkhtmltopdf` executable requirements (to absolute URLs).
Capable of handling URLs to both other domains and relative URLS.

content - the String of HTML content to normalize/update

Returns content un-modified if wants_html is true. Otherwise returns

the content string with the modified 'href' attribute on all
<a>-tags in content.


124
125
126
# File 'app/helpers/chili_pdf_helper.rb', line 124

def update_a_hrefs(content)
  update_tag_attribute(:a, :href, content, false, :absolute_url)
end

#update_img_src_tags_of(content, wants_html = false) ⇒ Object

Public: Converts ‘src’ attributes of any <img> tags to be compatible with

the `wkhtmltopdf` executable requirements, using "file://"-format
for all local assets.

content - the String of HTML content to normalize/update wants_html - specifies whether the ‘src’ attribute should be formatted

for HTML or PDF requests (local vs. relative paths). Added to
keep excessive boolean logic out of views.

Returns content un-modified if wants_html is true. Otherwise returns

the content string with the modified 'src' attribute on all
locally-hosted <img>-tags in content.


92
93
94
# File 'app/helpers/chili_pdf_helper.rb', line 92

def update_img_src_tags_of(content, wants_html = false)
  update_tag_attribute(:img, :src, content, wants_html)
end

Public: Converts ‘href’ attributes of any <a> tags and any ‘src’ attributes

of <img> tags contained in 'content' to be compatible with
the `wkhtmltopdf` executable (to absolute & 'file://-based' URLs).

content - the String of HTML content to normalize/update wants_html - specifies whether the attribute should be formatted

for HTML or PDF requests (local vs. relative paths). Added to
keep excessive boolean logic out of views.

Returns content un-modified if wants_html is true. Otherwise returns

the content string with the modified 'href' attribute on all
<a>-tags in content.


140
141
142
# File 'app/helpers/chili_pdf_helper.rb', line 140

def update_link_and_image_urls(content, wants_html)
  update_a_hrefs(update_img_src_tags_of(content, wants_html))
end