saucerly 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ pkg
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tim Riley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ Saucerly
2
+ ========
3
+
4
+ Saucerly provides PDF rendering for your Rails app using the Java-based [FlyingSaucer](https://xhtmlrenderer.dev.java.net/) library.
5
+
6
+ It is based on [Princely](http://github.com/mbleigh/princely/). The benefit of Saucerly is that it provides competent XHTML to PDF rendering without the $4k PrinceXML pricetag.
7
+
8
+ Example
9
+ -------
10
+
11
+ Rendering from a template:
12
+
13
+ class ExamplesController < ApplicationController::Base
14
+ def show
15
+ @document = Document.find(params[:id])
16
+
17
+ respond_to do |format|
18
+ format.html
19
+ format.pdf { render :pdf => 'file_name', :template => 'controller/action.pdf.haml', :layout => 'pdf' }
20
+ end
21
+ end
22
+ end
23
+
24
+ Rendering from an inline string:
25
+
26
+ render :pdf => 'file_name', :inline => 'XHTML goes here'
27
+
28
+ Installation
29
+ ------------
30
+
31
+ First, set up the dependencies:
32
+
33
+ 1. Install [JRuby](http://jruby.org/)
34
+ 2. Register the flying_saucer gem dependency: add `config.gem 'flying_saucer'` to `config/environment.rb`
35
+ 3. Install flying_saucer: `jruby -S rake gems:install`
36
+
37
+ Then, install Saucerly:
38
+
39
+ * As a gem, add `config.gem 'timriley-saucerly', :source => 'http://gems.github.com/', :lib => 'saucerly'` to `config/environment.rb` and run `jruby -S rake gems:install`
40
+ * As a plugin, run `jruby script/plugin install git://github.com/timriley/saucerly`
41
+
42
+ Now you're ready to go! Add some code to your controllers like the examples above.
43
+
44
+ If you're developing on OS X and you don't want a Java icon to appear in your dock, put `java.lang.System.set_property("java.awt.headless", "true")` in `environment.rb` or an initializer
45
+
46
+ Copyright (c) 2009 Tim Riley & RentMonkey Pty Ltd, released under the MIT license
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "saucerly"
9
+ gemspec.summary = "PDF rending plugin for Rails using FlyingSaucer."
10
+ gemspec.email = "tim@openmonkey.com"
11
+ gemspec.homepage = "http://github.com/timriley/saucerly"
12
+ gemspec.authors = ["Tim Riley"]
13
+
14
+ gemspec.add_dependency('flying_saucer')
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ desc 'Default: run unit tests.'
21
+ task :default => :test
22
+
23
+ desc 'Test the saucerly plugin.'
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = true
29
+ end
30
+
31
+ desc 'Generate documentation for the saucerly plugin.'
32
+ Rake::RDocTask.new(:rdoc) do |rdoc|
33
+ rdoc.rdoc_dir = 'rdoc'
34
+ rdoc.title = 'Saucerly'
35
+ rdoc.options << '--line-numbers' << '--inline-source'
36
+ rdoc.rdoc_files.include('README')
37
+ rdoc.rdoc_files.include('lib/**/*.rb')
38
+ end
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ - turn into a gemplugin
2
+ - stop flying_saucer from polluting the logs
3
+ - add support for saving the pdf to a file?
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
@@ -0,0 +1,7 @@
1
+ require 'java'
2
+ require 'flying_saucer'
3
+
4
+ require 'saucerly/pdf_helper'
5
+
6
+ Mime::Type.register 'application/pdf', :pdf
7
+ ActionController::Base.send(:include, Saucerly::PdfHelper)
@@ -0,0 +1,60 @@
1
+ module Saucerly
2
+ module PdfHelper
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method_chain :render, :saucerly
6
+ end
7
+ end
8
+
9
+ def render_with_saucerly(options = nil, *args, &block)
10
+ if options.is_a?(Hash) && options.has_key?(:pdf)
11
+ options[:name] ||= options.delete(:pdf)
12
+ make_and_send_pdf(options.delete(:name), options)
13
+ else
14
+ render_without_saucerly(options, *args, &block)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def pdf_from_string(str)
21
+ # These lines are extracted from ITextRenderer's #setDocumentFromString, which doesn't
22
+ # seem to exist in the flying_saucer gem
23
+ is = org.xml.sax.InputSource.new(java.io.BufferedReader.new(java.io.StringReader.new(str)))
24
+ dom = org.xhtmlrenderer.resource.XMLResource.load(is).getDocument()
25
+
26
+ renderer = org.xhtmlrenderer.pdf.ITextRenderer.new
27
+ renderer.setDocument(dom, nil)
28
+ renderer.layout
29
+
30
+ output = java.io.ByteArrayOutputStream.new
31
+ renderer.createPDF(output, true)
32
+
33
+ String.from_java_bytes(output.to_byte_array)
34
+ end
35
+
36
+ def make_pdf(options = {})
37
+ html_string = if options[:inline]
38
+ render_to_string(:inline => options[:inline])
39
+ else
40
+ render_to_string(:template => options[:template], :layout => options[:layout])
41
+ end
42
+
43
+ # Make all paths relative, on disk paths...
44
+ html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
45
+ html_string.gsub!(/src=["']+([^:]+?)["']/i) { |m| "src=\"#{Rails.root}/public/" + $1 + '"' } # re-route absolute paths
46
+
47
+ # Remove asset ids on images with a regex
48
+ html_string.gsub!(/src=["'](\S+\?\d*)["']/i) { |m| 'src="' + $1.split('?').first + '"' }
49
+ pdf_from_string(html_string)
50
+ end
51
+
52
+ def make_and_send_pdf(pdf_name, options = {})
53
+ send_data_options = {:filename => pdf_name + ".pdf", :type => 'application/pdf'}
54
+ disposition = options.delete(:disposition)
55
+ send_data_options[:disposition] = disposition if disposition
56
+
57
+ send_data(make_pdf(options), send_data_options)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'saucerly')
@@ -0,0 +1,48 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{saucerly}
8
+ s.version = "0.5.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tim Riley"]
12
+ s.date = %q{2009-09-25}
13
+ s.email = %q{tim@openmonkey.com}
14
+ s.extra_rdoc_files = [
15
+ "README.md"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "MIT-LICENSE",
20
+ "README.md",
21
+ "Rakefile",
22
+ "TODO",
23
+ "VERSION",
24
+ "init.rb",
25
+ "lib/saucerly.rb",
26
+ "lib/saucerly/pdf_helper.rb",
27
+ "rails/init.rb",
28
+ "saucerly.gemspec"
29
+ ]
30
+ s.homepage = %q{http://github.com/timriley/saucerly}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.4}
34
+ s.summary = %q{PDF rending plugin for Rails using FlyingSaucer.}
35
+
36
+ if s.respond_to? :specification_version then
37
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
41
+ s.add_runtime_dependency(%q<flying_saucer>, [">= 0"])
42
+ else
43
+ s.add_dependency(%q<flying_saucer>, [">= 0"])
44
+ end
45
+ else
46
+ s.add_dependency(%q<flying_saucer>, [">= 0"])
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: saucerly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Tim Riley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-25 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: flying_saucer
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: tim@openmonkey.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.md
33
+ files:
34
+ - .gitignore
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - TODO
39
+ - VERSION
40
+ - init.rb
41
+ - lib/saucerly.rb
42
+ - lib/saucerly/pdf_helper.rb
43
+ - rails/init.rb
44
+ - saucerly.gemspec
45
+ has_rdoc: true
46
+ homepage: http://github.com/timriley/saucerly
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.4
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: PDF rending plugin for Rails using FlyingSaucer.
73
+ test_files: []
74
+