pdfkit 0.8.4.1 → 0.8.4.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pdfkit might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc5beb396f2168a0c0ca91ee77bba2af22421d445ea05bbb5f57f687957b75e1
4
- data.tar.gz: 96505e77627a3655edfd2c91c5ad190faea49fa7df184d456d838fa3ec620b08
3
+ metadata.gz: d07f9293cf2555d6bf3f44fce50ff185524fd202073fcd00fe0e6b2fb094963d
4
+ data.tar.gz: ede68296b5314d8572b002945360e0c2cb90d95f57440f8a6213d9054a436dcc
5
5
  SHA512:
6
- metadata.gz: b07cf202025b2f252250d76be4b293c2cb3a0ec1b89666d5aec74dafa377e675190074f7dd2e2eba7ea2d6744e1f31be75ecf632827ce27dc209fbf9f137e564
7
- data.tar.gz: 86c5e37ed66290882d4e7e0c7d7c7fb2b378068e9d1b28a4837acbc6268ee0da60819c5efdafc2c1ad0940adf30cbd084219fd3413efb1f9b4feefb5105dc55f
6
+ metadata.gz: 2447695954315c580730d87c1f0ed2353228ca3e564a5692fef67d4d2d2fc244e6f2442e1b69c433f35b92c685acc35cd3764c111dcaa69f773cd67e77d725e7
7
+ data.tar.gz: b106bb8ead667a62cca3f4790ea0c074301ea02ab693e7b39a5b7006ddc314700a95eed42507c007010c8134c40665024d32a421e664541aff8a0d6bd9ddd4db
@@ -0,0 +1,19 @@
1
+ name: Mark stale issues and pull requests
2
+
3
+ on:
4
+ schedule:
5
+ - cron: "0 0 * * *"
6
+
7
+ jobs:
8
+ stale:
9
+
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/stale@v1
14
+ with:
15
+ repo-token: ${{ secrets.GITHUB_TOKEN }}
16
+ stale-issue-message: 'This issue has been marked as stale and will be automatically closed.'
17
+ stale-pr-message: 'This pull request has been marked as stale and will be automatically closed.'
18
+ stale-issue-label: 'no-issue-activity'
19
+ stale-pr-label: 'no-pr-activity'
@@ -1 +1 @@
1
- 2.5.1
1
+ 2.6.4
@@ -1,16 +1,16 @@
1
+ language: ruby
2
+
1
3
  rvm:
2
- - 2.2
3
4
  - 2.3
4
5
  - 2.4
5
6
  - 2.5
7
+ - 2.6
6
8
 
7
9
  before_install:
8
10
  - gem update --system
9
11
  - gem update bundler
10
12
 
11
13
  before_script:
12
- - "export DISPLAY=:99.0"
13
- - "sh -e /etc/init.d/xvfb start"
14
14
  - "sudo apt-get -qq -y install fontconfig libxrender1"
