##// END OF EJS Templates
PDF: wrap title fields properly (#8295)....
Toshi MARUYAMA -
r5531:919bf61caeb4
parent child
Show More
@@ -1,458 +1,470
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2009 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require 'iconv'
21 21 require 'rfpdf/fpdf'
22 22 require 'fpdf/chinese'
23 23 require 'fpdf/japanese'
24 24 require 'fpdf/korean'
25 25
26 26 module Redmine
27 27 module Export
28 28 module PDF
29 29 include ActionView::Helpers::TextHelper
30 30 include ActionView::Helpers::NumberHelper
31 31
32 32 class ITCPDF < TCPDF
33 33 include Redmine::I18n
34 34 attr_accessor :footer_date
35 35
36 36 def initialize(lang)
37 37 super()
38 38 set_language_if_valid lang
39 39 @font_for_content = 'FreeSans'
40 40 @font_for_footer = 'FreeSans'
41 41 SetCreator(Redmine::Info.app_name)
42 42 SetFont(@font_for_content)
43 43 end
44 44
45 45 def SetFontStyle(style, size)
46 46 SetFont(@font_for_content, style, size)
47 47 end
48 48
49 49 def SetTitle(txt)
50 50 txt = begin
51 51 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
52 52 hextxt = "<FEFF" # FEFF is BOM
53 53 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
54 54 hextxt << ">"
55 55 rescue
56 56 txt
57 57 end || ''
58 58 super(txt)
59 59 end
60 60
61 61 def textstring(s)
62 62 # Format a text string
63 63 if s =~ /^</ # This means the string is hex-dumped.
64 64 return s
65 65 else
66 66 return '('+escape(s)+')'
67 67 end
68 68 end
69 69
70 70 alias RDMCell Cell
71 71 alias RDMMultiCell MultiCell
72 72
73 73 def Footer
74 74 SetFont(@font_for_footer, 'I', 8)
75 75 SetY(-15)
76 76 SetX(15)
77 77 RDMCell(0, 5, @footer_date, 0, 0, 'L')
78 78 SetY(-15)
79 79 SetX(-30)
80 80 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
81 81 end
82 82 end
83 83
84 84 class IFPDF < FPDF
85 85 include Redmine::I18n
86 86 attr_accessor :footer_date
87 87
88 88 def initialize(lang)
89 89 super()
90 90 set_language_if_valid lang
91 91 case l(:general_pdf_encoding).upcase
92 92 when 'CP949'
93 93 extend(PDF_Korean)
94 94 AddUHCFont()
95 95 @font_for_content = 'UHC'
96 96 @font_for_footer = 'UHC'
97 97 when 'CP932'
98 98 extend(PDF_Japanese)
99 99 AddSJISFont()
100 100 @font_for_content = 'SJIS'
101 101 @font_for_footer = 'SJIS'
102 102 when 'GB18030'
103 103 extend(PDF_Chinese)
104 104 AddGBFont()
105 105 @font_for_content = 'GB'
106 106 @font_for_footer = 'GB'
107 107 when 'BIG5'
108 108 extend(PDF_Chinese)
109 109 AddBig5Font()
110 110 @font_for_content = 'Big5'
111 111 @font_for_footer = 'Big5'
112 112 else
113 113 @font_for_content = 'Arial'
114 114 @font_for_footer = 'Helvetica'
115 115 end
116 116 SetCreator(Redmine::Info.app_name)
117 117 SetFont(@font_for_content)
118 118 end
119 119
120 120 def SetFontStyle(style, size)
121 121 SetFont(@font_for_content, style, size)
122 122 end
123 123
124 124 def SetTitle(txt)
125 125 txt = begin
126 126 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
127 127 hextxt = "<FEFF" # FEFF is BOM
128 128 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
129 129 hextxt << ">"
130 130 rescue
131 131 txt
132 132 end || ''
133 133 super(txt)
134 134 end
135 135
136 136 def textstring(s)
137 137 # Format a text string
138 138 if s =~ /^</ # This means the string is hex-dumped.
139 139 return s
140 140 else
141 141 return '('+escape(s)+')'
142 142 end
143 143 end
144 144
145 145 def fix_text_encoding(txt)
146 146 txt ||= ''
147 147 if txt.respond_to?(:force_encoding)
148 148 txt.force_encoding('UTF-8')
149 149 txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace,
150 150 :undef => :replace, :replace => '?')
151 151 txt.force_encoding('ASCII-8BIT')
152 152 else
153 153 @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
154 154 txtar = ""
155 155 begin
156 156 txtar += @ic.iconv(txt)
157 157 rescue Iconv::IllegalSequence
158 158 txtar += $!.success
159 159 txt = '?' + $!.failed[1,$!.failed.length]
160 160 retry
161 161 rescue
162 162 txtar += $!.success
163 163 end
164 164 txt = txtar
165 165 end
166 166 txt
167 167 end
168 168
169 169 def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
170 170 Cell(w,h,fix_text_encoding(txt),border,ln,align,fill,link)
171 171 end
172 172
173 173 def RDMMultiCell(w,h=0,txt='',border=0,align='',fill=0)
174 174 MultiCell(w,h,fix_text_encoding(txt),border,align,fill)
175 175 end
176 176
177 177 def Footer
178 178 SetFont(@font_for_footer, 'I', 8)
179 179 SetY(-15)
180 180 SetX(15)
181 181 RDMCell(0, 5, @footer_date, 0, 0, 'L')
182 182 SetY(-15)
183 183 SetX(-30)
184 184 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
185 185 end
186 186 alias alias_nb_pages AliasNbPages
187 187 end
188 188
189 189 # Returns a PDF string of a list of issues
190 190 def issues_to_pdf(issues, project, query)
191 191 if l(:general_pdf_encoding).upcase != 'UTF-8'
192 192 pdf = IFPDF.new(current_language)
193 193 else
194 194 pdf = ITCPDF.new(current_language)
195 195 end
196 196 title = query.new_record? ? l(:label_issue_plural) : query.name
197 197 title = "#{project} - #{title}" if project
198 198 pdf.SetTitle(title)
199 199 pdf.alias_nb_pages
200 200 pdf.footer_date = format_date(Date.today)
201 201 pdf.SetAutoPageBreak(false)
202 202 pdf.AddPage("L")
203 203
204 204 # Landscape A4 = 210 x 297 mm
205 205 page_height = 210
206 206 page_width = 297
207 207 right_margin = 10
208 208 bottom_margin = 20
209 209 col_id_width = 10
210 210 row_height = 5
211 211
212 212 # column widths
213 213 table_width = page_width - right_margin - 10 # fixed left margin
214 214 col_width = []
215 215 unless query.columns.empty?
216 216 col_width = query.columns.collect do |c|
217 217 (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) && ['string', 'text'].include?(c.custom_field.field_format)))? 4.0 : 1.0
218 218 end
219 219 ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
220 220 col_width = col_width.collect {|w| w * ratio}
221 221 end
222 222
223 223 # title
224 224 pdf.SetFontStyle('B',11)
225 225 pdf.RDMCell(190,10, title)
226 226 pdf.Ln
227 227
228 228 # headers
229 229 pdf.SetFontStyle('B',8)
230 230 pdf.SetFillColor(230, 230, 230)
231 pdf.RDMCell(col_id_width, row_height, "#", 1, 0, 'C', 1)
232 query.columns.each_with_index do |column, i|
233 pdf.RDMCell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
234 end
235 pdf.Ln
231
232 # render it background to find the max height used
233 base_x = pdf.GetX
234 base_y = pdf.GetY
235 max_height = issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
236 pdf.Rect(base_x, base_y, table_width, max_height, 'FD');
237 pdf.SetXY(base_x, base_y);
238
239 # write the cells on page
240 pdf.RDMCell(col_id_width, row_height, "#", "T", 0, 'C', 1)
241 issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
242 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
243 pdf.SetY(base_y + max_height);
236 244
237 245 # rows
238 246 pdf.SetFontStyle('',8)
239 247 pdf.SetFillColor(255, 255, 255)
240 248 previous_group = false
241 249 issues.each do |issue|
242 250 if query.grouped? &&
243 251 (group = query.group_by_column.value(issue)) != previous_group
244 252 pdf.SetFontStyle('B',9)
245 253 pdf.RDMCell(277, row_height,
246 254 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
247 255 1, 1, 'L')
248 256 pdf.SetFontStyle('',8)
249 257 previous_group = group
250 258 end
251 259 # fetch all the row values
252 260 col_values = query.columns.collect do |column|
253 261 s = if column.is_a?(QueryCustomFieldColumn)
254 262 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
255 263 show_value(cv)
256 264 else
257 265 value = issue.send(column.name)
258 266 if value.is_a?(Date)
259 267 format_date(value)
260 268 elsif value.is_a?(Time)
261 269 format_time(value)
262 270 else
263 271 value
264 272 end
265 273 end
266 274 s.to_s
267 275 end
268 276
269 277 # render it off-page to find the max height used
270 278 base_x = pdf.GetX
271 279 base_y = pdf.GetY
272 280 pdf.SetY(2 * page_height)
273 281 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
274 282 pdf.SetXY(base_x, base_y)
275 283
276 284 # make new page if it doesn't fit on the current one
277 285 space_left = page_height - base_y - bottom_margin
278 286 if max_height > space_left
279 287 pdf.AddPage("L")
280 288 base_x = pdf.GetX
281 289 base_y = pdf.GetY
282 290 end
283 291
284 292 # write the cells on page
285 293 pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
286 294 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
287 295 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
288 296 pdf.SetY(base_y + max_height);
289 297 end
290 298
291 299 if issues.size == Setting.issues_export_limit.to_i
292 300 pdf.SetFontStyle('B',10)
293 301 pdf.RDMCell(0, row_height, '...')
294 302 end
295 303 pdf.Output
296 304 end
297 305
298 306 # Renders MultiCells and returns the maximum height used
299 def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height)
307 def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height, head=false)
300 308 base_y = pdf.GetY
301 309 max_height = row_height
302 310 col_values.each_with_index do |column, i|
303 311 col_x = pdf.GetX
304 pdf.RDMMultiCell(col_widths[i], row_height, col_values[i], "T", 'L', 1)
312 if head == true
313 pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1)
314 else
315 pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1)
316 end
305 317 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
306 318 pdf.SetXY(col_x + col_widths[i], base_y);
307 319 end
308 320 return max_height
309 321 end
310 322
311 323 # Draw lines to close the row (MultiCell border drawing in not uniform)
312 324 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y, id_width, col_widths)
313 325 col_x = top_x + id_width
314 326 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
315 327 col_widths.each do |width|
316 328 col_x += width
317 329 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
318 330 end
319 331 pdf.Line(top_x, top_y, top_x, lower_y) # left border
320 332 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
321 333 end
322 334
323 335 # Returns a PDF string of a single issue
324 336 def issue_to_pdf(issue)
325 337 if l(:general_pdf_encoding).upcase != 'UTF-8'
326 338 pdf = IFPDF.new(current_language)
327 339 else
328 340 pdf = ITCPDF.new(current_language)
329 341 end
330 342 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
331 343 pdf.alias_nb_pages
332 344 pdf.footer_date = format_date(Date.today)
333 345 pdf.AddPage
334 346 pdf.SetFontStyle('B',11)
335 347 pdf.RDMMultiCell(190,5, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
336 348 pdf.Ln
337 349
338 350 y0 = pdf.GetY
339 351
340 352 pdf.SetFontStyle('B',9)
341 353 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
342 354 pdf.SetFontStyle('',9)
343 355 pdf.RDMCell(60,5, issue.status.to_s,"RT")
344 356 pdf.SetFontStyle('B',9)
345 357 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
346 358 pdf.SetFontStyle('',9)
347 359 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
348 360 pdf.Ln
349 361
350 362 pdf.SetFontStyle('B',9)
351 363 pdf.RDMCell(35,5, l(:field_author) + ":","L")
352 364 pdf.SetFontStyle('',9)
353 365 pdf.RDMCell(60,5, issue.author.to_s,"R")
354 366 pdf.SetFontStyle('B',9)
355 367 pdf.RDMCell(35,5, l(:field_category) + ":","L")
356 368 pdf.SetFontStyle('',9)
357 369 pdf.RDMCell(60,5, issue.category.to_s,"R")
358 370 pdf.Ln
359 371
360 372 pdf.SetFontStyle('B',9)
361 373 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
362 374 pdf.SetFontStyle('',9)
363 375 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
364 376 pdf.SetFontStyle('B',9)
365 377 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
366 378 pdf.SetFontStyle('',9)
367 379 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
368 380 pdf.Ln
369 381
370 382 pdf.SetFontStyle('B',9)
371 383 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
372 384 pdf.SetFontStyle('',9)
373 385 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
374 386 pdf.SetFontStyle('B',9)
375 387 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
376 388 pdf.SetFontStyle('',9)
377 389 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
378 390 pdf.Ln
379 391
380 392 for custom_value in issue.custom_field_values
381 393 pdf.SetFontStyle('B',9)
382 394 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
383 395 pdf.SetFontStyle('',9)
384 396 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
385 397 end
386 398
387 399 pdf.SetFontStyle('B',9)
388 400 pdf.RDMCell(35,5, l(:field_subject) + ":","LT")
389 401 pdf.SetFontStyle('',9)
390 402 pdf.RDMMultiCell(155,5, issue.subject,"RT")
391 403
392 404 pdf.SetFontStyle('B',9)
393 405 pdf.RDMCell(35,5, l(:field_description) + ":","LT")
394 406 pdf.SetFontStyle('',9)
395 407 pdf.RDMMultiCell(155,5, issue.description.to_s,"RT")
396 408
397 409 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
398 410 pdf.Line(pdf.GetX, pdf.GetY, pdf.GetX + 190, pdf.GetY)
399 411 pdf.Ln
400 412
401 413 if issue.changesets.any? &&
402 414 User.current.allowed_to?(:view_changesets, issue.project)
403 415 pdf.SetFontStyle('B',9)
404 416 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
405 417 pdf.Ln
406 418 for changeset in issue.changesets
407 419 pdf.SetFontStyle('B',8)
408 420 pdf.RDMCell(190,5,
409 421 format_time(changeset.committed_on) + " - " + changeset.author.to_s)
410 422 pdf.Ln
411 423 unless changeset.comments.blank?
412 424 pdf.SetFontStyle('',8)
413 425 pdf.RDMMultiCell(190,5, changeset.comments.to_s)
414 426 end
415 427 pdf.Ln
416 428 end
417 429 end
418 430
419 431 pdf.SetFontStyle('B',9)
420 432 pdf.RDMCell(190,5, l(:label_history), "B")
421 433 pdf.Ln
422 434 for journal in issue.journals.find(
423 435 :all, :include => [:user, :details],
424 436 :order => "#{Journal.table_name}.created_on ASC")
425 437 pdf.SetFontStyle('B',8)
426 438 pdf.RDMCell(190,5,
427 439 format_time(journal.created_on) + " - " + journal.user.name)
428 440 pdf.Ln
429 441 pdf.SetFontStyle('I',8)
430 442 for detail in journal.details
431 443 pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true))
432 444 end
433 445 if journal.notes?
434 446 pdf.Ln unless journal.details.empty?
435 447 pdf.SetFontStyle('',8)
436 448 pdf.RDMMultiCell(190,5, journal.notes.to_s)
437 449 end
438 450 pdf.Ln
439 451 end
440 452
441 453 if issue.attachments.any?
442 454 pdf.SetFontStyle('B',9)
443 455 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
444 456 pdf.Ln
445 457 for attachment in issue.attachments
446 458 pdf.SetFontStyle('',8)
447 459 pdf.RDMCell(80,5, attachment.filename)
448 460 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
449 461 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
450 462 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
451 463 pdf.Ln
452 464 end
453 465 end
454 466 pdf.Output
455 467 end
456 468 end
457 469 end
458 470 end
General Comments 0
You need to be logged in to leave comments. Login now