pdfkit 0.2.3 → 0.3.0

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.

data/README.md CHANGED
@@ -25,7 +25,12 @@ Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antia
25
25
  pdf = kit.to_pdf
26
26
 
27
27
  # Save the PDF to a file
28
- File.open('/path/to/save/pdf','w') {|file| file << pdf}
28
+ file = kit.to_file('/path/to/save/pdf')
29
+
30
+ # PDFKit.new can optionally accept a URL or a File.
31
+ # Stylesheets can not be added when source is provided as a URL of File.
32
+ kit = PDFKit.new('http://google.com')
33
+ kit = PDFKit.new(File.new('/path/to/html'))
29
34
 
30
35
  ## Middleware
31
36
 
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ begin
12
12
  gem.authors = ["jdpace"]
13
13
  gem.add_dependency "activesupport"
14
14
  gem.add_development_dependency "rspec", "~> 2.0.0.beta.8"
15
+ gem.add_development_dependency 'mocha'
15
16
  gem.files = [
16
17
  ".document",
17
18
  ".gitignore",
@@ -19,12 +20,19 @@ begin
19
20
  "README.md",
20
21
  "Rakefile",
21
22
  "VERSION",
23
+ "bin/pdfkit.rb",
24
+ "bin/wkhtmltopdf-linux-i386-0-9-9",
25
+ "bin/wkhtmltopdf-osx-i386-0-9-9",
26
+ "bin/wkhtmltopdf-proxy",
22
27
  "lib/pdfkit.rb",
23
28
  "lib/pdfkit/middleware.rb",
24
29
  "lib/pdfkit/pdfkit.rb",
30
+ "lib/pdfkit/source.rb",
25
31
  "pdfkit.gemspec",
26
32
  "spec/pdfkit_spec.rb",
33
+ "spec/source_spec.rb",
27
34
  "spec/fixtures/example.css",
35
+ "spec/fixtures/example.html",
28
36
  "spec/spec.opts",
29
37
  "spec/spec_helper.rb"
30
38
  ]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.3.0
data/lib/pdfkit/pdfkit.rb CHANGED
@@ -6,11 +6,18 @@ class PDFKit
6
6
  end
7
7
  end
8
8
 
9
- attr_accessor :html, :stylesheets
9
+ class ImproperSourceError < StandardError
10
+ def initialize(msg)
11
+ super("Improper Source: #{msg}")
12
+ end
13
+ end
14
+
15
+ attr_accessor :source, :stylesheets
10
16
  attr_reader :options
11
17
 
12
- def initialize(html, options = {})
13
- @html = html
18
+ def initialize(url_file_or_html, options = {})
19
+ @source = Source.new(url_file_or_html)
20
+
14
21
  @stylesheets = []
15
22
 