15
- - "wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.trusty_amd64.deb"
16
- - "sudo apt-get install ./wkhtmltox_0.12.5-1.trusty_amd64.deb"
15
+ - "wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.xenial_amd64.deb"
16
+ - "sudo apt-get install ./wkhtmltox_0.12.5-1.xenial_amd64.deb"
@@ -1,3 +1,10 @@
1
+ 2020-04-01
2
+ =================
3
+ * Bump to 0.8.4.2
4
+ * Improve path detection feedback (#460)
5
+ * Fix typos (#444)
6
+ * Update readme (#439)
7
+
1
8
  2019-02-22
2
9
  =================
3
10
  * Bump to 0.8.4.1
data/README.md CHANGED
@@ -14,7 +14,7 @@ gem install pdfkit
14
14
 
15
15
  <https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF>
16
16
 
17
- 2. Try using the `wkhtmltopdf-binary` gem (mac + linux i386)
17
+ 2. Try using the `wkhtmltopdf-binary-edge` gem (mac + linux i386)
18
18
  ```
19
19
  gem install wkhtmltopdf-binary
20
20
  ```
@@ -34,7 +34,7 @@ pdf = kit.to_pdf
34
34
  file = kit.to_file('/path/to/save/pdf')
35
35
 
36
36
  # PDFKit.new can optionally accept a URL or a File.
37
- # Stylesheets can not be added when source is provided as a URL of File.
37
+ # Stylesheets can not be added when source is provided as a URL or File.
38
38
  kit = PDFKit.new('http://google.com')
39
39
  kit = PDFKit.new(File.new('/path/to/html'))
40
40
 
@@ -116,6 +116,13 @@ config.middleware.use PDFKit::Middleware, {}, :except => [%r[^/prawn], %r[^/secr
116
116
  # conditions can be strings (either one or an array)
117
117
  config.middleware.use PDFKit::Middleware, {}, :except => ['/secret']
118
118
  ```
119
+ **With conditions to force download**
120
+ ```ruby
121
+ # force download with attachment disposition
122
+ config.middleware.use PDFKit::Middleware, {}, :dispositon => 'attachment'
123
+ # conditions can force a filename
124
+ config.middleware.use PDFKit::Middleware, {}, :dispositon => 'attachment; filename=report.pdf'
125
+ ```
119
126
  **Saving the generated .pdf to disk**
120
127
 
121
128
  Setting the `PDFKit-save-pdf` header will cause PDFKit to write the generated .pdf to the file indicated by the value of the header.
@@ -125,7 +132,7 @@ For example:
125
132
  headers['PDFKit-save-pdf'] = 'path/to/saved.pdf'
126
133
  ```
127
134
 
128
- Will cause the .pdf to be saved to `path/to/saved.pdf` in addition to being sent back to the client. If the path is not writable/non-existant the write will fail silently. The `PDFKit-save-pdf` header is never sent back to the client.
135
+ Will cause the .pdf to be saved to `path/to/saved.pdf` in addition to being sent back to the client. If the path is not writable/non-existent the write will fail silently. The `PDFKit-save-pdf` header is never sent back to the client.
129
136
 
130
137
  ## Troubleshooting
131
138
 
@@ -140,13 +147,13 @@ Will cause the .pdf to be saved to `path/to/saved.pdf` in addition to being sent
140
147
  around this issue you may want to run a server with multiple workers
141
148
  like Passenger or try to embed your resources within your HTML to
142
149
  avoid extra HTTP requests.
143
-
150
+
144
151
  Example solution (rails / bundler), add unicorn to the development
145
152
  group in your Gemfile `gem 'unicorn'` then run `bundle`. Next, add a
146
153
  file `config/unicorn.conf` with
147
-
154
+
148
155
  worker_processes 3
149
-
156
+
150
157
  Then to run the app `unicorn_rails -c config/unicorn.conf` (from rails_root)
151
158
 
152
159
  * **Resources aren't included in the PDF:** Images, CSS, or JavaScript
@@ -1,7 +1,8 @@
1
1
  class PDFKit
2
2
  class Configuration
3
- attr_accessor :meta_tag_prefix, :default_options, :root_url
3
+ attr_accessor :meta_tag_prefix, :root_url
4
4
  attr_writer :use_xvfb, :verbose
5
+ attr_reader :default_options
5
6
 
6
7
  def initialize
7
8
  @verbose = false
@@ -24,14 +25,19 @@ class PDFKit
24
25
  end
25
26
 
26
27
  def default_wkhtmltopdf
27
- @default_command_path ||= (defined?(Bundler::GemfileError) && File.exists?('Gemfile') ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
28
+ return @default_command_path if @default_command_path
29
+ if defined?(Bundler::GemfileError) && File.exists?('Gemfile')
30
+ @default_command_path = `bundle exec which wkhtmltopdf`.chomp
31
+ end
32
+ @default_command_path = `which wkhtmltopdf`.chomp if @default_command_path.nil? || @default_command_path.empty?
33
+ @default_command_path
28
34
  end
29
35
 
30
36
  def wkhtmltopdf=(path)
31
37
  if File.exist?(path)
32
38
  @wkhtmltopdf = path
33
39
  else
34
- warn "No executable found at #{path}. Will fall back to #{default_wkhtmltopdf}" unless File.exist?(path)
40
+ warn "No executable found at #{path}. Will fall back to #{default_wkhtmltopdf}"
35
41
  @wkhtmltopdf = default_wkhtmltopdf
36
42
  end
37
43
  end
@@ -51,6 +57,10 @@ class PDFKit
51
57
  def verbose?
52
58
  @verbose
53
59
  end
60
+
61
+ def default_options=(options)
62
+ @default_options.merge!(options)
63
+ end
54
64
  end
55
65
 
56
66
  class << self
@@ -47,6 +47,7 @@ class PDFKit
47
47
 
48
48
  headers['Content-Length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
49
49
  headers['Content-Type'] = 'application/pdf'
50
+ headers['Content-Disposition'] = @conditions[:disposition] || 'inline'
50
51
  end
51
52
 
52
53
  [status, headers, response]
@@ -1,7 +1,9 @@
1
1
  require 'shellwords'
2
2
 
3
3
  class PDFKit
4
- class NoExecutableError < StandardError
4
+ class Error < StandardError; end
5
+
6
+ class NoExecutableError < Error
5
7
  def initialize
6
8
  msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n"
7
9
  msg << ">> Please install wkhtmltopdf - https://github.com/pdfkit/PDFKit/wiki/Installing-WKHTMLTOPDF"
@@ -9,12 +11,18 @@ class PDFKit
9
11
  end
10
12
  end
11
13
 
12
- class ImproperSourceError < StandardError
14
+ class ImproperSourceError < Error
13
15
  def initialize(msg)
14
16
  super("Improper Source: #{msg}")
15
17
  end
16
18
  end
17
19
 
20
+ class ImproperWkhtmltopdfExitStatus < Error
21
+ def initialize(invoke)
22
+ super("Command failed (exitstatus=#{$?.exitstatus}): #{invoke}")
23
+ end
24
+ end
25
+
18
26
  attr_accessor :source, :stylesheets
19
27
  attr_reader :renderer
20
28
 
@@ -31,7 +39,7 @@ class PDFKit
31
39
  @renderer = WkHTMLtoPDF.new options
32
40
  @renderer.normalize_options
33
41
 
34
- raise NoExecutableError.new unless File.exists?(PDFKit.configuration.wkhtmltopdf)
42
+ raise NoExecutableError unless File.exists?(PDFKit.configuration.wkhtmltopdf)
35
43
  end
36
44
 
37
45
  def command(path = nil)
@@ -70,7 +78,7 @@ class PDFKit
70
78
 
71
79
  # $? is thread safe per
72
80
  # http://stackoverflow.com/questions/2164887/thread-safe-external-process-in-ruby-plus-checking-exitstatus
73
- raise "command failed (exitstatus=#{$?.exitstatus}): #{invoke}" if empty_result?(path, result) or !successful?($?)
81
+ raise ImproperWkhtmltopdfExitStatus, invoke if empty_result?(path, result) || !successful?($?)
74
82
  return result
75
83
  end
76
84
 
@@ -83,7 +91,7 @@ class PDFKit
83
91
 
84
92
  def find_options_in_meta(content)
85
93
  # Read file if content is a File
86
- content = content.read if content.is_a?(File)
94
+ content = content.read if content.is_a?(File) || content.is_a?(Tempfile)
87
95
 
88
96
  found = {}
89
97
  content.scan(/<meta [^>]*>/) do |meta|
@@ -116,7 +124,7 @@ class PDFKit
116
124
  end
117
125
 
118
126
  def append_stylesheets
119
- raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?
127
+ raise ImproperSourceError, 'Stylesheets may only be added to an HTML source' if stylesheets.any? && !@source.html?
120
128
 
121
129
  stylesheets.each do |stylesheet|
122
130
  if @source.to_s.match(/<\/head>/)
@@ -13,7 +13,7 @@ class PDFKit
13
13
  end
14
14
 
15
15
  def file?
16
- @is_file ||= @source.kind_of?(File)
16
+ @is_file ||= @source.kind_of?(File) || @source.kind_of?(Tempfile)
17
17
  end
18
18
 
19
19
  def html?
@@ -1,3 +1,3 @@
1
1
  class PDFKit
2
- VERSION = '0.8.4.1'
2
+ VERSION = '0.8.4.2'
3
3
  end
@@ -13,8 +13,6 @@ Gem::Specification.new do |s|
13
13
  s.description = "Uses wkhtmltopdf to create PDFs using HTML"
14
14
  s.license = "MIT"
15
15
 
16
- s.rubyforge_project = "pdfkit"
17
-
18
16
  s.files = `git ls-files`.split("\n")
19
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -3,11 +3,48 @@ require 'spec_helper'
3
3
  describe PDFKit::Configuration do
4
4
  subject { PDFKit::Configuration.new }
5
5
  describe "#wkhtmltopdf" do
6
+ context "when explicitly configured" do
7
+ it "uses configured value and don't detect" do
8
+ expect(subject).not_to receive(:default_wkhtmltopdf)
9
+ subject.wkhtmltopdf = "./Gemfile" # Need a file which exists
10
+ expect(subject.wkhtmltopdf).to eq("./Gemfile")
11
+ end
12
+
13
+ it "falls back to detected binary if configured path doesn't exists" do
14
+ expect(subject).to receive(:default_wkhtmltopdf).twice.and_return("/bin/fallback")
15
+ expect(subject).to receive(:warn).with(/No executable found/)
16
+ subject.wkhtmltopdf = "./missing-file" # Need a file which doesn't exist
17
+ expect(subject.wkhtmltopdf).to eq("/bin/fallback")
18
+ end
19
+ end
20
+
6
21
  context "when not explicitly configured" do
7
- it "detects the existance of bundler" do
8
- # Test assumes bundler is installed in your test environment
9
- expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return('c:\windows\path.exe')
10
- subject.wkhtmltopdf
22
+ context "when running inside bundler" do
23
+ # Simulate the presence of bundler even if it's not here
24
+ before { stub_const("Bundler::GemfileError", Class) }
25
+
26
+ it "detects the existance of bundler" do
27
+ expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return("c:\\windows\\path.exe\n")
28
+ expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
29
+ end
30
+
31
+ it "falls back if bundler path fails" do
32
+ # This happens when there is a wrong (buggy) version of bundler for example
33
+ expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return("")
34
+ expect(subject).to receive(:`).with('which wkhtmltopdf').and_return("c:\\windows\\path.exe\n")
35
+ expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
36
+ end
37
+ end
38
+
39
+ context "when running without bundler" do
40
+ # Simulate the absence of bundler even if it's there
41
+ before { hide_const("Bundler::GemfileError") }
42
+
43
+ it "detects the existance of bundler" do
44
+ expect(subject).not_to receive(:`).with('bundle exec which wkhtmltopdf')
45
+ expect(subject).to receive(:`).with('which wkhtmltopdf').and_return('c:\windows\path.exe')
46
+ expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
47
+ end
11
48
  end
12
49
  end
13
50
  end
@@ -40,6 +77,13 @@ describe PDFKit::Configuration do
40
77
  expect(subject.default_options[:quiet]).to eql false
41
78
  expect(subject.default_options[:is_awesome]).to eql true
42
79
  end
80
+
81
+ it "merges additional options with existing defaults" do
82
+ subject.default_options = { quiet: false, is_awesome: true }
83
+ expect(subject.default_options[:quiet]).to eql false
84
+ expect(subject.default_options[:is_awesome]).to eql true
85
+ expect(subject.default_options[:disable_smart_shrinking]).to eql false
86
+ end
43
87
  end
44
88
 
45
89
  describe "#root_url" do
@@ -341,6 +341,44 @@ describe PDFKit::Middleware do
341
341
  end
342
342
  end
343
343
  end
344
+
345
+ describe ":disposition" do
346
+ describe "inline or blank" do
347
+ context "default" do
348
+ specify do
349
+ mock_app
350
+ get 'http://www.example.org/public/test.pdf'
351
+ expect(last_response.headers["Content-Disposition"]).to eq("inline")
352
+ end
353
+ end
354
+
355
+ context "inline" do
356
+ specify do
357
+ mock_app({}, { :disposition => 'inline' })
358
+ get 'http://www.example.org/public/test.pdf'
359
+ expect(last_response.headers["Content-Disposition"]).to eq("inline")
360
+ end
361
+ end
362
+ end
363
+
364
+ describe "attachment" do
365
+ context "attachment" do
366
+ specify do
367
+ mock_app({}, { :disposition => 'attachment' })
368
+ get 'http://www.example.org/public/test.pdf'
369
+ expect(last_response.headers["Content-Disposition"]).to eq("attachment")
370
+ end
371
+ end
372
+
373
+ context "attachment with filename" do
374
+ specify do
375
+ mock_app({}, { :disposition => 'attachment; filename=report.pdf' })
376
+ get 'http://www.example.org/public/test.pdf'
377
+ expect(last_response.headers["Content-Disposition"]).to eq("attachment; filename=report.pdf")
378
+ end
379
+ end
380
+ end
381
+ end
344
382
  end
345
383
 
346
384
  describe "remove .pdf from PATH_INFO and REQUEST_URI" do
@@ -23,6 +23,13 @@ describe PDFKit do
23
23
  expect(pdfkit.source.to_s).to eq(file_path)
24
24
  end
25
25
 
26
+ it "accepts a Tempfile as the source" do
27
+ file_path = File.join(SPEC_ROOT,'fixtures','example.html')
28
+ pdfkit = PDFKit.new(Tempfile.new(file_path))
29
+ expect(pdfkit.source).to be_file
30
+ expect(pdfkit.source.to_s).to match /^#{Dir.tmpdir}/
31
+ end
32
+
26
33
  # Options
27
34
  ## options keys
28
35
  it "drops options without values" do
@@ -252,6 +259,12 @@ describe PDFKit do
252
259
  expect(pdfkit.command).to match /#{file_path} -$/
253
260
  end
254
261
 
262
+ it "specifies the path to the source if it is a tempfile" do
263
+ file_path = File.join(SPEC_ROOT,'fixtures','example.html')
264
+ pdfkit = PDFKit.new(Tempfile.new(file_path))
265
+ expect(pdfkit.command).to match /#{Dir.tmpdir}\S+ -$/
266
+ end
267
+
255
268
  it "specifies the path for the ouput if a path is given" do
256
269
  file_path = "/path/to/output.pdf"
257
270
  pdfkit = PDFKit.new("html")
@@ -499,7 +512,7 @@ describe PDFKit do
499
512
  #NOTICE: This test is failed if use wkhtmltopdf-binary (0.9.9.1)
500
513
  it "throws an error if it is unable to connect" do
501
514
  pdfkit = PDFKit.new("http://google.com/this-should-not-be-found/404.html")
502
- expect { pdfkit.to_pdf }.to raise_error /exitstatus=1/
515
+ expect { pdfkit.to_pdf }.to raise_error PDFKit::ImproperWkhtmltopdfExitStatus, /exitstatus=1/
503
516
  end
504
517
 
505
518
  it "does not throw an error if it is unable to connect", pending: 'this test works for wkhtmltopdf-binary (0.9.9.1)' do
@@ -12,6 +12,11 @@ describe PDFKit::Source do
12
12
  expect(source).not_to be_url
13
13
  end
14
14
 
15
+ it "returns false if passed a tempfile" do
16
+ source = PDFKit::Source.new(::Tempfile.new(__FILE__))
17
+ expect(source).not_to be_url
18
+ end
19
+
15
20
  it "returns false if passed HTML" do
16
21
  source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
17
22
  expect(source).not_to be_url
@@ -29,6 +34,11 @@ describe PDFKit::Source do
29
34
  expect(source).to be_file
30
35
  end
31
36
 
37
+ it "returns true if passed a tempfile" do
38
+ source = PDFKit::Source.new(::Tempfile.new(__FILE__))
39
+ expect(source).to be_file
40
+ end
41
+
32
42
  it "returns false if passed a url like string" do
33
43
  source = PDFKit::Source.new('http://google.com')
34
44
  expect(source).not_to be_file
@@ -51,6 +61,11 @@ describe PDFKit::Source do
51
61
  expect(source).not_to be_html
52
62
  end
53
63
 
64
+ it "returns false if passed a tempfile" do
65
+ source = PDFKit::Source.new(::Tempfile.new(__FILE__))
66
+ expect(source).not_to be_html
67
+ end
68
+
54
69
  it "returns false if passed a url like string" do
55
70
  source = PDFKit::Source.new('http://google.com')
56
71
  expect(source).not_to be_html
@@ -77,6 +92,11 @@ describe PDFKit::Source do
77
92
  source = PDFKit::Source.new(::File.new(__FILE__))
78
93
  expect(source.to_input_for_command).to match 'spec/source_spec.rb'
79
94
  end
95
+
96
+ it "returns the file path for tempfile sources" do
97
+ source = PDFKit::Source.new(file = ::Tempfile.new(__FILE__))
98
+ expect(source.to_input_for_command).to match file.path
99
+ end
80
100
  end
81
101
 
82
102
  describe "#to_s" do
@@ -90,6 +110,11 @@ describe PDFKit::Source do
90
110
  expect(source.to_s).to eq(__FILE__)
91
111
  end
92
112
 
113
+ it "returns a path if passed a tempfile" do
114
+ source = PDFKit::Source.new(file = ::Tempfile.new(__FILE__))
115
+ expect(source.to_s).to eq(file.path)
116
+ end
117
+
93
118
  it "returns the url if passed a url like string" do
94
119
  source = PDFKit::Source.new('http://google.com')
95
120
  expect(source.to_s).to eq('http://google.com')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdfkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4.1
4
+ version: 0.8.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared Pace
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-02-22 00:00:00.000000000 Z
12
+ date: 2020-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -103,6 +103,7 @@ extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
105
  - ".document"
106
+ - ".github/workflows/stale.yml"
106
107
  - ".gitignore"
107
108
  - ".rspec"
108
109
  - ".ruby-gemset"
@@ -154,8 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  version: '0'
155
156
  requirements:
156
157
  - wkhtmltopdf
157
- rubyforge_project: pdfkit
158
- rubygems_version: 2.7.6
158
+ rubygems_version: 3.0.3
159
159
  signing_key:
160
160
  specification_version: 4
161
161
  summary: HTML+CSS -> PDF