Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

OMG Significantly reduced size for Rails 3.

  • Loading branch information...
commit 779d88db2e5febd0b704372c286e95f2461f8201 1 parent bf327fc
Yehuda Katz authored
32 lib/saucerly.rb
... ... @@ -1,7 +1,33 @@
1 1 require 'java'
2 2 require 'flying_saucer'
3 3
4   -require 'saucerly/pdf_helper'
5   -
  4 +require "action_controller"
6 5 Mime::Type.register 'application/pdf', :pdf
7   -ActionController::Base.send(:include, Saucerly::PdfHelper)
  6 +
  7 +ActionController.add_renderer :pdf do |pdf_name, options|
  8 + send_data_options = {:filename => pdf_name + ".pdf", :type => 'application/pdf'}
  9 + send_data_options.merge!(options.slice(:disposition))
  10 +
  11 + html_string = render_to_string(options)
  12 +
  13 + send_data(Saucerly::Pdf.new(html_string).to_pdf, send_data_options)
  14 +end
  15 +
  16 +class Saucerly::Pdf < String
  17 + def normalize!
  18 + gsub!(".com:/", ".com/") # strip out bad attachment_fu URLs
  19 + gsub!(/src=["']+([^:]+?)["']/i, %{src="#{Rails.root}/public/\\1"}) # reroute absolute paths
  20 + gsub!(/(src=["']\S+)(\?\d*)?(["'])/i, '\1\3') # remove asset ids
  21 + end
  22 +
  23 + def to_pdf
  24 + normalize!
  25 + io = StringIO.new
  26 + ITextRenderer.new.instance_eval do
  27 + set_document_from_string(self)
  28 + layout
  29 + create_pdf(io.to_outputstream)
  30 + end
  31 + io.string
  32 + end
  33 +end
60 lib/saucerly/pdf_helper.rb
... ... @@ -1,60 +0,0 @@
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
1  rails/init.rb
... ... @@ -1 +0,0 @@
1   -require File.join(File.dirname(__FILE__), '..', 'lib', 'saucerly')

0 comments on commit 779d88d

Please sign in to comment.
Something went wrong with that request. Please try again.