16
23
  default_options = {
@@ -31,7 +38,13 @@ class PDFKit
31
38
  args = [@cmd]
32
39
  args += @options.to_a.flatten.compact
33
40
  args << '--quiet'
34
- args << '-' # Get HTML from stdin
41
+
42
+ if @source.html?
43
+ args << '-' # Get HTML from stdin
44
+ else
45
+ args << @source.to_s
46
+ end
47
+
35
48
  args << '-' # Read PDF from stdout
36
49
  args.join(' ')
37
50
  end
@@ -40,13 +53,17 @@ class PDFKit
40
53
  append_stylesheets
41
54
 
42
55
  pdf = IO.popen(command, "w+")
43
- pdf.puts(@html)
56
+ pdf.puts(@source.to_s) if @source.html?
44
57
  pdf.close_write
45
58
  result = pdf.gets(nil)
46
59
  pdf.close_read
47
60
  return result
48
61
  end
49
62
 
63
+ def to_file(path)
64
+ File.open(path,'w') {|file| file << self.to_pdf}
65
+ end
66
+
50
67
  protected
51
68
 
52
69
  def style_tag_for(stylesheet)
@@ -54,11 +71,13 @@ class PDFKit
54
71
  end
55
72
 
56
73
  def append_stylesheets
74
+ raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?
75
+
57
76
  stylesheets.each do |stylesheet|
58
- if @html.match(/<\/head>/)
59
- @html.gsub!(/(<\/head>)/, style_tag_for(stylesheet)+'\1')
77
+ if @source.to_s.match(/<\/head>/)
78
+ @source.to_s.gsub!(/(<\/head>)/, style_tag_for(stylesheet)+'\1')
60
79
  else
61
- @html.insert(0, style_tag_for(stylesheet))
80
+ @source.to_s.insert(0, style_tag_for(stylesheet))
62
81
  end
63
82
  end
64
83
  end
@@ -66,6 +85,7 @@ class PDFKit
66
85
  def normalize_options(options)
67
86
  normalized_options = {}
68
87
  options.each do |key, value|
88
+ next if !value
69
89
  normalized_key = "--#{key.to_s.downcase.dasherize}"
70
90
  normalized_value = value.is_a?(TrueClass) ? nil : value
71
91
  normalized_options[normalized_key] = normalized_value
@@ -0,0 +1,27 @@
1
+ class PDFKit
2
+
3
+ class Source
4
+
5
+ def initialize(url_file_or_html)
6
+ @source = url_file_or_html
7
+ end
8
+
9
+ def url?
10
+ @source.is_a?(String) && @source.match(/^http/)
11
+ end
12
+
13
+ def file?
14
+ @source.kind_of?(File)
15
+ end
16
+
17
+ def html?
18
+ !(url? || file?)
19
+ end
20
+
21
+ def to_s
22
+ file? ? @source.path : @source
23
+ end
24
+
25
+ end
26
+
27
+ end
data/lib/pdfkit.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'active_support/core_ext'
3
3
  require 'active_support/inflector'
4
+ require 'pdfkit/source'
4
5
  require 'pdfkit/pdfkit'
5
6
  require 'pdfkit/middleware'
data/pdfkit.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pdfkit}
8
- s.version = "0.2.3"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jdpace"]
12
- s.date = %q{2010-06-01}
12
+ s.date = %q{2010-06-11}
13
13
  s.description = %q{Uses wkhtmltopdf to create PDFs using HTML}
14
14
  s.email = %q{jared@codewordstudios.com}
15
15
  s.executables = ["pdfkit.rb", "wkhtmltopdf-linux-i386-0-9-9", "wkhtmltopdf-osx-i386-0-9-9", "wkhtmltopdf-proxy"]
@@ -24,12 +24,19 @@ Gem::Specification.new do |s|
24
24
  "README.md",
25
25
  "Rakefile",
26
26
  "VERSION",
27
+ "bin/pdfkit.rb",
28
+ "bin/wkhtmltopdf-linux-i386-0-9-9",
29
+ "bin/wkhtmltopdf-osx-i386-0-9-9",
30
+ "bin/wkhtmltopdf-proxy",
27
31
  "lib/pdfkit.rb",
28
32
  "lib/pdfkit/middleware.rb",
29
33
  "lib/pdfkit/pdfkit.rb",
34
+ "lib/pdfkit/source.rb",
30
35
  "pdfkit.gemspec",
31
36
  "spec/fixtures/example.css",
37
+ "spec/fixtures/example.html",
32
38
  "spec/pdfkit_spec.rb",
39
+ "spec/source_spec.rb",
33
40
  "spec/spec.opts",
34
41
  "spec/spec_helper.rb"
35
42
  ]
@@ -40,6 +47,7 @@ Gem::Specification.new do |s|
40
47
  s.summary = %q{HTML+CSS -> PDF}
41
48
  s.test_files = [
42
49
  "spec/pdfkit_spec.rb",
50
+ "spec/source_spec.rb",
43
51
  "spec/spec_helper.rb"
44
52
  ]
45
53
 
@@ -50,13 +58,16 @@ Gem::Specification.new do |s|
50
58
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
59
  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
52
60
  s.add_development_dependency(%q<rspec>, ["~> 2.0.0.beta.8"])
61
+ s.add_development_dependency(%q<mocha>, [">= 0"])
53
62
  else
54
63
  s.add_dependency(%q<activesupport>, [">= 0"])
55
64
  s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.8"])
65
+ s.add_dependency(%q<mocha>, [">= 0"])
56
66
  end
57
67
  else
58
68
  s.add_dependency(%q<activesupport>, [">= 0"])
59
69
  s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.8"])
70
+ s.add_dependency(%q<mocha>, [">= 0"])
60
71
  end
61
72
  end
62
73
 
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ <h1>Oh Hai!</h1>
4
+ </body>
5
+ </html>
data/spec/pdfkit_spec.rb CHANGED
@@ -3,9 +3,23 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe PDFKit do
4
4
 
5
5
  context "initialization" do
6
- it "should take HTML for the renderer" do
6
+ it "should accept HTML as the source" do
7
7
  pdfkit = PDFKit.new('<h1>Oh Hai</h1>')
