Module: WickedPdf::WickedPdfHelper::Assets
- Defined in:
- lib/wicked_pdf/wicked_pdf_helper/assets.rb
Defined Under Namespace
Classes: LocalAsset, MissingAsset, MissingLocalAsset, MissingRemoteAsset, PropshaftAsset, SprocketsEnvironment
Constant Summary
collapse
- ASSET_URL_REGEX =
/url\(['"]?([^'"]+?)['"]?\)/
Instance Method Summary
collapse
Instance Method Details
#wicked_pdf_asset_base64(path) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 75
def wicked_pdf_asset_base64(path)
asset = find_asset(path)
raise MissingLocalAsset, path if asset.nil?
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end
|
#wicked_pdf_asset_pack_path(asset) ⇒ Object
163
164
165
166
167
168
169
170
171
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 163
def wicked_pdf_asset_pack_path(asset)
return unless defined?(Webpacker)
if running_in_development?
asset_pack_path(asset)
else
wicked_pdf_asset_path webpacker_source_url(asset)
end
end
|
#wicked_pdf_asset_path(asset) ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 155
def wicked_pdf_asset_path(asset)
if (pathname = asset_pathname(asset).to_s) =~ URI_REGEXP
pathname
else
"file:///#{pathname}"
end
end
|
#wicked_pdf_image_tag(img, options = {}) ⇒ Object
139
140
141
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 139
def wicked_pdf_image_tag(img, options = {})
image_tag wicked_pdf_asset_path(img), options
end
|
#wicked_pdf_javascript_include_tag(*sources) ⇒ Object
148
149
150
151
152
153
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 148
def wicked_pdf_javascript_include_tag(*sources)
sources.collect do |source|
source = WickedPdfHelper.add_extension(source, 'js')
"<script type='text/javascript'>#{read_asset(source)}</script>"
end.join("\n").html_safe
end
|
#wicked_pdf_javascript_pack_tag(*sources) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 126
def wicked_pdf_javascript_pack_tag(*sources)
return unless defined?(Webpacker)
if running_in_development?
javascript_pack_tag(*sources)
else
sources.collect do |source|
source = WickedPdfHelper.add_extension(source, 'js')
"<script type='text/javascript'>#{read_asset(webpacker_source_url(source))}</script>"
end.join("\n").html_safe
end
end
|
#wicked_pdf_javascript_src_tag(jsfile, options = {}) ⇒ Object
143
144
145
146
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 143
def wicked_pdf_javascript_src_tag(jsfile, options = {})
jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
javascript_include_tag wicked_pdf_asset_path(jsfile), options
end
|
#wicked_pdf_stylesheet_link_tag(*sources) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 97
def wicked_pdf_stylesheet_link_tag(*sources)
stylesheet_contents = sources.collect do |source|
source = WickedPdfHelper.add_extension(source, 'css')
"<style type='text/css'>#{read_asset(source)}</style>"
end.join("\n")
stylesheet_contents.gsub(ASSET_URL_REGEX) do
if Regexp.last_match[1].starts_with?('data:')
"url(#{Regexp.last_match[1]})"
else
"url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
end
end.html_safe
end
|
#wicked_pdf_stylesheet_pack_tag(*sources) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 112
def wicked_pdf_stylesheet_pack_tag(*sources)
return unless defined?(Webpacker)
if running_in_development?
stylesheet_pack_tag(*sources)
else
css_text = sources.collect do |source|
source = WickedPdfHelper.add_extension(source, 'css')
wicked_pdf_stylesheet_link_tag(webpacker_source_url(source))
end.join("\n")
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
end
end
|
#wicked_pdf_url_base64(url) ⇒ Object
Using ‘image_tag` with URLs when generating PDFs (specifically large PDFs with lots of pages) can cause buffer/stack overflows.
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 85
def wicked_pdf_url_base64(url)
response = Net::HTTP.get_response(URI(url))
if response.is_a?(Net::HTTPSuccess)
base64 = Base64.encode64(response.body).gsub(/\s+/, '')
"data:#{response.content_type};base64,#{Rack::Utils.escape(base64)}"
else
Rails.logger.warn("[wicked_pdf] #{response.code} #{response.message}: #{url}")
nil
end
end
|