Skip to content

Commit

Permalink
Added ability to specify logo for first page
Browse files Browse the repository at this point in the history
* An admin can enter a URL on the plugin configuration page which will
  be included on the first page of all PDFs (in the top-right corner)
  [closes #15]
  • Loading branch information
tomkersten committed Jun 19, 2011
1 parent ca864d0 commit 24fcc84
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 9 deletions.
7 changes: 7 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
=== NEXT

* New features:
* 'Logo' support. Admins can add the URL of an image on the plugin
configuration page. The logo will show up on the top-right corner
of the first page of all exported PDFs. [closes #15]

=== 0.4.0 / 2011-06-18

* New features:
Expand Down
18 changes: 18 additions & 0 deletions app/helpers/chili_pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ def update_img_src_tags_of(content, wants_html = false)
end


# 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.
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

private
# Updates the value of the specified attribute of any `tag_type` tags
# contained in `content` to be compatible with the `wkhtmltopdf`
Expand Down
2 changes: 2 additions & 0 deletions app/views/extended_wiki/show.pdf.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
<%= normalize_custom_js_src_tags_in(ChiliPDF::Config.custom_js, @requesting_html_version) %>
<% end %>
<%= logo_img_tag(@requesting_html_version) %>
<%- html_content = textilizable(@content.text, :attachments => @content.page.attachments) %>
<%= update_img_src_tags_of(html_content, @requesting_html_version) %>
3 changes: 2 additions & 1 deletion app/views/settings/_chili_pdf_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<%= render_tabs([
{:name => 'typical_settings', :partial => 'settings/chili_pdf_typical_settings', :label => :typical_plugin_settings},
{:name => 'advanced_settings', :partial => 'settings/chili_pdf_advanced_settings', :label => :advanced_plugin_settings},
{:name => 'watermark_settings', :partial => 'settings/chili_pdf_watermark_settings', :label => :watermark_plugin_settings},
{:name => 'advanced_settings', :partial => 'settings/chili_pdf_advanced_settings', :label => :advanced_plugin_settings}
]) %>

6 changes: 3 additions & 3 deletions app/views/settings/_chili_pdf_typical_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
</p>

<p>
<label for='settings_<%= ChiliPDF::Config::HEADER_ENABLED_KEYNAME %>'><%= l(:header_enabled_label) %></label>
<label for='settings_<%= ChiliPDF::Config::HEADER_ENABLED_KEYNAME %>'><%= l(:label_header_enabled) %></label>
<%= check_box_tag "settings[#{ChiliPDF::Config::HEADER_ENABLED_KEYNAME}]", ChiliPDF::Config::ENABLED_VALUE, ChiliPDF::Config.header_enabled? %>
</p>

<p>
<label for='settings_<%= ChiliPDF::Config::FOOTER_ENABLED_KEYNAME %>'><%= l(:footer_enabled_label) %></label>
<label for='settings_<%= ChiliPDF::Config::FOOTER_ENABLED_KEYNAME %>'><%= l(:label_footer_enabled) %></label>
<%= check_box_tag "settings[#{ChiliPDF::Config::FOOTER_ENABLED_KEYNAME}]", ChiliPDF::Config::ENABLED_VALUE, ChiliPDF::Config.footer_enabled? %>
</p>

<h3>Custom Header/Footer Content</h3>

<table>
<table id="header-footer-settings">
<thead>
<tr>
<td></td>
Expand Down
8 changes: 8 additions & 0 deletions app/views/settings/_chili_pdf_watermark_settings.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p class="chili-pdf-instructions">
Enter the URL of an image you would like to appear on the first
page of all exported PDFs. You can enter a URL of a file you
have uploaded to this site, or any public URL.
<p>
<label for='settings_<%= ChiliPDF::Config::LOGO_KEYNAME%>'><%= l(:label_logo_url) %></label>
<%= text_field_tag "settings[#{ChiliPDF::Config::LOGO_KEYNAME}]", ChiliPDF::Config.logo_url, :class => 'url' %>
</p>
5 changes: 5 additions & 0 deletions assets/stylesheets/chili_pdf_settings_tweaks.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ textarea {width: 600px; height: 200px;}

p.chili-pdf-instructions {padding-left: 0;}
.tip-label {font-weight: bold; text-decoration: underline;}
.url {width: 600px;}

#header-footer-settings input {
width: 300px;
}

#pdf-token-help {margin-top: 20px;}
#pdf-token-help table {width: 100%; border: 1px #AAA solid;}
Expand Down
5 changes: 5 additions & 0 deletions assets/stylesheets/pdf.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ ol li {list-style-type: decimal;}
ul li {list-style-type: disc;}
h1, h2 {font-weight: bold;}
img {max-width: 100%;}

#chili-pdf-logo {
max-height: 50px;
float:right;
}
10 changes: 6 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# English strings go here for Rails i18n
en:
chili_pdf_instructions: 'You can customize the behavior of the ChiliPDf plugin with the settings below'
header_enabled_label: Display page header?
footer_enabled_label: Display page footer?
typical_plugin_settings: Typical Settings
advanced_plugin_settings: Technical Settings
typical_plugin_settings: Typical
advanced_plugin_settings: Technical
watermark_plugin_settings: Logo
label_header_enabled: Display page header?
label_footer_enabled: Display page footer?
label_custom_css: Custom CSS
label_custom_js: Custom JavaScript
label_logo_url: Logo URL
12 changes: 11 additions & 1 deletion lib/chili_pdf/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Config
FOOTER_LEFT_KEYNAME = :footer_content_left
FOOTER_CENTER_KEYNAME = :footer_content_center
FOOTER_RIGHT_KEYNAME = :footer_content_right
LOGO_KEYNAME = :watermarks_logo

HEADER_LEFT_DEFAULT_VALUE = '{{page_title}}'
HEADER_CENTER_DEFAULT_VALUE = ''
Expand Down Expand Up @@ -54,7 +55,8 @@ def defaults
FOOTER_CENTER_KEYNAME => FOOTER_CENTER_DEFAULT_VALUE,
FOOTER_RIGHT_KEYNAME => FOOTER_RIGHT_DEFAULT_VALUE,
CUSTOM_CSS_KEYNAME => CUSTOM_CSS_DEFAULT_VALUE,
CUSTOM_JS_KEYNAME => CUSTOM_JS_DEFAULT_VALUE
CUSTOM_JS_KEYNAME => CUSTOM_JS_DEFAULT_VALUE,
LOGO_KEYNAME => ''
}
end

Expand Down Expand Up @@ -111,6 +113,14 @@ def custom_js
end
end

def logo_url
plugin_settings[LOGO_KEYNAME]
end

def logo_url?
!logo_url.blank?
end

private
def default_header_values
{:left => HEADER_LEFT_DEFAULT_VALUE,
Expand Down
22 changes: 22 additions & 0 deletions test/unit/helpers/chili_pdf_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,26 @@ class ChiliPdfHelperTest < HelperTestCase
end
end
end

context "#logo_img_tag" do
context "when no logo URL is defined for the plugin" do
setup do
ChiliPDF::Config.stubs(:logo_url).returns("")
end

should "return nil" do
assert_nil logo_img_tag(true)
end
end

context "when a logo URL is defined" do
setup do
ChiliPDF::Config.stubs(:logo_url).returns("/images/cancel.png")
end

should "return an '<img>' tag with an 'id' of 'chili-pdf-logo'" do
assert_match /id="chili-pdf-logo"/, logo_img_tag(true)
end
end
end
end

0 comments on commit 24fcc84

Please sign in to comment.