8
- pdfkit.html.should == '<h1>Oh Hai</h1>'
8
+ pdfkit.source.should be_html
9
+ pdfkit.source.to_s.should == '<h1>Oh Hai</h1>'
10
+ end
11
+
12
+ it "should accept a URL as the source" do
13
+ pdfkit = PDFKit.new('http://google.com')
14
+ pdfkit.source.should be_url
15
+ pdfkit.source.to_s.should == 'http://google.com'
16
+ end
17
+
18
+ it "should accept a File as the source" do
19
+ file_path = File.join(SPEC_ROOT,'fixtures','example.html')
20
+ pdfkit = PDFKit.new(File.new(file_path))
21
+ pdfkit.source.should be_file
22
+ pdfkit.source.to_s.should == file_path
9
23
  end
10
24
 
11
25
  it "should parse the options into a cmd line friedly format" do
@@ -32,9 +46,25 @@ describe PDFKit do
32
46
  pdfkit.command.should include('wkhtmltopdf')
33
47
  pdfkit.command.should include('--page-size Letter')
34
48
  end
49
+
50
+ it "read the source from stdin if it is html" do
51
+ pdfkit = PDFKit.new('html')
52
+ pdfkit.command.should match(/ - -$/)
53
+ end
54
+
55
+ it "specify the URL to the source if it is a url" do
56
+ pdfkit = PDFKit.new('http://google.com')
57
+ pdfkit.command.should match(/ http:\/\/google\.com -$/)
58
+ end
59
+
60
+ it "should specify the path to the source if it is a file" do
61
+ file_path = File.join(SPEC_ROOT,'fixtures','example.html')
62
+ pdfkit = PDFKit.new(File.new(file_path))
63
+ pdfkit.command.should match(/ #{file_path} -$/)
64
+ end
35
65
  end
36
66
 
37
- context "to_pdf" do
67
+ context "#to_pdf" do
38
68
  it "should generate a PDF of the HTML" do
39
69
  pdfkit = PDFKit.new('html', :page_size => 'Letter')
40
70
  pdf = pdfkit.to_pdf
@@ -43,18 +73,44 @@ describe PDFKit do
43
73
 
44
74
  it "should have the stylesheet added to the head if it has one" do
45
75
  pdfkit = PDFKit.new("<html><head></head><body>Hai!</body></html>")
46
- css = File.expand_path(File.dirname(__FILE__) + '/fixtures/example.css')
76
+ css = File.join(SPEC_ROOT,'fixtures','example.css')
47
77
  pdfkit.stylesheets << css
48
78
  pdfkit.to_pdf
49
- pdfkit.html.should include("<style>#{File.read(css)}</style>")
79
+ pdfkit.source.to_s.should include("<style>#{File.read(css)}</style>")
50
80
  end
51
81
 
52
82
  it "should prepend style tags if the HTML doesn't have a head tag" do
53
83
  pdfkit = PDFKit.new("<html><body>Hai!</body></html>")
54
- css = File.expand_path(File.dirname(__FILE__) + '/fixtures/example.css')
84
+ css = File.join(SPEC_ROOT,'fixtures','example.css')
55
85
  pdfkit.stylesheets << css
56
86
  pdfkit.to_pdf
57
- pdfkit.html.should include("<style>#{File.read(css)}</style><html>")
87
+ pdfkit.source.to_s.should include("<style>#{File.read(css)}</style><html>")
88
+ end
89
+
90
+ it "should throw an error if the source is not html and stylesheets have been added" do
91
+ pdfkit = PDFKit.new('http://google.com')
92
+ css = File.join(SPEC_ROOT,'fixtures','example.css')
93
+ pdfkit.stylesheets << css
94
+ lambda { pdfkit.to_pdf }.should raise_error(PDFKit::ImproperSourceError)
95
+ end
96
+ end
97
+
98
+ context "#to_file" do
99
+ before do
100
+ @file_path = File.join(SPEC_ROOT,'fixtures','test.pdf')
101
+ File.delete(@file_path) if File.exist?(@file_path)
102
+ end
103
+
104
+ after do
105
+ File.delete(@file_path)
106
+ end
107
+
108
+ it "should create a file with the PDF as content" do
109
+ pdfkit = PDFKit.new('html', :page_size => 'Letter')
110
+ pdfkit.expects(:to_pdf).returns('PDF')
111
+ file = pdfkit.to_file(@file_path)
112
+ file.should be_instance_of(File)
113
+ File.read(file.path).should == 'PDF'
58
114
  end
59
115
  end
60
116
 
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe PDFKit::Source do
4
+
5
+ describe "#url?" do
6
+ it "should return true if passed a url like string" do
7
+ source = PDFKit::Source.new('http://google.com')
8
+ source.should be_url
9
+ end
10
+
11
+ it "should return false if passed a file" do
12
+ source = PDFKit::Source.new(File.new(__FILE__))
13
+ source.should_not be_url
14
+ end
15
+
16
+ it "should return false if passed HTML" do
17
+ source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
18
+ source.should_not be_url
19
+ end
20
+ end
21
+
22
+ describe "#file?" do
23
+ it "should return true if passed a file" do
24
+ source = PDFKit::Source.new(File.new(__FILE__))
25
+ source.should be_file
26
+ end
27
+
28
+ it "should return false if passed a url like string" do
29
+ source = PDFKit::Source.new('http://google.com')
30
+ source.should_not be_file
31
+ end
32
+
33
+ it "should return false if passed HTML" do
34
+ source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
35
+ source.should_not be_file
36
+ end
37
+ end
38
+
39
+ describe "#html?" do
40
+ it "should return true if passed HTML" do
41
+ source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
42
+ source.should be_html
43
+ end
44
+
45
+ it "should return false if passed a file" do
46
+ source = PDFKit::Source.new(File.new(__FILE__))
47
+ source.should_not be_html
48
+ end
49
+
50
+ it "should return false if passed a url like string" do
51
+ source = PDFKit::Source.new('http://google.com')
52
+ source.should_not be_html
53
+ end
54
+ end
55
+
56
+ describe "#to_s" do
57
+ it "should return the HTML if passed HTML" do
58
+ source = PDFKit::Source.new('<blink>Oh Hai!</blink>')
59
+ source.to_s.should == '<blink>Oh Hai!</blink>'
60
+ end
61
+
62
+ it "should return a path if passed a file" do
63
+ source = PDFKit::Source.new(File.new(__FILE__))
64
+ source.to_s.should == __FILE__
65
+ end
66
+
67
+ it "should return the url if passed a url like string" do
68
+ source = PDFKit::Source.new('http://google.com')
69
+ source.to_s.should == 'http://google.com'
70
+ end
71
+ end
72
+
73
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,10 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
1
+ SPEC_ROOT = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift(SPEC_ROOT)
3
+ $LOAD_PATH.unshift(File.join(SPEC_ROOT, '..', 'lib'))
3
4
  require 'pdfkit'
4
5
  require 'spec'
5
6
  require 'spec/autorun'
7
+ require 'mocha'
6
8
 
7
9
  Spec::Runner.configure do |config|
8
10
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
7
  - 3
9
- version: 0.2.3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - jdpace
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-01 00:00:00 -04:00
17
+ date: 2010-06-11 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,18 @@ dependencies:
45
45
  version: 2.0.0.beta.8
46
46
  type: :development
47
47
  version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: mocha
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id003
48
60
  description: Uses wkhtmltopdf to create PDFs using HTML
49
61
  email: jared@codewordstudios.com
50
62
  executables:
@@ -64,12 +76,19 @@ files:
64
76
  - README.md
65
77
  - Rakefile
66
78
  - VERSION
79
+ - bin/pdfkit.rb
80
+ - bin/wkhtmltopdf-linux-i386-0-9-9
81
+ - bin/wkhtmltopdf-osx-i386-0-9-9
82
+ - bin/wkhtmltopdf-proxy
67
83
  - lib/pdfkit.rb
68
84
  - lib/pdfkit/middleware.rb
69
85
  - lib/pdfkit/pdfkit.rb
86
+ - lib/pdfkit/source.rb
70
87
  - pdfkit.gemspec
71
88
  - spec/fixtures/example.css
89
+ - spec/fixtures/example.html
72
90
  - spec/pdfkit_spec.rb
91
+ - spec/source_spec.rb
73
92
  - spec/spec.opts
74
93
  - spec/spec_helper.rb
75
94
  has_rdoc: true
@@ -104,4 +123,5 @@ specification_version: 3
104
123
  summary: HTML+CSS -> PDF
105
124
  test_files:
106
125
  - spec/pdfkit_spec.rb
126
+ - spec/source_spec.rb
107
127
  - spec/spec_helper.rb