adzap-wicked_pdf 2.0.0.beta2 → 2.0.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4ea1861662d27d078f0687835542b048648b15237258699e266f0ab018fd608
4
- data.tar.gz: 47dc57c0ed8a638a3ab3921ad746248c6c2a72f2ab355b2448f2188ac2718388
3
+ metadata.gz: 00a2f0d862aef8fdabd275049bdeb153fde574191baf868e38331e024f9c9a20
4
+ data.tar.gz: 9a3a4eab190d8e2eeffb72c3d7ef8dc9e682c08298c3865a500827fbb90f2418
5
5
  SHA512:
6
- metadata.gz: 2a41b903e2d397afcf0f20fc0b72f5a9fbecf97f3e6f75f974fe0a827fa8f961dd09d9d0a29828c3b5cb3fd30f2c9066016eb9f88c3eb64892c4fa5a4a8606d7
7
- data.tar.gz: 4730471489cd16958963fabacdf9bb25c4509539491c20ec9706ffecb0a678f8b5e8cf34e9aacfafc04ceb2b17ab670039e4558cc3b2639bcca35e2a4f11bbbe
6
+ metadata.gz: 35308c7be05d32e170d2de22e215a5c3c491cf111864c61ba9120e15fd635d7c0f41f6441df54d4605f39b3d8f690a8fbde26f1ee0b2cedc1ab22cc18064deab
7
+ data.tar.gz: 65925e576958b7bc555168c0c90f8d6040d0f4435dd6c8e55eff629b5d034ab30bb9d54a0d6fbf0095a5b2c9f7a0a0e47d94baf36980c6f83372471a80df36a3
data/.gitignore CHANGED
@@ -18,4 +18,4 @@ tmp
18
18
  *.swp
