public
Fork of sandal/prawn
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.lighthouseapp.com/projects/9398/home
Clone URL: git://github.com/yob/prawn.git
added support for compressing page content streams
James Healy (author)
Wed Aug 06 03:44:38 -0700 2008
commit  a3e1a59fffd0e3965bdc1b7932afe9b8269bdcbd
tree    a875b32361b46b93c04eb308eea6ed146875cd56
parent  50245e66e8e6640be121f11e90216657985c4d55
...
59
60
61
 
62
63
64
...
80
81
82
 
83
84
85
...
239
240
241
 
 
 
 
 
242
243
244
...
286
287
288
 
289
290
291
...
59
60
61
62
63
64
65
...
81
82
83
84
85
86
87
...
241
242
243
244
245
246
247
248
249
250
251
...
293
294
295
296
297
298
299
0
@@ -59,6 +59,7 @@ module Prawn
0
     # <tt>:top_margin</tt>:: Sets the top margin in points [ 0.5 inch]
0
     # <tt>:bottom_margin</tt>:: Sets the bottom margin in points [0.5 inch]
0
     # <tt>:skip_page_creation</tt>:: Creates a document without starting the first page [false]
0
+    # <tt>:compress</tt>:: Compresses content streams before rendering them [false]
0
     # 
0
     #                             
0
     #   # New document, US Letter paper, portrait orientation
0
@@ -80,6 +81,7 @@ module Prawn
0
        @page_stop_proc  = options[:on_page_end]              
0
        @page_size   = options[:page_size]   || "LETTER"    
0
        @page_layout = options[:page_layout] || :portrait
0
+       @compress = options[:compress] || false
0
              
0
        @margins = { :left   => options[:left_margin]   || 36,
0
                     :right  => options[:right_margin]  || 36,  
0
@@ -239,6 +241,11 @@ module Prawn
0
       yield
0
       fields.each { |f| send("#{f}=", stored[f]) }
0
     end
0
+
0
+    def compression_enabled?
0
+      @compress
0
+    end
0
+
0
    
0
     private 
0
     
0
@@ -286,6 +293,7 @@ module Prawn
0
     def finish_page_content     
0
       @page_stop_proc[self] if @page_stop_proc
0
       add_content "Q"
0
+      @page_content.compress_stream if compression_enabled?
0
       @page_content.data[:Length] = @page_content.stream.size
0
     end
0
     
...
6
7
8
 
 
9
10
11
...
17
18
19
 
20
21
22
...
29
30
31
 
32
33
34
35
36
37
38
 
 
 
 
 
 
39
40
41
...
6
7
8
9
10
11
12
13
...
19
20
21
22
23
24
25
...
32
33
34
35
36
37
38
39
40
41
 
42
43
44
45
46
47
48
49
50
0
@@ -6,6 +6,8 @@
0
 #
0
 # This is free software. Please see the LICENSE and COPYING files for details.
0
 
0
+require 'zlib'
0
+
0
 module Prawn  
0
   
0
   class Reference #:nodoc:
0
@@ -17,6 +19,7 @@ module Prawn
0
       @identifier = id 
0
       @gen   = 0       
0
       @data  = data     
0
+      @compressed = false
0
     end            
0
     
0
     def object 
0
@@ -29,13 +32,19 @@ module Prawn
0
     end  
0
     
0
     def <<(data)
0
+      raise 'Cannot add data to a stream that is compressed' if @compressed
0
       (@stream ||= "") << data  
0
     end  
0
     
0
     def to_s            
0
       "#{@identifier} #{gen} R"
0
     end
0
-      
0
+
0
+    def compress_stream
0
+      @stream = Zlib::Deflate.deflate(@stream)
0
+      @data[:Filter] = :FlateDecode
0
+      @compressed = true
0
+    end
0
   end         
0
   
0
   module_function
...
73
74
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
77
78
...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
0
@@ -73,6 +73,30 @@ describe "When ending each page" do
0
     pdf.render
0
   end
0
 
0
+  it "should not compress the page content stream if compression is disabled" do
0
+
0
+    pdf = Prawn::Document.new(:compress => false)
0
+    content_stub = pdf.ref({})
0
+    content_stub.stub!(:compress_stream).and_return(true)
0
+    content_stub.should_not_receive(:compress_stream)
0
+
0
+    pdf.instance_variable_set("@page_content", content_stub)
0
+    pdf.text "Hi There" * 20
0
+    pdf.render
0
+  end
0
+
0
+  it "should compress the page content stream if compression is enabled" do
0
+
0
+    pdf = Prawn::Document.new(:compress => true)
0
+    content_stub = pdf.ref({})
0
+    content_stub.stub!(:compress_stream).and_return(true)
0
+    content_stub.should_receive(:compress_stream).exactly(1).times
0
+
0
+    pdf.instance_variable_set("@page_content", content_stub)
0
+    pdf.text "Hi There" * 20
0
+    pdf.render
0
+  end
0
+
0
 end                                 
0
 
0
 class PageDetails      
...
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
29
...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
0
@@ -26,4 +26,16 @@ describe "A Reference object" do
0
                            "\nBT\n/F1 12 Tf\n72 712 Td\n( A stream ) Tj\nET" +
0
                            "\nendstream\nendobj\n"
0
   end
0
+
0
+  it "should compress a stream upon request" do
0
+    ref = Prawn::Reference(2,{})
0
+    ref << "Hi There " * 20
0
+
0
+    cref = Prawn::Reference(2,{})
0
+    cref << "Hi There " * 20
0
+    cref.compress_stream
0
+
0
+    (cref.stream.size < ref.stream.size).should be_true
0
+    cref.data[:Filter].should eql(:FlateDecode)
0
+  end
0
 end
...
9
10
11
 
 
 
 
 
 
12
13
14
...
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -9,6 +9,12 @@ require "prawn"
0
 gem 'pdf-reader', ">=0.7.3"
0
 require "pdf/reader"
0
 
0
+module Prawn
0
+  class Document
0
+    public :ref
0
+  end
0
+end
0
+
0
 def create_pdf
0
   @pdf = Prawn::Document.new(:left_margin   => 0,
0
                              :right_margin  => 0,

Comments