timriley-saucerly 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,38 @@
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
+ 1. Install [JRuby](http://jruby.org/)
32
+ 2. Register the flying_saucer gem dependency: add `config.gem 'flying_saucer'` to `config/environment.rb`
33
+ 3. Install flying_saucer: `jruby -S rake gems:install`
34
+ 4. Install Saucerly: `jruby script/plugin install git://github.com/timriley/saucerly`
35
+ 5. You're ready to go! Add some code to your controllers like the examples above.
36
+ 6. 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
37
+
38
+ Copyright (c) 2009 Tim Riley & RentMonkey Pty Ltd, released under the MIT license
data/Rakefile ADDED
@@ -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.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
@@ -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,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)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/../lib/saucerly/rails"
data/saucerly.gemspec ADDED
@@ -0,0 +1,47 @@
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.0"
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
+ "MIT-LICENSE",
19
+ "README.md",
20
+ "Rakefile",
21
+ "TODO",
22
+ "VERSION",
23
+ "init.rb",
24
+ "lib/saucerly/pdf_helper.rb",
25
+ "lib/saucerly/rails.rb",
26
+ "rails/init.rb",
27
+ "saucerly.gemspec"
28
+ ]
29
+ s.homepage = %q{http://github.com/timriley/saucerly}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.4}
33
+ s.summary = %q{PDF rending plugin for Rails using FlyingSaucer.}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ s.add_runtime_dependency(%q<flying_saucer>, [">= 0"])
41
+ else
42
+ s.add_dependency(%q<flying_saucer>, [">= 0"])
43
+ end
44
+ else
45
+ s.add_dependency(%q<flying_saucer>, [">= 0"])
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timriley-saucerly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
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 -07: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
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - TODO
38
+ - VERSION
39
+ - init.rb
40
+ - lib/saucerly/pdf_helper.rb
41
+ - lib/saucerly/rails.rb
42
+ - rails/init.rb
43
+ - saucerly.gemspec
44
+ has_rdoc: false
45
+ homepage: http://github.com/timriley/saucerly
46
+ licenses:
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.5
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: PDF rending plugin for Rails using FlyingSaucer.
71
+ test_files: []
72
+