Changeset 883

Show
Ignore:
Timestamp:
05/02/07 11:03:47 (2 years ago)
Author:
sandal
Message:

Fix for #293

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ruport/trunk/lib/ruport/formatter/pdf.rb

    r873 r883  
    4040  #       * style (:inline,:justified,:separated,:offset) 
    4141  # 
    42   class Formatter::PDF < Formatter     
     42  class Formatter::PDF < Formatter           
     43     
     44    ## THESE ARE WHY YOU SHOULD NEVER USE PDF::Writer  
     45     
     46    module PDFWriterMemoryPatch #:nodoc: 
     47      unless self.class.instance_methods.include?("_post_transaction_rewind") 
     48        def _post_transaction_rewind 
     49          @objects.each { |e| e.instance_variable_set(:@parent,self) } 
     50        end 
     51      end 
     52    end     
     53     
     54    module PDFSimpleTableOrderingPatch #:nodoc: 
     55      def __find_table_max_width__(pdf)       
     56         #p "this actually gets called" 
     57         max_width = PDF::Writer::OHash.new(-1) 
     58 
     59           # Find the maximum cell widths based on the data and the headings. 
     60           # Passing through the data multiple times is unavoidable as we must do 
     61           # some analysis first. 
     62         @data.each do |row| 
     63           @cols.each do |name, column| 
     64             w = pdf.text_width(row[name].to_s, @font_size) 
     65             w *= PDF::SimpleTable::WIDTH_FACTOR 
     66 
     67             max_width[name] = w if w > max_width[name] 
     68           end 
     69         end 
     70 
     71         @cols.each do |name, column| 
     72           title = column.heading.title if column.heading 
     73           title ||= column.name 
     74           w = pdf.text_width(title, @heading_font_size) 
     75           w *= PDF::SimpleTable::WIDTH_FACTOR   
     76           max_width[name] = w if w > max_width[name] 
     77         end 
     78         max_width 
     79      end 
     80    end 
    4381     
    4482    renders :pdf, :for => [ Renderer::Row, Renderer::Table, 
     
    5593 
    5694    def initialize 
    57       quiet do 
     95      quiet do    
    5896        require "pdf/writer" 
    5997        require "pdf/simpletable" 
     
    275313      format_opts = table_format.merge(format_opts) if table_format   
    276314       
    277       ::PDF::SimpleTable.new do |table|               
    278         table.data = table_data   
     315      ::PDF::SimpleTable.new do |table|  
     316        table.extend(PDFSimpleTableOrderingPatch)              
    279317        table.maximum_width = 500 
    280         table.column_order  = table_data.column_names                                             
    281            
     318        table.column_order  = table_data.column_names 
     319        table.data = table_data                                                  
    282320        apply_pdf_table_column_opts(table,table_data,format_opts) 
    283321 
    284         format_opts.each {|k,v| table.send("#{k}=", v) } 
    285  
     322        format_opts.each {|k,v| table.send("#{k}=", v) }   
    286323        table.render_on(pdf_writer) 
    287324      end 
     
    344381 
    345382    include DrawingHelpers 
    346       
    347     module PDFWriterMemoryPatch #:nodoc: 
    348       unless self.class.instance_methods.include?("_post_transaction_rewind") 
    349         def _post_transaction_rewind 
    350           @objects.each { |e| e.instance_variable_set(:@parent,self) } 
    351         end 
    352       end 
    353     end 
    354383     
    355384    private