19
19
  gemfiles/*.lock
20
20
  .DS_Store
21
- test/dummy/db/
21
+ test/dummy/db
data/Gemfile CHANGED
@@ -1,5 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'byebug'
4
-
5
3
  gemspec
@@ -1,4 +1,4 @@
1
- require 'open-uri'
1
+ require 'net/http'
2
2
 
3
3
  module WickedPdf
4
4
  module AssetHelper
@@ -124,8 +124,7 @@ module WickedPdf
124
124
  end
125
125
 
126
126
  def read_from_uri(uri)
127
- encoding = ':UTF-8'
128
- asset = open(uri, "r#{encoding}", &:read)
127
+ asset = Net::HTTP.get(URI(uri))
129
128
  asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
130
129
  asset
131
130
  end
@@ -9,6 +9,7 @@ module WickedPdf
9
9
 
10
10
  def execute(options, *args)
11
11
  command = [binary.path]
12
+ command << '--enable-local-file-access' # FIXME should be set as config
12
13
  command += option_parser.parse(options)
13
14
  command += args
14
15
 
@@ -12,11 +12,12 @@ module WickedPdf
12
12
  options = options.dup
13
13
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
14
14
  string_file = WickedPdf::Tempfile.new('wicked_pdf.html', options[:temp_path])
15
- string_file.binmode
16
- string_file.write(string)
17
- string_file.close
15
+ string_file.write_in_chunks(string)
18
16
 
19
17
  pdf_from_html_file(string_file.path, options)
18
+ rescue Errno::EINVAL => e
19
+ Rails.logger.error '[wicked_pdf] The HTML file is too large! Try reducing the size or using the return_file option instead.'
20
+ raise e
20
21
  ensure
21
22
  string_file.close! if string_file
22
23
  end
@@ -25,21 +26,22 @@ module WickedPdf
25
26
  # merge in global config options
26
27
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
27
28
  generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
29
+ return_file = options.delete(:return_file)
28
30
 
29
- result = @command.execute(options, url, generated_pdf_file.path.to_s)
31
+ err = @command.execute(options, url, generated_pdf_file.path.to_s)
30
32
 
31
- if options[:return_file]
32
- return_file = options.delete(:return_file)
33
+ if return_file
33
34
  return generated_pdf_file
34
35
  end
35
36
 
36
- generated_pdf_file.rewind
37
- generated_pdf_file.binmode
38
- pdf = generated_pdf_file.read
37
+ pdf = generated_pdf_file.read_in_chunks
39
38
 
40
- raise "PDF could not be generated!\n Command Error: #{result}" if pdf && pdf.rstrip.empty?
39
+ raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
41
40
 
42
41
  pdf
42
+ rescue Errno::EINVAL => e
43
+ Rails.logger.error '[wicked_pdf] The PDF file is too large! Try reducing the size or using the return_file option instead.'
44
+ raise e
43
45
  ensure
44
46
  generated_pdf_file.close! if generated_pdf_file && !return_file
45
47
  end
@@ -5,11 +5,13 @@ require 'wicked_pdf/asset_helper'
5
5
  module WickedPdf
6
6
  class Railtie < Rails::Railtie
7
7
  initializer 'wicked_pdf.register' do |_app|
8
- ActionController::Base.send :prepend, PdfHelper
9
- ActionController::Renderers.add :pdf do |template, options|
10
- WickedPdf::Renderer.new(self).render(options.merge(:pdf => template))
11
- end
12
- ActionView::Base.send :include, WickedPdf::AssetHelper
8
+ ActiveSupport.on_load(:action_controller) {
9
+ ActionController::Base.send :prepend, PdfHelper
10
+ ActionController::Renderers.add :pdf do |template, options|
11
+ WickedPdf::Renderer.new(self).render(options.merge(pdf: template))
12
+ end
13
+ }
14
+ ActiveSupport.on_load(:action_view) { include WickedPdf::AssetHelper }
13
15
  end
14
16
  end
15
17
  end
@@ -2,8 +2,6 @@ module WickedPdf
2
2
  class Renderer
3
3
  attr_reader :controller
4
4
 
5
- delegate :request, :send_data, :controller_path, :action_name, :to => :controller
6
-
7
5
  def initialize(controller)
8
6
  @controller = controller
9
7
  @hf_tempfiles = []
@@ -23,25 +21,13 @@ module WickedPdf
23
21
  private
24
22
 
25
23
  def set_basic_auth(options = {})
26
- options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false }
27
- return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION']
28
- request.env['HTTP_AUTHORIZATION'].split(' ').last
24
+ options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth, false)
25
+ return unless options[:basic_auth] && controller.request.env['HTTP_AUTHORIZATION']
26
+ controller.request.env['HTTP_AUTHORIZATION'].split(' ').last
29
27
  end
30
28
 
31
29
  def make_pdf(options = {})
32
- render_opts = {
33
- :template => options[:template],
34
- :prefixes => options[:prefixes],
35
- :layout => options[:layout],
36
- :formats => options[:formats],
37
- :handlers => options[:handlers],
38
- :assigns => options[:assigns]
39
- }
40
- render_opts[:inline] = options[:inline] if options[:inline]
41
- render_opts[:locals] = options[:locals] if options[:locals]
42
- render_opts[:file] = options[:file] if options[:file]
43
-
44
- html_string = controller.render_to_string(render_opts)
30
+ html_string = controller.render_to_string(render_options(options))
45
31
  options = prerender_header_and_footer(options)
46
32
 
47
33
  document = WickedPdf::Document.new(command(options[:wkhtmltopdf]))
@@ -52,26 +38,14 @@ module WickedPdf
52
38
 
53
39
  def make_and_send_pdf(pdf_name, options = {})
54
40
  options[:layout] ||= false
55
- options[:template] ||= File.join(controller_path, action_name)
41
+ options[:template] ||= File.join(controller.controller_path, controller.action_name)
56
42
  options[:disposition] ||= 'inline'
57
43
  if options[:show_as_html]
58
- render_opts = {
59
- :template => options[:template],
60
- :prefixes => options[:prefixes],
61
- :layout => options[:layout],
62
- :formats => options[:formats],
63
- :handlers => options[:handlers],
64
- :assigns => options[:assigns],
65
- :content_type => 'text/html'
66
- }
67
- render_opts[:inline] = options[:inline] if options[:inline]
68
- render_opts[:locals] = options[:locals] if options[:locals]
69
- render_opts[:file] = options[:file] if options[:file]
70
- render(render_opts)
44
+ controller.render(render_options(options).merge(:content_type => 'text/html'))
71
45
  else
72
46
  pdf_content = make_pdf(options)
73
47
  File.open(options[:save_to_file], 'wb') { |file| file << pdf_content } if options[:save_to_file]
74
- send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only]
48
+ controller.send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only]
75
49
  end
76
50
  end
77
51
 
@@ -82,22 +56,17 @@ module WickedPdf
82
56
  next unless options[hf] && options[hf][:html] && options[hf][:html][:template]
83
57
 
84
58
  options[hf][:html][:layout] ||= options[:layout]
85
- render_opts = {
86
- :template => options[hf][:html][:template],
87
- :layout => options[hf][:html][:layout],
88
- :formats => options[hf][:html][:formats],
89
- :handlers => options[hf][:html][:handlers],
90
- :assigns => options[hf][:html][:assigns]
91
- }
92
- render_opts[:locals] = options[hf][:html][:locals] if options[hf][:html][:locals]
93
- render_opts[:file] = options[hf][:html][:file] if options[:file]
94
-
59
+ render_opts = render_options(options[hf][:html])
95
60
  path = render_to_tempfile("wicked_#{hf}_pdf.html", render_opts)
96
61
  options[hf][:html][:url] = "file:///#{path}"
97
62
  end
98
63
  options
99
64
  end
100
65
 
66
+ def render_options(options)
67
+ options.slice(:template, :prefixes, :layout, :formats, :handlers, :assigns, :inline, :locals, :file)
68
+ end
69
+
101
70
  def render_to_tempfile(filename, options)
102
71
  tf = WickedPdf::Tempfile.new(filename)
103
72
  @hf_tempfiles.push(tf)
@@ -1,13 +1,43 @@
1
1
  require 'tempfile'
2
+ require 'stringio'
2
3
 
3
- module WickedPdf
4
- class Tempfile < Tempfile
5
- # ensures the Tempfile's filename always keeps its extension
4
+ class WickedPdf
5
+ class Tempfile < ::Tempfile
6
6
  def initialize(filename, temp_dir = nil)
7
7
  temp_dir ||= Dir.tmpdir
8
8
  extension = File.extname(filename)
9
- basename = File.basename(filename, extension)
9
+ basename = File.basename(filename, extension)
10
10
  super([basename, extension], temp_dir)
11
11
  end
12
+
13
+ def write_in_chunks(input_string)
14
+ binmode
15
+ string_io = StringIO.new(input_string)
16
+ write(string_io.read(chunk_size)) until string_io.eof?
17
+ close
18
+ self
19
+ rescue Errno::EINVAL => e
20
+ raise e, file_too_large_message
21
+ end
22
+
23
+ def read_in_chunks
24
+ rewind
25
+ binmode
26
+ output_string = ''
27
+ output_string << read(chunk_size) until eof?
28
+ output_string
29
+ rescue Errno::EINVAL => e
30
+ raise e, file_too_large_message
31
+ end
32
+
33
+ private
34
+
35
+ def chunk_size
36
+ 1024 * 1024
37
+ end
38
+
39
+ def file_too_large_message
40
+ 'The HTML file is too large! Try reducing the size or using the return_file option instead.'
41
+ end
12
42
  end
13
43
  end
@@ -1,3 +1,3 @@
1
1
  module WickedPdf
2
- VERSION = '2.0.0.beta2'.freeze
2
+ VERSION = '2.0.0.beta4'.freeze
3
3
  end
data/test/test_helper.rb CHANGED
@@ -2,11 +2,10 @@
2
2
  ENV['RAILS_ENV'] = 'test'
3
3
 
4
4
  require 'combustion'
5
- require 'byebug'
6
5
 
7
6
  Combustion.path = 'test/dummy'
8
7
  Combustion.initialize!(:all) do
9
- if config.active_record.sqlite3.respond_to?(:represent_boolean_as_integer) # Rails 5.2
8
+ if Rails::VERSION::MAJOR < 6.0 && config.active_record.sqlite3.respond_to?(:represent_boolean_as_integer) # Rails 5.2
10
9
  config.active_record.sqlite3.represent_boolean_as_integer = true
11
10
  end
12
11
  end
data/wicked_pdf.gemspec CHANGED
@@ -28,11 +28,12 @@ DESC
28
28
 
29
29
  spec.add_dependency 'activesupport'
30
30
 
31
- spec.add_development_dependency 'rails'
31
+ spec.add_development_dependency 'rails', '< 6.0'
32
32
  spec.add_development_dependency 'combustion'
33
33
  spec.add_development_dependency 'bundler', '>= 1.3.0', '< 3'
34
34
  spec.add_development_dependency 'rake'
35
35
  spec.add_development_dependency 'rubocop', '~> 0.50.0'
36
- spec.add_development_dependency 'sqlite3', '~> 1.3.6'
36
+ spec.add_development_dependency 'sqlite3', '<= 1.4'
37
37
  spec.add_development_dependency 'mocha', '= 1.3'
38
+ spec.add_development_dependency 'sprockets', '~> 3.0'
38
39
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adzap-wicked_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta2
4
+ version: 2.0.0.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Z. Sterrett
8
8
  - David Jones
9
9
  - Adam Meehan
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-03-30 00:00:00.000000000 Z
13
+ date: 2023-10-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -30,16 +30,16 @@ dependencies:
30
30
  name: rails
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - ">="
33
+ - - "<"
34
34
  - !ruby/object:Gem::Version
35
- version: '0'
35
+ version: '6.0'
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ">="
40
+ - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '0'
42
+ version: '6.0'
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: combustion
45
45
  requirement: !ruby/object:Gem::Requirement
@@ -106,16 +106,16 @@ dependencies:
106
106
  name: sqlite3
107
107
  requirement: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - "~>"
109
+ - - "<="
110
110
  - !ruby/object:Gem::Version
111
- version: 1.3.6
111
+ version: '1.4'
112
112
  type: :development
113
113
  prerelease: false
114
114
  version_requirements: !ruby/object:Gem::Requirement
115
115
  requirements:
116
- - - "~>"
116
+ - - "<="
117
117
  - !ruby/object:Gem::Version
118
- version: 1.3.6
118
+ version: '1.4'
119
119
  - !ruby/object:Gem::Dependency
120
120
  name: mocha
121
121
  requirement: !ruby/object:Gem::Requirement
@@ -130,6 +130,20 @@ dependencies:
130
130
  - - '='
131
131
  - !ruby/object:Gem::Version
132
132
  version: '1.3'
133
+ - !ruby/object:Gem::Dependency
134
+ name: sprockets
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '3.0'
140
+ type: :development
141
+ prerelease: false
142
+ version_requirements: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '3.0'
133
147
  description: |
134
148
  Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
135
149
  In other words, rather than dealing with a PDF generation DSL of some sort,
@@ -201,7 +215,7 @@ homepage: https://github.com/adzap/wicked_pdf
201
215
  licenses:
202
216
  - MIT
203
217
  metadata: {}
204
- post_install_message:
218
+ post_install_message:
205
219
  rdoc_options: []
206
220
  require_paths:
207
221
  - lib
@@ -217,9 +231,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
231
  version: 1.3.1
218
232
  requirements:
219
233
  - wkhtmltopdf
220
- rubyforge_project:
221
- rubygems_version: 2.7.6.2
222
- signing_key:
234
+ rubygems_version: 3.4.19
235
+ signing_key:
223
236
  specification_version: 4
224
237
  summary: PDF generator (from HTML) gem for Ruby on Rails
225
238
  test_files: