Skip to content

Commit

Permalink
Base64 encode fonts and images for PDF rendering
Browse files Browse the repository at this point in the history
* Encode FontAwesome and Patternfly icon fonts
* Add new image tag and path helpers to encode assets when necessary
* Move pdf_generator_spec to main repo
  • Loading branch information
hayesr committed May 17, 2017
1 parent 9f64aa7 commit f7aa272
Show file tree
Hide file tree
Showing 27 changed files with 150 additions and 100 deletions.
21 changes: 21 additions & 0 deletions app/assets/stylesheets/pdf/_fontawesome.scss.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Encoded FontAwesome ttf
*/

@font-face {
font-family: 'FontAwesome';
src: url(<%= asset_data_uri("font-awesome/fontawesome-webfont.ttf") %>);
font-weight: normal;
font-style: normal;
}

/*
* Encoded PatternFly ttf
*/

@font-face {
font-family: "PatternFlyIcons-webfont";
src:url(<%= asset_data_uri("patternfly/PatternFlyIcons-webfont.ttf") %>);
font-weight: normal;
font-style: normal;
}
48 changes: 48 additions & 0 deletions app/assets/stylesheets/pdf/_summary.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
body {font: 12px/1.231 Arial,Helvetica,sans-serif; padding-top: 100px;}

#xyz_main {
position: absolute;
top: 0px;
left: 50%;
height: 200px !important;
width: 100px;
margin-left: -55px;
text-align: left;
}

#quadicon {color: #e3e4e4;float:left; height: 72px; padding: 0px 1px 1px 0px;position: relative;text-transform: none;width: 72px;}
#quadicon img {border:0px;}

