a phantomjs based pdf renderer
Creates PDFs from URLs using phantomjs
Read our blogpost about how it works.
Add this line to your application's Gemfile:
gem 'shrimp'And then execute:
$ bundleOr install it yourself as:
$ gem install shrimpSee http://phantomjs.org/download.html on how to install phantomjsrequire 'shrimp'
url = 'http://www.google.com'
options = { :margin => "1cm"}
Shrimp::Phantom.new(url, options).to_pdf("~/output.pdf")…{
"diskCache": false,
"ignoreSslErrors": false,
"loadImages": true,
"outputEncoding": "utf8",
"webSecurity": true
}Shrimp comes with a middleware that allows users to get a PDF view of any page on your site by appending .pdf to the URL.
Non-Rails Rack apps
# in config.ru
require 'shrimp'
use Shrimp::MiddlewareRails apps
# in application.rb(Rails3) or environment.rb(Rails2)
require 'shrimp'
config.middleware.use Shrimp::MiddlewareWith Shrimp options
# options will be passed to Shrimp::Phantom.new
config.middleware.use Shrimp::Middleware, :margin => '0.5cm', :format => 'Letter'With conditions to limit routes that can be generated in pdf
# conditions can be regexps (either one or an array)
config.middleware.use Shrimp::Middleware, {}, :only => %r[^/public]
config.middleware.use Shrimp::Middleware, {}, :only => [%r[^/invoice], %r[^/public]]
# conditions can be strings (either one or an array)
config.middleware.use Shrimp::Middleware, {}, :only => '/public'
config.middleware.use Shrimp::Middleware, {}, :only => ['/invoice', '/public']
# conditions can be regexps (either one or an array)
config.middleware.use Shrimp::Middleware, {}, :except => [%r[^/prawn], %r[^/secret]]
# conditions can be strings (either one or an array)
config.middleware.use Shrimp::Middleware, {}, :except => ['/secret']To avoid deadlocks Shrimp::Middleware renders the pdf in a separate process retuning a 503 Retry-After response Header. you can setup the polling interval and the polling offset in seconds.
config.middleware.use Shrimp::Middleware, :polling_interval => 1, :polling_offset => 5To avoid rendering the page on each request you can setup some the cache ttl in seconds
config.middleware.use Shrimp::Middleware, :cache_ttl => 3600, :out_path => "my/pdf/store"To include some fancy Ajax stuff with jquery
var url = '/my_page.pdf'
var statusCodes = {
200: function() {
return window.location.assign(url);
},
504: function() {
console.log("Shit's being wired")
},
503: function(jqXHR, textStatus, errorThrown) {
var wait;
wait = parseInt(jqXHR.getResponseHeader('Retry-After'));
return setTimeout(function() {
return $.ajax({
url: url,
statusCode: statusCodes
});
}, wait * 1000);
}
}
$.ajax({
url: url,
statusCode: statusCodes
})
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)Shrimp is Copyright © 2012 adeven (Manuel Kniep). It is free software, and may be redistributed under the terms specified in the LICENSE file.
No open issues yet, or sync has not completed.