forked from pdfkit/pdfkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge https://github.com/rymai/PDFKit into rymai-master
Conflicts: lib/pdfkit/configuration.rb lib/pdfkit/middleware.rb spec/middleware_spec.rb
- Loading branch information
Showing
10 changed files
with
215 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ pkg | |
|
||
## PROJECT::SPECIFIC | ||
.bundle | ||
spec/custom_wkhtmltopdf_path.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ group :development do | |
gem "mocha" | ||
gem "jeweler" | ||
gem "rack" | ||
gem "rack-test" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,77 @@ | ||
class PDFKit | ||
|
||
class Middleware | ||
|
||
def initialize(app, options = {}) | ||
@app = app | ||
@options = options | ||
|
||
def initialize(app, options = {}, conditions = {}) | ||
@app = app | ||
@options = options | ||
@conditions = conditions | ||
end | ||
|
||
def call(env) | ||
@render_pdf = false | ||
set_request_to_render_as_pdf(env) if env['PATH_INFO'].match(/\.pdf$/) | ||
|
||
@request = Rack::Request.new(env) | ||
|
||
set_request_to_render_as_pdf(env) if render_as_pdf? | ||
status, headers, response = @app.call(env) | ||
|
||
request = Rack::Request.new(env) | ||
if @render_pdf && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/ | ||
body = response.body | ||
|
||
body = translate_paths(body, env) | ||
|
||
pdf = PDFKit.new(body, @options) | ||
body = pdf.to_pdf | ||
|
||
|
||
if rendering_pdf? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/ | ||
body = response.respond_to?(:body) ? response.body : response.join | ||
body = PDFKit.new(translate_paths(body, env), @options).to_pdf | ||
response = [body] | ||
|
||
# Do not cache PDFs | ||
headers.delete('ETag') | ||
headers.delete('Cache-Control') | ||
|
||
headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s | ||
headers["Content-Type"] = "application/pdf" | ||
headers.store("Middleware", "PDFKit") | ||
|
||
response = [body] | ||
|
||
headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s | ||
headers["Content-Type"] = "application/pdf" | ||
headers["Rack-Middleware-PDFKit"] = "true" | ||
end | ||
|
||
[status, headers, response] | ||
end | ||
|
||
private | ||
|
||
# Change relative paths to absolute | ||
def translate_paths(body, env) | ||
# Host with protocol | ||
root = env['rack.url_scheme'] + "://" + env['HTTP_HOST'] + "/" | ||
|
||
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/,'\1=\2'+root+'\3\2') | ||
end | ||
|
||
def set_request_to_render_as_pdf(env) | ||
@render_pdf = true | ||
|
||
path = Pathname(env['PATH_INFO']) | ||
['PATH_INFO','REQUEST_URI'].each { |e| env[e] = path.to_s.sub(/#{path.extname}$/,'') } if path.extname == '.pdf' | ||
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html')) | ||
env['MIDDLEWARE'] = "PDFKit" | ||
end | ||
|
||
def concat(accepts, type) | ||
(accepts || '').split(',').unshift(type).compact.join(',') | ||
|
||
# Change relative paths to absolute | ||
def translate_paths(body, env) | ||
# Host with protocol | ||
root = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/" | ||
|
||
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2') | ||
end | ||
|
||
def rendering_pdf? | ||
@render_pdf | ||
end | ||
|
||
def render_as_pdf? | ||
request_path_is_pdf = @request.path.match(%r{\.pdf$}) | ||
|
||
if request_path_is_pdf && @conditions[:only] | ||
rules = [@conditions[:only]].flatten | ||
rules.any? do |pattern| | ||
if pattern.is_a?(Regexp) | ||
@request.path =~ pattern | ||
else | ||
@request.path[0, pattern.length] == pattern | ||
end | ||
end | ||
else | ||
request_path_is_pdf | ||
end | ||
|
||
end | ||
|
||
def set_request_to_render_as_pdf(env) | ||
@render_pdf = true | ||
path = @request.path.sub(%r{\.pdf$}, '') | ||
%w[PATH_INFO REQUEST_URI].each { |e| env[e] = path } | ||
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html')) | ||
end | ||
|
||
def concat(accepts, type) | ||
(accepts || '').split(',').unshift(type).compact.join(',') | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.