Project

General

Profile

Patch #5629 » issue5629_wrap_text_fields_in_pdf_r4784.patch

Patch updated for Redmine v1.1.1 (r4784) - Hugo Ferreira, 2011-02-01 14:53

View differences:

lib/redmine/export/pdf.rb
88 88
          end
89 89
        end
90 90
          
91
        def Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
91
        def fix_text_encoding(txt)
92 92
          @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
93 93
          # these quotation marks are not correctly rendered in the pdf
94 94
          txt = txt.gsub(/[“�]/, '"') if txt
......
100 100
          rescue
101 101
            txt
102 102
          end || ''
103
          super w,h,txt,border,ln,align,fill,link
103
          return txt
104
        end
105
        
106
        def Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
107
          super w,h,fix_text_encoding(txt),border,ln,align,fill,link
108
        end
109
        
110
        def MultiCell(w,h=0,txt='',border=0,align='',fill=0)
111
          super w,h,fix_text_encoding(txt),border,align,fill
104 112
        end
105 113
        
106 114
        def Footer
......
122 130
        pdf.SetTitle(title)
123 131
        pdf.AliasNbPages
124 132
        pdf.footer_date = format_date(Date.today)
133
        pdf.SetAutoPageBreak(false)
125 134
        pdf.AddPage("L")
126 135
        
127
        row_height = 6
136
        # Landscape A4 = 210 x 297 mm
137
        page_height = 210
138
        page_width = 297
139
        right_margin = 10
140
        bottom_margin = 20
141
        col_id_width = 10
142
        row_height = 5
143
        
144
        # column widths
145
        table_width = page_width - right_margin - 10  # fixed left margin
128 146
        col_width = []
129 147
        unless query.columns.empty?
130
          col_width = query.columns.collect {|column| column.name == :subject ? 4.0 : 1.0 }
131
          ratio = 262.0 / col_width.inject(0) {|s,w| s += w}
148
          col_width = query.columns.collect do |c|
149
            (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) && ['string', 'text'].include?(c.custom_field.field_format)))? 4.0 : 1.0
150
          end
151
          ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
132 152
          col_width = col_width.collect {|w| w * ratio}
133 153
        end
134 154
        
......
140 160
        # headers
141 161
        pdf.SetFontStyle('B',8)
142 162
        pdf.SetFillColor(230, 230, 230)
143
        pdf.Cell(15, row_height, "#", 1, 0, 'L', 1)
163
        pdf.Cell(col_id_width, row_height, "#", 1, 0, 'C', 1)
144 164
        query.columns.each_with_index do |column, i|
145 165
          pdf.Cell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
146 166
        end
......
159 179
            pdf.SetFontStyle('',8)
160 180
            previous_group = group
161 181
          end
162
          pdf.Cell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
163
          query.columns.each_with_index do |column, i|
182
          
183
          # fetch all the row values
184
          col_values = query.columns.collect do |column|
164 185
            s = if column.is_a?(QueryCustomFieldColumn)
165 186
              cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
166 187
              show_value(cv)
......
174 195
                value
175 196
              end
176 197
            end
177
            pdf.Cell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
198
            s.to_s
178 199
          end
179
          pdf.Ln
200
          
201
          # render it off-page to find the max height used
202
          base_x = pdf.GetX
203
          base_y = pdf.GetY
204
          pdf.SetY(2 * page_height)
205
          max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
206
          pdf.SetXY(base_x, base_y)
207
          
208
          # make new page if it doesn't fit on the current one
209
          space_left = page_height - base_y - bottom_margin
210
          if max_height > space_left
211
            pdf.AddPage("L")
212
            base_x = pdf.GetX
213
            base_y = pdf.GetY
214
          end
215
          
216
          # write the cells on page
217
          pdf.Cell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
218
          issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
219
          issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
220
          pdf.SetY(base_y + max_height);
180 221
        end
222
        
181 223
        if issues.size == Setting.issues_export_limit.to_i
182 224
          pdf.SetFontStyle('B',10)
183 225
          pdf.Cell(0, row_height, '...')
184 226
        end
185 227
        pdf.Output
186 228
      end
187

  
229
      
230
      # Renders MultiCells and returns the maximum height used
231
      def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height)
232
        base_y = pdf.GetY
233
        max_height = row_height
234
        col_values.each_with_index do |column, i|
235
          col_x = pdf.GetX
236
          pdf.MultiCell(col_widths[i], row_height, col_values[i], "T", 'L', 1)
237
          max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
238
          pdf.SetXY(col_x + col_widths[i], base_y);
239
        end
240
        return max_height
241
      end
242
      
243
      # Draw lines to close the row (MultiCell border drawing in not uniform)
244
      def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y, id_width, col_widths)
245
        col_x = top_x + id_width
246
        pdf.Line(col_x, top_y, col_x, lower_y)    # id right border
247
        col_widths.each do |width|
248
          col_x += width
249
          pdf.Line(col_x, top_y, col_x, lower_y)  # columns right border
250
        end
251
        pdf.Line(top_x, top_y, top_x, lower_y)    # left border
252
        pdf.Line(top_x, lower_y, col_x, lower_y)  # bottom border
253
      end
254
      
188 255
      # Returns a PDF string of a single issue
189 256
      def issue_to_pdf(issue)
190 257
        pdf = IFPDF.new(current_language)
......
194 261
        pdf.AddPage
195 262
        
196 263
        pdf.SetFontStyle('B',11)    
197
        pdf.Cell(190,10, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
264
        pdf.MultiCell(190,5, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
198 265
        pdf.Ln
199 266
        
200 267
        y0 = pdf.GetY
......
247 314
        end
248 315
        
249 316
        pdf.SetFontStyle('B',9)
250
        pdf.Cell(35,5, l(:field_subject) + ":","LTB")
317
        pdf.Cell(35,5, l(:field_subject) + ":","LT")
251 318
        pdf.SetFontStyle('',9)
252
        pdf.Cell(155,5, issue.subject,"RTB")
253
        pdf.Ln    
319
        pdf.MultiCell(155,5, issue.subject,"RT")
254 320
        
255 321
        pdf.SetFontStyle('B',9)
256
        pdf.Cell(35,5, l(:field_description) + ":")
322
        pdf.Cell(35,5, l(:field_description) + ":","LT")
257 323
        pdf.SetFontStyle('',9)
258
        pdf.MultiCell(155,5, issue.description.to_s,"BR")
324
        pdf.MultiCell(155,5, issue.description.to_s,"RT")
259 325
        
260 326
        pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
261
        pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
327
        pdf.Line(pdf.GetX, pdf.GetY, pdf.GetX + 190, pdf.GetY)
262 328
        pdf.Ln
263 329
        
264 330
        if issue.changesets.any? && User.current.allowed_to?(:view_changesets, issue.project)
......
286 352
          pdf.Ln
287 353
          pdf.SetFontStyle('I',8)
288 354
          for detail in journal.details
289
            pdf.Cell(190,5, "- " + show_detail(detail, true))
290
            pdf.Ln
355
            pdf.MultiCell(190,5, "- " + show_detail(detail, true))
291 356
          end
292 357
          if journal.notes?
358
            pdf.Ln unless journal.details.empty?
293 359
            pdf.SetFontStyle('',8)
294 360
            pdf.MultiCell(190,5, journal.notes.to_s)
295 361
          end   
(5-5/8)