rails-pdf-renderer 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/rails/pdf/renderer/action_view_helper.rb +118 -17
- data/lib/rails/pdf/renderer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b4ec4f6dc891ddf5ebdb89311c9b9c7015a67245b0efee12a4d92aeb9b42e7d
|
4
|
+
data.tar.gz: 78e5a1550b403bdef40193c5e55d0f53eb8370d5db0e7349f730c5e746986e86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1be67035de112bf744f41c8e4762cf0ffe61b0687f6a35855e01472ef1b5f9e10b35d6119cc0b6634208b8d487899bacfd4531e4959a353ebb4fb0a9f7b1a329
|
7
|
+
data.tar.gz: b912f6f36c4d9fd77d6e0b918be7f62fb664c5a9a48e961d5aa0b850ad1b4a132f4a461938d817a46d712caaff6f92958d2c1e2e3a28851519015c0d7f997a11
|
data/CHANGELOG.md
CHANGED
@@ -7,6 +7,27 @@ class RailsPdfRenderer
|
|
7
7
|
module ActionViewHelper
|
8
8
|
ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
|
9
9
|
|
10
|
+
class MissingAsset < StandardError; end
|
11
|
+
|
12
|
+
class MissingLocalAsset < MissingAsset
|
13
|
+
attr_reader :path
|
14
|
+
|
15
|
+
def initialize(path)
|
16
|
+
@path = path
|
17
|
+
super("Could not find asset '#{path}'")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class MissingRemoteAsset < MissingAsset
|
22
|
+
attr_reader :url, :response
|
23
|
+
|
24
|
+
def initialize(url, response)
|
25
|
+
@url = url
|
26
|
+
@response = response
|
27
|
+
super("Could not fetch asset '#{url}': server responded with #{response.code} #{response.message}")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
10
31
|
class PropshaftAsset < SimpleDelegator
|
11
32
|
def content_type
|
12
33
|
super.to_s
|
@@ -21,9 +42,39 @@ class RailsPdfRenderer
|
|
21
42
|
end
|
22
43
|
end
|
23
44
|
|
45
|
+
class SprocketsEnvironment
|
46
|
+
def self.instance
|
47
|
+
@instance ||= Sprockets::Railtie.build_environment(Rails.application)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.find_asset(*args)
|
51
|
+
instance.find_asset(*args)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class LocalAsset
|
56
|
+
attr_reader :path
|
57
|
+
|
58
|
+
def initialize(path)
|
59
|
+
@path = path
|
60
|
+
end
|
61
|
+
|
62
|
+
def content_type
|
63
|
+
Mime::Type.lookup_by_extension(File.extname(path).delete('.'))
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_s
|
67
|
+
IO.read(path)
|
68
|
+
end
|
69
|
+
|
70
|
+
def filename
|
71
|
+
path.to_s
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
24
75
|
def pdf_asset_base64(path)
|
25
76
|
asset = find_asset(path)
|
26
|
-
raise
|
77
|
+
raise MissingLocalAsset, path if asset.nil?
|
27
78
|
|
28
79
|
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
|
29
80
|
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
|
@@ -149,18 +200,38 @@ class RailsPdfRenderer
|
|
149
200
|
if Rails.application.assets.respond_to?(:find_asset)
|
150
201
|
Rails.application.assets.find_asset(path, :base_path => Rails.application.root.to_s)
|
151
202
|
elsif defined?(Propshaft::Assembly) && Rails.application.assets.is_a?(Propshaft::Assembly)
|
152
|
-
|
153
|
-
|
154
|
-
|
203
|
+
PropshaftAsset.new(Rails.application.assets.load_path.find(path))
|
204
|
+
elsif Rails.application.respond_to?(:assets_manifest)
|
205
|
+
relative_asset_path = get_asset_path_from_manifest(path)
|
206
|
+
return unless relative_asset_path
|
207
|
+
|
208
|
+
asset_path = File.join(Rails.application.assets_manifest.dir, relative_asset_path)
|
209
|
+
LocalAsset.new(asset_path) if File.file?(asset_path)
|
155
210
|
else
|
156
|
-
|
211
|
+
SprocketsEnvironment.find_asset(path, :base_path => Rails.application.root.to_s)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def get_asset_path_from_manifest(path)
|
216
|
+
assets = Rails.application.assets_manifest.assets
|
217
|
+
|
218
|
+
if File.extname(path).empty?
|
219
|
+
assets.find do |asset, _v|
|
220
|
+
directory = File.dirname(asset)
|
221
|
+
asset_path = File.basename(asset, File.extname(asset))
|
222
|
+
asset_path = File.join(directory, asset_path) if directory != '.'
|
223
|
+
|
224
|
+
asset_path == path
|
225
|
+
end&.last
|
226
|
+
else
|
227
|
+
assets[path]
|
157
228
|
end
|
158
229
|
end
|
159
230
|
|
160
231
|
# will prepend a http or default_protocol to a protocol relative URL
|
161
232
|
# or when no protcol is set.
|
162
233
|
def prepend_protocol(source)
|
163
|
-
protocol =
|
234
|
+
protocol = WickedPdf.config[:default_protocol] || 'http'
|
164
235
|
if source[0, 2] == '//'
|
165
236
|
source = [protocol, ':', source].join
|
166
237
|
elsif source[0] != '/' && !source[0, 8].include?('://')
|
@@ -177,24 +248,48 @@ class RailsPdfRenderer
|
|
177
248
|
end
|
178
249
|
|
179
250
|
def read_asset(source)
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
251
|
+
asset = find_asset(source)
|
252
|
+
return asset.to_s.force_encoding('UTF-8') if asset
|
253
|
+
|
254
|
+
unless precompiled_or_absolute_asset?(source)
|
255
|
+
raise MissingLocalAsset, source if WickedPdf.config[:raise_on_missing_assets]
|
256
|
+
|
257
|
+
return
|
258
|
+
end
|
259
|
+
|
260
|
+
pathname = asset_pathname(source)
|
261
|
+
if pathname =~ URI_REGEXP
|
262
|
+
read_from_uri(pathname)
|
263
|
+
elsif File.file?(pathname)
|
264
|
+
IO.read(pathname)
|
265
|
+
elsif WickedPdf.config[:raise_on_missing_assets]
|
266
|
+
raise MissingLocalAsset, pathname if WickedPdf.config[:raise_on_missing_assets]
|
189
267
|
end
|
190
268
|
end
|
191
269
|
|
192
270
|
def read_from_uri(uri)
|
193
|
-
|
271
|
+
response = Net::HTTP.get_response(URI(uri))
|
272
|
+
|
273
|
+
unless response.is_a?(Net::HTTPSuccess)
|
274
|
+
raise MissingRemoteAsset.new(uri, response) if WickedPdf.config[:raise_on_missing_assets]
|
275
|
+
|
276
|
+
return
|
277
|
+
end
|
278
|
+
|
279
|
+
asset = response.body
|
194
280
|
asset.force_encoding('UTF-8') if asset
|
281
|
+
asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
|
195
282
|
asset
|
196
283
|
end
|
197
284
|
|
285
|
+
def gzip(asset)
|
286
|
+
stringified_asset = StringIO.new(asset)
|
287
|
+
gzipper = Zlib::GzipReader.new(stringified_asset)
|
288
|
+
gzipper.read
|
289
|
+
rescue Zlib::GzipFile::Error
|
290
|
+
nil
|
291
|
+
end
|
292
|
+
|
198
293
|
def webpacker_source_url(source)
|
199
294
|
return unless webpacker_version
|
200
295
|
|
@@ -224,7 +319,13 @@ class RailsPdfRenderer
|
|
224
319
|
end
|
225
320
|
|
226
321
|
def webpacker_version
|
227
|
-
|
322
|
+
if defined?(Shakapacker)
|
323
|
+
require 'shakapacker/version'
|
324
|
+
Shakapacker::VERSION
|
325
|
+
elsif defined?(Webpacker)
|
326
|
+
require 'webpacker/version'
|
327
|
+
Webpacker::VERSION
|
328
|
+
end
|
228
329
|
end
|
229
330
|
end
|
230
331
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-pdf-renderer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Axel Nielsen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|