.flobj { position: absolute;z-index: auto;width: 72px;}
.flobj p { margin: 0 0 0 0;padding: 0;color: #e3e4e4;vertical-align: middle;text-align:center;text-transform: none;text-shadow: 0 0 3px #000;font: normal 16px OpenSansSemibold,Arial,Helvetica,sans-serif !important;}
.a72 { padding: 3px 0 0 5px; width: 28px; height: 28px;font-size: 1.3em;line-height: 1.8em;}
.b72 { padding: 3px 0 0 37px; width: 28px; height: 28px;font-size: 1.3em;line-height: 1.8em;}
.c72 { padding: 38px 0 0 5px; width: 31px; height: 31px;font-size: 1.3em;line-height: 1.7em;}
.d72 { padding: 38px 0 0 39px; width: 31px; height: 31px;font-size: 1.3em;line-height: 1.7em;}
.e72 { padding: 6px 0 0 6px; width: 35px; height: 35px;font-size: 1.3em;line-height: 2em;}
.f72 { padding: 22px 0 0 24px; width: 24px; height: 24px;}
.g72 { padding: 16px 0 0 19px; width: 24px; height: 24px;}
.b72 img {margin-top: -3px; width: 34px; height: 34px;}
.a72 img,.c72 img,.d72 img{ width: 28px; height: 28px;}
.f72 img,.f72 img { width: 24px; height: 24px;}
.g72 img,.g72 img { width: 36px; height: 36px;}
.e72 img { width: 60px; height: 60px;}

dl{float:left;width:100%;}
dl.col2 dd {float:left;width:47%;margin:0;margin-right:15px;padding:0;}

table {background-color: #ededed;border-spacing: 1px;margin-bottom: 15px;width: 100%;color: #4b4b4b;}
table tr th {height: 19px;padding: 4px;padding-left:10px;font: normal 12px Arial,Helvetica,sans-serif !important;line-height: 19px;}
table tr td.label{font: 12px Arial,Helvetica,sans-serif;width: 33%; text-align: right;cursor: default;}
table tr td {background: #fff; vertical-align: top; padding: 3px 6px 3px 6px}
table tr td img {height: 16px;width: 16px;float: left; padding-right: 3px; }
table tr.row0 td {background: #fff}
table tr.row1 td {background: #f5f5f5}

.b72 .stretch {
background-repeat: no-repeat;
width: 35px;
height: 32px;
margin: -3px 0 -2px 1px;
border-top-right-radius: 15px;
}
44 changes: 4 additions & 40 deletions app/assets/stylesheets/pdf_summary.css
Original file line number Diff line number Diff line change
@@ -1,40 +1,4 @@
body {font: 12px/1.231 Arial,Helvetica,sans-serif; padding-top: 100px;}

#xyz_main {
position: absolute;
top: 0px;
left: 50%;
height: 200px !important;
width: 100px;
margin-left: -55px;
text-align: left;
}

#quadicon {color: #e3e4e4;float:left; height: 72px; padding: 0px 1px 1px 0px;position: relative;text-transform: none;width: 72px;}
#quadicon img {border:0px;}

.flobj { position: absolute;z-index: auto;width: 72px;}
.flobj p { margin: 0 0 0 0;padding: 0;color: #e3e4e4;vertical-align: middle;text-align:center;text-transform: none;text-shadow: 0 0 3px #000;font: normal 16px OpenSansSemibold,Arial,Helvetica,sans-serif !important;}
.a72 { padding: 3px 0 0 5px; width: 28px; height: 28px;font-size: 1.3em;line-height: 1.8em;}
.b72 { padding: 3px 0 0 37px; width: 28px; height: 28px;font-size: 1.3em;line-height: 1.8em;}
.c72 { padding: 38px 0 0 5px; width: 31px; height: 31px;font-size: 1.3em;line-height: 1.7em;}
.d72 { padding: 38px 0 0 39px; width: 31px; height: 31px;font-size: 1.3em;line-height: 1.7em;}
.e72 { padding: 6px 0 0 6px; width: 35px; height: 35px;font-size: 1.3em;line-height: 2em;}
.f72 { padding: 22px 0 0 24px; width: 24px; height: 24px;}
.g72 { padding: 16px 0 0 19px; width: 24px; height: 24px;}
.b72 img {margin-top: -3px; width: 34px; height: 34px;}
.a72 img,.c72 img,.d72 img{ width: 28px; height: 28px;}
.f72 img,.f72 img { width: 24px; height: 24px;}
.g72 img,.g72 img { width: 36px; height: 36px;}
.e72 img { width: 60px; height: 60px;}

dl{float:left;width:100%;}
dl.col2 dd {float:left;width:47%;margin:0;margin-right:15px;padding:0;}

table {background-color: #ededed;border-spacing: 1px;margin-bottom: 15px;width: 100%;color: #4b4b4b;}
table tr th {height: 19px;padding: 4px;padding-left:10px;font: normal 12px Arial,Helvetica,sans-serif !important;line-height: 19px;}
table tr td.label{font: 12px Arial,Helvetica,sans-serif;width: 33%; text-align: right;cursor: default;}
table tr td {background: #fff; vertical-align: top; padding: 3px 6px 3px 6px}
table tr td img {height: 16px;width: 16px;float: left; padding-right: 3px; }
table tr.row0 td {background: #fff}
table tr.row1 td {background: #f5f5f5}
/*
*= require pdf/_summary
*= require pdf/_fontawesome
*/
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ApplicationController < ActionController::Base
helper ToolbarHelper
helper JsHelper
helper QuadiconHelper
helper ImageEncodeHelper

helper CloudResourceQuotaHelper

Expand Down Expand Up @@ -156,6 +157,9 @@ def download_summary_pdf
@embedded = true
@showlinks = false

# encode images and embed in HTML that is sent to Prince
@base64_encode_images = true

@record = identify_record(params[:id])
yield if block_given?
return if record_no_longer_exists?(@record)
Expand Down
33 changes: 33 additions & 0 deletions app/helpers/image_encode_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module ImageEncodeHelper
def encodable_image_tag(source, options = {})
image_tag(encodable_image_source(source), options)
end
alias eimage_tag encodable_image_tag

def encodable_image_source(source)
if base64_encode_images? && source.present?
base64_encoded_uri(source)
else
path_to_image(source)
end
end
alias eimage_source encodable_image_source

def base64_encode_images?
@base64_encode_images
end

def base64_encoded_uri(source)
asset = Rails.application.assets[source]

if asset.content_type == 'image/svg+xml'
encoding = 'charset=utf-8'
data = CGI.escape(asset.source).gsub('+', '%20')
else
encoding = 'base64'
data = Base64.encode64(asset.source)
end

"data:#{asset.content_type};#{encoding},#{data}"
end
end
40 changes: 18 additions & 22 deletions app/helpers/quadicon_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def quadicon_edit_key?(key)
!!(@edit && @edit[key])
end

def quadicon_encode_images?
!!@quadicon_encode_images
end

#
# Ways of Building URLs
# Collect here to see if any can be eliminated
Expand Down Expand Up @@ -146,6 +150,8 @@ def render_quadicon(item, options = {})
tag_options[:class] = ""
end

@quadicon_encode_images = options[:encode_images]

quadicon_tag(tag_options) do
quadicon_builder_factory(item, options)
end
Expand Down Expand Up @@ -323,14 +329,8 @@ def quadicon_link_to(url, sparkle: false, remote: false, options: {}, &block)
#
def quadicon_reflection_img(options = {})
path = options.delete(:path) || "layout/reflection.png"

options = {
:border => 0,
:width => 72,
:height => 72,
}.merge(options)

image_tag(image_path(path), options)
options = { :border => 0, :size => 72 }.merge(options)
encodable_image_tag(path, options)
end

CLASSLY_NAMED_ITEMS = %w(
Expand Down Expand Up @@ -395,14 +395,10 @@ def flobj_img_simple(image = nil, cls = '', size = 72)
image ||= "layout/base-single.png"

content_tag(:div, :class => "flobj #{cls}") do
image_tag(image, :size => size)
encodable_image_tag(image, :size => size)
end
end

def flobj_img_small(image = nil, cls = '')
flobj_img_simple(image, cls, 64)
end

def flobj_p_simple(cls, text)
content_tag(:div, :class => "flobj #{cls}") do
content_tag(:p, text)
Expand All @@ -425,7 +421,7 @@ def render_service_quadicon(item, options)

output << content_tag(:div, :class => "flobj e72") do
quadicon_link_to(url, **link_opts) do
quadicon_reflection_img(:path => item.decorate.fileicon)
quadicon_reflection_img(options.merge!(:path => item.decorate.fileicon))
end
end

Expand All @@ -439,7 +435,7 @@ def render_resource_pool_quadicon(item, options)
output = []

output << flobj_img_simple
output << flobj_img_small(img, "e72")
output << flobj_img_simple(img, "e72", 64)
output << flobj_img_simple('100/shield.png', "g72") unless item.get_policies.empty?

unless options[:typ] == :listnav
Expand All @@ -458,7 +454,7 @@ def render_resource_pool_quadicon(item, options)
def currentstate_icon(state)
path = "svg/currentstate-#{h(state)}.svg"
content_tag(:div, :class => "flobj b72") do
content_tag(:div, '', :class => "stretch", :style => "background-image: url('#{image_path(path)}')")
content_tag(:div, '', :class => "stretch", :style => "background-image: url('#{encodable_image_source(path)}')")
end
end

Expand All @@ -477,7 +473,7 @@ def render_host_quadicon(item, options)
output << flobj_img_simple('100/shield.png', "g72") unless item.get_policies.empty?
else
output << flobj_img_simple
output << flobj_img_small(img_for_host_vendor(item), "e72")
output << flobj_img_simple(img_for_host_vendor(item), "e72", 64)
end

if options[:typ] == :listnav
Expand Down Expand Up @@ -515,7 +511,7 @@ def render_ext_management_system_quadicon(item, options)
output << flobj_img_simple('100/shield.png', "g72") unless item.get_policies.empty?
else
output << flobj_img_simple("layout/base-single.png")
output << flobj_img_small("svg/vendor-#{h(item.image_name)}.svg", "e72")
output << flobj_img_simple("svg/vendor-#{h(item.image_name)}.svg", "e72", 64)
end

if options[:typ] == :listnav
Expand All @@ -541,7 +537,7 @@ def render_ems_cluster_quadicon(item, options)
output = []

output << flobj_img_simple("layout/base-single.png")
output << flobj_img_small("100/emscluster.png", "e72")
output << flobj_img_simple("100/emscluster.png", "e72", 64)
output << flobj_img_simple("100/shield.png", "g72") unless item.get_policies.empty?

unless options[:typ] == :listnav
Expand Down Expand Up @@ -607,7 +603,7 @@ def render_listicon_single_quadicon(item, options)
output = []

output << flobj_img_simple("layout/base-single.png")
output << flobj_img_small("100/#{@listicon}.png", "e72")
output << flobj_img_simple("100/#{@listicon}.png", "e72", 64)

unless options[:typ] == :listnav
title = case @listicon
Expand Down Expand Up @@ -740,10 +736,10 @@ def render_vm_or_template_quadicon(item, options)
output << flobj_img_simple("layout/base-single.png")

if quadicon_policy_sim? && !session[:policies].empty?
output << flobj_img_small(img_for_compliance(item), "e72")
output << flobj_img_simple(img_for_compliance(item), "e72", 64)
end

output << flobj_img_small(img_for_vendor(item), "e72")
output << flobj_img_simple(img_for_vendor(item), "e72", 64)
end

unless options[:typ] == :listnav
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/show_pdf.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= render :partial => "layouts/pdf_styles"
- controller = %w(miq_template vm_infra vm_or_template vm).include?(controller_name) ? "vm_common" : controller_name
#xyz_main
= render_quadicon(@record, :mode => :icon, :size => 72)
#xyz_main.encode_images
= render_quadicon(@record, :mode => :icon, :size => 72, :encode_images => true)
- if @record.kind_of?(ConfigurationProfile)
= render :partial => "#{controller}/main_configuration_profile"
- elsif @record.kind_of?(ManageIQ::Providers::AutomationManager::InventoryGroup)
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/summary/_icon_or_image.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- if item[:icon]
%i{:class => item[:icon], :title => item[:title]}
- elsif item[:image]
= image_tag(image_path(item[:image]), :alt => item[:title], :title => item[:title])
= encodable_image_tag(item[:image], :alt => item[:title], :title => item[:title])
2 changes: 2 additions & 0 deletions spec/helpers/quadicon_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
# FIXME: complex describe blocks mirror the existing complex control flow

describe QuadiconHelper do
helper(ImageEncodeHelper)

describe "#render_quadicon" do
context "when vm_or_template" do
subject { helper.render_quadicon(item) }
Expand Down
35 changes: 0 additions & 35 deletions spec/lib/pdf_generator_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe "layouts/listnav/_availability_zone.html.haml" do
helper(ImageEncodeHelper)
helper(QuadiconHelper)

before :each do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe "layouts/listnav/_cloud_network.html.haml" do
helper(ImageEncodeHelper)
helper(QuadiconHelper)

before :each do
Expand Down
1 change: 1 addition & 0 deletions spec/views/layouts/listnav/_cloud_subnet.html.haml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe "layouts/listnav/_cloud_subnet.html.haml" do
helper(ImageEncodeHelper)
helper(QuadiconHelper)

before :each do
Expand Down
1 change: 1 addition & 0 deletions spec/views/layouts/listnav/_cloud_tenant.html.haml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe "layouts/listnav/_cloud_tenant.html.haml" do
helper(ImageEncodeHelper)
helper(QuadiconHelper)

before :each do
Expand Down
1 change: 1 addition & 0 deletions spec/views/layouts/listnav/_ems_cloud.html.haml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe "layouts/listnav/_ems_cloud.html.haml" do
helper(ImageEncodeHelper)
helper(QuadiconHelper)

before :each do
Expand Down
1 change: 1 addition & 0 deletions spec/views/layouts/listnav/_ems_cluster.html.haml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe "layouts/listnav/_ems_cluster.html.haml" do
helper(ImageEncodeHelper)
helper(QuadiconHelper)

before :each do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe "layouts/listnav/_ems_container.html.haml" do
helper(ImageEncodeHelper)
helper(QuadiconHelper)

before :each do
Expand Down
Loading

0 comments on commit f7aa272

Please sign in to comment.