pdfkit 0.1.1 → 0.2.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
@@ -5,8 +5,8 @@ Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antia
5
5
  ## Installation
6
6
 
7
7
  1. Install wkhtmltopdf
8
- ## Download the latest binary from http://code.google.com/p/wkhtmltopdf/downloads/list
9
- ## Place the binary somewhere on your path (e.g /usr/local/bin)
8
+ ** Download the latest binary from http://code.google.com/p/wkhtmltopdf/downloads/list
9
+ ** Place the binary somewhere on your path (e.g /usr/local/bin)
10
10
  2. Install PDFKit
11
11
 
12
12
  $ gem install pdfkit
@@ -26,7 +26,7 @@ Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antia
26
26
 
27
27
  ## Middleware
28
28
 
29
- PDFKit comes with a middleware that allows users to visit any to get a PDF view of any page on your site by appending ?pdf=true to the URL.
29
+ PDFKit comes with a middleware that allows users to visit any to get a PDF view of any page on your site by appending .pdf to the URL.
30
30
 
31
31
  ### Middleware Setup
32
32
 
@@ -41,6 +41,12 @@ PDFKit comes with a middleware that allows users to visit any to get a PDF view
41
41
  # in application.rb(Rails3) or environment.rb(Rails2)
42
42
  require 'pdfkit'
43
43
  config.middleware.use PDFKit::Middleware
44
+
45
+ **With PDFKit options**
46
+
47
+ # options will be passed to PDFKit.new
48
+ config.middleware.use PDFKit::Middleware, :print_media_type => true
49
+
44
50
 
45
51
  ## Note on Patches/Pull Requests
46
52
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -3,39 +3,67 @@ class PDFKit
3
3
  # A rack middleware for validating HTML via w3c validator
4
4
  class Middleware
5
5
 
6
- def initialize( app )
6
+ def initialize(app, options = {})
7
7
  @app = app
8
+ @options = options
8
9
  end
9
10
 
10
- def call( env )
11
+ def call(env)
12
+ @render_pdf = false
13
+ set_request_to_render_as_pdf(env) if env['PATH_INFO'].match(/\.pdf$/)
14
+
11
15
  status, headers, response = @app.call( env )
12
16
 
13
17
  request = Rack::Request.new( env )
14
- if !request.params['pdf'].blank?
15
- if headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
16
- body = response.body
17
-
18
- # Make absolute urls
19
- uri = env['REQUEST_URI'].split('?').first
20
- uri += '/' unless uri.match(/\/$/)
21
- root = env['rack.url_scheme'] + "://" + env['HTTP_HOST']
22
- # translate relative urls
23
- body.gsub!(/(href|src)=['"]([^\/][^\"']*)['"]/,'\1="'+root+'/\2"')
24
-
25
- # translate absolute urls
26
- body.gsub!(/(href|src)=['"]\/([^\"]*|[^']*)['"]/,'\1="'+uri+'\2"')
27
-
28
- pdf = PDFKit.new(body)
29
- body = pdf.to_pdf
30
-
31
- headers["Content-Length"] = body.length.to_s
32
- headers["Content-Type"] = "application/pdf"
33
- response = [body]
34
- end
18
+ if @render_pdf && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
19
+ body = response.body
20
+
21
+ body = translate_paths(body, env)
22
+
23
+ pdf = PDFKit.new(body, @options)
24
+ body = pdf.to_pdf
25
+
26
+ # Do not cache PDFs
27
+ puts "DELETING CACHING"
28
+ headers.delete('ETag')
29
+ headers.delete('Cache-Control')
30
+
31
+ headers["Content-Length"] = body.length.to_s
32
+ headers["Content-Type"] = "application/pdf"
33
+
34
+ response = [body]
35
35
  end
36
36
 
37
37
  [status, headers, response]
38
38
  end
39
+
40
+ private
41
+
42
+ def translate_paths(body, env)
43
+ # Make absolute urls
44
+ uri = env['REQUEST_URI'].split('?').first
45
+ uri += '/' unless uri.match(/\/$/)
46
+ root = env['rack.url_scheme'] + "://" + env['HTTP_HOST']
47
+
48
+ # translate relative urls
49
+ body.gsub!(/(href|src)=['"]([^\/][^\"']*)['"]/,'\1="'+root+'/\2"')
50
+
51
+ # translate absolute urls
52
+ body.gsub!(/(href|src)=['"]\/([^\"]*|[^']*)['"]/,'\1="'+uri+'\2"')
53
+ end
54
+
55
+ def set_request_to_render_as_pdf(env)
56
+ @render_pdf = true
57
+ puts "Setting PDF mode"
58
+
59
+ path = Pathname(env['PATH_INFO'])
60
+ env['PATH_INFO'] = path.to_s.sub(/#{path.extname}$/,'') if path.extname == '.pdf'
61
+ env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('html'))
62
+ end
63
+
64
+ def concat(accepts, type)
65
+ (accepts || '').split(',').unshift(type).compact.join(',')
66
+ end
39
67
 
40
68
  end
41
69
  end
data/pdfkit.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pdfkit}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.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"]
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - jdpace