rails-pdf-renderer 0.1.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 +9 -0
- data/README.md +4 -0
- data/lib/rails/pdf/renderer/action_view_helper.rb +118 -17
- data/lib/rails/pdf/renderer/config.rb +1 -1
- data/lib/rails/pdf/renderer/path_helper.rb +0 -29
- data/lib/rails/pdf/renderer/version.rb +1 -1
- metadata +3 -3
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
@@ -2,6 +2,15 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
4
|
|
5
|
+
## [0.3.0]
|
6
|
+
### Fixes
|
7
|
+
Fix Propshaft encoding issue
|
8
|
+
|
9
|
+
## [0.2.0]
|
10
|
+
### Fixes
|
11
|
+
- Removed hardcoded URL
|
12
|
+
- Removed unused helper methods
|
13
|
+
|
5
14
|
## [0.1.0]
|
6
15
|
### New Features
|
7
16
|
- Initial version
|
data/README.md
CHANGED
@@ -16,6 +16,10 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
16
16
|
|
17
17
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
18
18
|
|
19
|
+
## Releasing new gems
|
20
|
+
1. Update version.rb
|
21
|
+
2. Run `rake release` to release a new gem
|
22
|
+
|
19
23
|
## Contributing
|
20
24
|
|
21
25
|
Bug reports and pull requests are welcome on GitHub at https://github.com/erikaxel/rails-pdf-renderer.
|
@@ -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
|
@@ -3,7 +3,7 @@ class RailsPdfRenderer
|
|
3
3
|
include ActiveSupport::Configurable
|
4
4
|
|
5
5
|
config_accessor :auth_key
|
6
|
-
config_accessor
|
6
|
+
config_accessor :url
|
7
7
|
config_accessor(:basic_auth) { false }
|
8
8
|
config_accessor(:default_protocol) { "https" }
|
9
9
|
|
@@ -1,36 +1,7 @@
|
|
1
1
|
class RailsPdfRenderer
|
2
2
|
module PathHelper
|
3
|
-
def self.root_path
|
4
|
-
String === Rails.root ? Pathname.new(Rails.root) : Rails.root
|
5
|
-
end
|
6
|
-
|
7
3
|
def self.add_extension(filename, extension)
|
8
4
|
filename.to_s.split('.').include?(extension) ? filename : "#{filename}.#{extension}"
|
9
5
|
end
|
10
|
-
|
11
|
-
def wicked_pdf_stylesheet_link_tag(*sources)
|
12
|
-
css_dir = PathHelper.root_path.join('public', 'stylesheets')
|
13
|
-
css_text = sources.collect do |source|
|
14
|
-
source = PathHelper.add_extension(source, 'css')
|
15
|
-
"<style type='text/css'>#{File.read(css_dir.join(source))}</style>"
|
16
|
-
end.join("\n")
|
17
|
-
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
|
18
|
-
end
|
19
|
-
|
20
|
-
def wicked_pdf_image_tag(img, options = {})
|
21
|
-
image_tag "file:///#{PathHelper.root_path.join('public', 'images', img)}", options
|
22
|
-
end
|
23
|
-
|
24
|
-
def wicked_pdf_javascript_src_tag(jsfile, options = {})
|
25
|
-
jsfile = PathHelper.add_extension(jsfile, 'js')
|
26
|
-
type = ::Mime.respond_to?(:[]) ? ::Mime[:js] : ::Mime::JS # ::Mime[:js] cannot be used in Rails 2.3.
|
27
|
-
src = "file:///#{PathHelper.root_path.join('public', 'javascripts', jsfile)}"
|
28
|
-
content_tag('script', '', { 'type' => type, 'src' => path_to_javascript(src) }.merge(options))
|
29
|
-
end
|
30
|
-
|
31
|
-
def wicked_pdf_javascript_include_tag(*sources)
|
32
|
-
js_text = sources.collect { |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
|
33
|
-
js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
|
34
|
-
end
|
35
6
|
end
|
36
7
|
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:
|
11
|
+
date: 2024-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
- !ruby/object:Gem::Version
|
123
123
|
version: '0'
|
124
124
|
requirements: []
|
125
|
-
rubygems_version: 3.
|
125
|
+
rubygems_version: 3.5.21
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Create PDFs directly from Rails.
|