##// END OF EJS Templates
Omit subtasks/related issues sections when empty on PDF export (#5634)....
Jean-Philippe Lang -
r7725:886310055bd1
parent child
Show More
@@ -1,519 +1,524
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2011 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 'fpdf/chinese'
22 22 require 'fpdf/japanese'
23 23 require 'fpdf/korean'
24 24
25 25 module Redmine
26 26 module Export
27 27 module PDF
28 28 include ActionView::Helpers::TextHelper
29 29 include ActionView::Helpers::NumberHelper
30 30 include IssuesHelper
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 set_language_if_valid lang
38 38 pdf_encoding = l(:general_pdf_encoding).upcase
39 39 super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding)
40 40 case current_language.to_s.downcase
41 41 when 'vi'
42 42 @font_for_content = 'DejaVuSans'
43 43 @font_for_footer = 'DejaVuSans'
44 44 else
45 45 case pdf_encoding
46 46 when 'UTF-8'
47 47 @font_for_content = 'FreeSans'
48 48 @font_for_footer = 'FreeSans'
49 49 when 'CP949'
50 50 extend(PDF_Korean)
51 51 AddUHCFont()
52 52 @font_for_content = 'UHC'
53 53 @font_for_footer = 'UHC'
54 54 when 'CP932', 'SJIS', 'SHIFT_JIS'
55 55 extend(PDF_Japanese)
56 56 AddSJISFont()
57 57 @font_for_content = 'SJIS'
58 58 @font_for_footer = 'SJIS'
59 59 when 'GB18030'
60 60 extend(PDF_Chinese)
61 61 AddGBFont()
62 62 @font_for_content = 'GB'
63 63 @font_for_footer = 'GB'
64 64 when 'BIG5'
65 65 extend(PDF_Chinese)
66 66 AddBig5Font()
67 67 @font_for_content = 'Big5'
68 68 @font_for_footer = 'Big5'
69 69 else
70 70 @font_for_content = 'Arial'
71 71 @font_for_footer = 'Helvetica'
72 72 end
73 73 end
74 74 SetCreator(Redmine::Info.app_name)
75 75 SetFont(@font_for_content)
76 76 end
77 77
78 78 def SetFontStyle(style, size)
79 79 SetFont(@font_for_content, style, size)
80 80 end
81 81
82 82 def SetTitle(txt)
83 83 txt = begin
84 84 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
85 85 hextxt = "<FEFF" # FEFF is BOM
86 86 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
87 87 hextxt << ">"
88 88 rescue
89 89 txt
90 90 end || ''
91 91 super(txt)
92 92 end
93 93
94 94 def textstring(s)
95 95 # Format a text string
96 96 if s =~ /^</ # This means the string is hex-dumped.
97 97 return s
98 98 else
99 99 return '('+escape(s)+')'
100 100 end
101 101 end
102 102
103 103 def fix_text_encoding(txt)
104 104 RDMPdfEncoding::rdm_from_utf8(txt, l(:general_pdf_encoding))
105 105 end
106 106
107 107 def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='')
108 108 Cell(w, h, fix_text_encoding(txt), border, ln, align, fill, link)
109 109 end
110 110
111 111 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
112 112 MultiCell(w, h, fix_text_encoding(txt), border, align, fill, ln)
113 113 end
114 114
115 115 def RDMwriteHTMLCell(w, h, x, y, html='', border=0, ln=1, fill=0)
116 116 writeHTMLCell(w, h, x, y, fix_text_encoding(html), border, ln, fill)
117 117 end
118 118
119 119 def Footer
120 120 SetFont(@font_for_footer, 'I', 8)
121 121 SetY(-15)
122 122 SetX(15)
123 123 RDMCell(0, 5, @footer_date, 0, 0, 'L')
124 124 SetY(-15)
125 125 SetX(-30)
126 126 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
127 127 end
128 128 end
129 129
130 130 # Returns a PDF string of a list of issues
131 131 def issues_to_pdf(issues, project, query)
132 132 pdf = ITCPDF.new(current_language)
133 133 title = query.new_record? ? l(:label_issue_plural) : query.name
134 134 title = "#{project} - #{title}" if project
135 135 pdf.SetTitle(title)
136 136 pdf.alias_nb_pages
137 137 pdf.footer_date = format_date(Date.today)
138 138 pdf.SetAutoPageBreak(false)
139 139 pdf.AddPage("L")
140 140
141 141 # Landscape A4 = 210 x 297 mm
142 142 page_height = 210
143 143 page_width = 297
144 144 right_margin = 10
145 145 bottom_margin = 20
146 146 col_id_width = 10
147 147 row_height = 5
148 148
149 149 # column widths
150 150 table_width = page_width - right_margin - 10 # fixed left margin
151 151 col_width = []
152 152 unless query.columns.empty?
153 153 col_width = query.columns.collect do |c|
154 154 (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) &&
155 155 ['string', 'text'].include?(c.custom_field.field_format))) ? 4.0 : 1.0
156 156 end
157 157 ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
158 158 col_width = col_width.collect {|w| w * ratio}
159 159 end
160 160
161 161 # title
162 162 pdf.SetFontStyle('B',11)
163 163 pdf.RDMCell(190,10, title)
164 164 pdf.Ln
165 165
166 166 # headers
167 167 pdf.SetFontStyle('B',8)
168 168 pdf.SetFillColor(230, 230, 230)
169 169
170 170 # render it background to find the max height used
171 171 base_x = pdf.GetX
172 172 base_y = pdf.GetY
173 173 max_height = issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
174 174 pdf.Rect(base_x, base_y, table_width, max_height, 'FD');
175 175 pdf.SetXY(base_x, base_y);
176 176
177 177 # write the cells on page
178 178 pdf.RDMCell(col_id_width, row_height, "#", "T", 0, 'C', 1)
179 179 issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
180 180 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
181 181 pdf.SetY(base_y + max_height);
182 182
183 183 # rows
184 184 pdf.SetFontStyle('',8)
185 185 pdf.SetFillColor(255, 255, 255)
186 186 previous_group = false
187 187 issue_list(issues) do |issue, level|
188 188 if query.grouped? &&
189 189 (group = query.group_by_column.value(issue)) != previous_group
190 190 pdf.SetFontStyle('B',9)
191 191 pdf.RDMCell(277, row_height,
192 192 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
193 193 1, 1, 'L')
194 194 pdf.SetFontStyle('',8)
195 195 previous_group = group
196 196 end
197 197 # fetch all the row values
198 198 col_values = query.columns.collect do |column|
199 199 s = if column.is_a?(QueryCustomFieldColumn)
200 200 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
201 201 show_value(cv)
202 202 else
203 203 value = issue.send(column.name)
204 204 if column.name == :subject
205 205 value = " " * level + value
206 206 end
207 207 if value.is_a?(Date)
208 208 format_date(value)
209 209 elsif value.is_a?(Time)
210 210 format_time(value)
211 211 else
212 212 value
213 213 end
214 214 end
215 215 s.to_s
216 216 end
217 217
218 218 # render it off-page to find the max height used
219 219 base_x = pdf.GetX
220 220 base_y = pdf.GetY
221 221 pdf.SetY(2 * page_height)
222 222 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
223 223 pdf.SetXY(base_x, base_y)
224 224
225 225 # make new page if it doesn't fit on the current one
226 226 space_left = page_height - base_y - bottom_margin
227 227 if max_height > space_left
228 228 pdf.AddPage("L")
229 229 base_x = pdf.GetX
230 230 base_y = pdf.GetY
231 231 end
232 232
233 233 # write the cells on page
234 234 pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
235 235 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
236 236 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
237 237 pdf.SetY(base_y + max_height);
238 238 end
239 239
240 240 if issues.size == Setting.issues_export_limit.to_i
241 241 pdf.SetFontStyle('B',10)
242 242 pdf.RDMCell(0, row_height, '...')
243 243 end
244 244 pdf.Output
245 245 end
246 246
247 247 # Renders MultiCells and returns the maximum height used
248 248 def issues_to_pdf_write_cells(pdf, col_values, col_widths,
249 249 row_height, head=false)
250 250 base_y = pdf.GetY
251 251 max_height = row_height
252 252 col_values.each_with_index do |column, i|
253 253 col_x = pdf.GetX
254 254 if head == true
255 255 pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1)
256 256 else
257 257 pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1)
258 258 end
259 259 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
260 260 pdf.SetXY(col_x + col_widths[i], base_y);
261 261 end
262 262 return max_height
263 263 end
264 264
265 265 # Draw lines to close the row (MultiCell border drawing in not uniform)
266 266 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y,
267 267 id_width, col_widths)
268 268 col_x = top_x + id_width
269 269 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
270 270 col_widths.each do |width|
271 271 col_x += width
272 272 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
273 273 end
274 274 pdf.Line(top_x, top_y, top_x, lower_y) # left border
275 275 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
276 276 end
277 277
278 278 # Returns a PDF string of a single issue
279 279 def issue_to_pdf(issue)
280 280 pdf = ITCPDF.new(current_language)
281 281 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
282 282 pdf.alias_nb_pages
283 283 pdf.footer_date = format_date(Date.today)
284 284 pdf.AddPage
285 285 pdf.SetFontStyle('B',11)
286 286 buf = "#{issue.project} - #{issue.tracker} # #{issue.id}"
287 287 pdf.RDMMultiCell(190, 5, buf)
288 288 pdf.Ln
289 289 pdf.SetFontStyle('',8)
290 290 base_x = pdf.GetX
291 291 i = 1
292 292 issue.ancestors.each do |ancestor|
293 293 pdf.SetX(base_x + i)
294 294 buf = "#{ancestor.tracker} # #{ancestor.id} (#{ancestor.status.to_s}): #{ancestor.subject}"
295 295 pdf.RDMMultiCell(190 - i, 5, buf)
296 296 i += 1 if i < 35
297 297 end
298 298 pdf.Ln
299 299
300 300 pdf.SetFontStyle('B',9)
301 301 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
302 302 pdf.SetFontStyle('',9)
303 303 pdf.RDMCell(60,5, issue.status.to_s,"RT")
304 304 pdf.SetFontStyle('B',9)
305 305 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
306 306 pdf.SetFontStyle('',9)
307 307 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
308 308 pdf.Ln
309 309
310 310 pdf.SetFontStyle('B',9)
311 311 pdf.RDMCell(35,5, l(:field_author) + ":","L")
312 312 pdf.SetFontStyle('',9)
313 313 pdf.RDMCell(60,5, issue.author.to_s,"R")
314 314 pdf.SetFontStyle('B',9)
315 315 pdf.RDMCell(35,5, l(:field_category) + ":","L")
316 316 pdf.SetFontStyle('',9)
317 317 pdf.RDMCell(60,5, issue.category.to_s,"R")
318 318 pdf.Ln
319 319
320 320 pdf.SetFontStyle('B',9)
321 321 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
322 322 pdf.SetFontStyle('',9)
323 323 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
324 324 pdf.SetFontStyle('B',9)
325 325 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
326 326 pdf.SetFontStyle('',9)
327 327 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
328 328 pdf.Ln
329 329
330 330 pdf.SetFontStyle('B',9)
331 331 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
332 332 pdf.SetFontStyle('',9)
333 333 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
334 334 pdf.SetFontStyle('B',9)
335 335 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
336 336 pdf.SetFontStyle('',9)
337 337 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
338 338 pdf.Ln
339 339
340 340 for custom_value in issue.custom_field_values
341 341 pdf.SetFontStyle('B',9)
342 342 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
343 343 pdf.SetFontStyle('',9)
344 344 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
345 345 end
346 346
347 347 y0 = pdf.GetY
348 348
349 349 pdf.SetFontStyle('B',9)
350 350 pdf.RDMCell(35,5, l(:field_subject) + ":","LT")
351 351 pdf.SetFontStyle('',9)
352 352 pdf.RDMMultiCell(155,5, issue.subject,"RT")
353 353 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
354 354
355 355 pdf.SetFontStyle('B',9)
356 356 pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
357 357 pdf.SetFontStyle('',9)
358 358
359 359 # Set resize image scale
360 360 pdf.SetImageScale(1.6)
361 361 pdf.RDMwriteHTMLCell(35+155, 5, 0, 0,
362 362 Redmine::WikiFormatting.to_html(
363 363 Setting.text_formatting, issue.description.to_s),"LRB")
364 364
365 unless issue.leaf?
365 366 # for CJK
366 367 truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 90 : 65 )
367 368
368 369 pdf.SetFontStyle('B',9)
369 370 pdf.RDMCell(35+155,5, l(:label_subtask_plural) + ":", "LTR")
370 371 pdf.Ln
371 372 issue_list(issue.descendants.sort_by(&:lft)) do |child, level|
372 373 buf = truncate("#{child.tracker} # #{child.id}: #{child.subject}",
373 374 :length => truncate_length)
374 375 level = 10 if level >= 10
375 376 pdf.SetFontStyle('',8)
376 377 pdf.RDMCell(35+135,5, (level >=1 ? " " * level : "") + buf, "L")
377 378 pdf.SetFontStyle('B',8)
378 379 pdf.RDMCell(20,5, child.status.to_s, "R")
379 380 pdf.Ln
380 381 end
381 pdf.SetFontStyle('B',9)
382 pdf.RDMCell(35+155,5, l(:label_related_issues) + ":", "LTR")
383 pdf.Ln
382 end
384 383
384 relations = issue.relations.select { |r| r.other_issue(issue).visible? }
385 unless relations.empty?
385 386 # for CJK
386 387 truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 80 : 60 )
387 388
388 issue.relations.select { |r| r.other_issue(issue).visible? }.each do |relation|
389 pdf.SetFontStyle('B',9)
390 pdf.RDMCell(35+155,5, l(:label_related_issues) + ":", "LTR")
391 pdf.Ln
392 relations.each do |relation|
389 393 buf = ""
390 394 buf += "#{l(relation.label_for(issue))} "
391 395 if relation.delay && relation.delay != 0
392 396 buf += "(#{l('datetime.distance_in_words.x_days', :count => relation.delay)}) "
393 397 end
394 398 if Setting.cross_project_issue_relations?
395 399 buf += "#{relation.other_issue(issue).project} - "
396 400 end
397 401 buf += "#{relation.other_issue(issue).tracker}" +
398 402 " # #{relation.other_issue(issue).id}: #{relation.other_issue(issue).subject}"
399 403 buf = truncate(buf, :length => truncate_length)
400 404 pdf.SetFontStyle('', 8)
401 405 pdf.RDMCell(35+155-50,5, buf, "L")
402 406 pdf.SetFontStyle('B',8)
403 407 pdf.RDMCell(10,5, relation.other_issue(issue).status.to_s, "")
404 408 pdf.RDMCell(20,5, format_date(relation.other_issue(issue).start_date), "")
405 409 pdf.RDMCell(20,5, format_date(relation.other_issue(issue).due_date), "R")
406 410 pdf.Ln
407 411 end
412 end
408 413 pdf.RDMCell(190,5, "", "T")
409 414 pdf.Ln
410 415
411 416 if issue.changesets.any? &&
412 417 User.current.allowed_to?(:view_changesets, issue.project)
413 418 pdf.SetFontStyle('B',9)
414 419 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
415 420 pdf.Ln
416 421 for changeset in issue.changesets
417 422 pdf.SetFontStyle('B',8)
418 423 csstr = "#{l(:label_revision)} #{changeset.format_identifier} - "
419 424 csstr += format_time(changeset.committed_on) + " - " + changeset.author.to_s
420 425 pdf.RDMCell(190, 5, csstr)
421 426 pdf.Ln
422 427 unless changeset.comments.blank?
423 428 pdf.SetFontStyle('',8)
424 429 pdf.RDMwriteHTMLCell(190,5,0,0,
425 430 Redmine::WikiFormatting.to_html(
426 431 Setting.text_formatting, changeset.comments.to_s), "")
427 432 end
428 433 pdf.Ln
429 434 end
430 435 end
431 436
432 437 pdf.SetFontStyle('B',9)
433 438 pdf.RDMCell(190,5, l(:label_history), "B")
434 439 pdf.Ln
435 440 for journal in issue.journals.find(
436 441 :all, :include => [:user, :details],
437 442 :order => "#{Journal.table_name}.created_on ASC")
438 443 pdf.SetFontStyle('B',8)
439 444 pdf.RDMCell(190,5,
440 445 format_time(journal.created_on) + " - " + journal.user.name)
441 446 pdf.Ln
442 447 pdf.SetFontStyle('I',8)
443 448 for detail in journal.details
444 449 pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true))
445 450 end
446 451 if journal.notes?
447 452 pdf.Ln unless journal.details.empty?
448 453 pdf.SetFontStyle('',8)
449 454 pdf.RDMwriteHTMLCell(190,5,0,0,
450 455 Redmine::WikiFormatting.to_html(
451 456 Setting.text_formatting, journal.notes.to_s), "")
452 457 end
453 458 pdf.Ln
454 459 end
455 460
456 461 if issue.attachments.any?
457 462 pdf.SetFontStyle('B',9)
458 463 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
459 464 pdf.Ln
460 465 for attachment in issue.attachments
461 466 pdf.SetFontStyle('',8)
462 467 pdf.RDMCell(80,5, attachment.filename)
463 468 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
464 469 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
465 470 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
466 471 pdf.Ln
467 472 end
468 473 end
469 474 pdf.Output
470 475 end
471 476
472 477 # Returns a PDF string of a single wiki page
473 478 def wiki_to_pdf(page, project)
474 479 pdf = ITCPDF.new(current_language)
475 480 pdf.SetTitle("#{project} - #{page.title}")
476 481 pdf.alias_nb_pages
477 482 pdf.footer_date = format_date(Date.today)
478 483 pdf.AddPage
479 484 pdf.SetFontStyle('B',11)
480 485 pdf.RDMMultiCell(190,5,
481 486 "#{project} - #{page.title} - # #{page.content.version}")
482 487 pdf.Ln
483 488 # Set resize image scale
484 489 pdf.SetImageScale(1.6)
485 490 pdf.SetFontStyle('',9)
486 491 pdf.RDMwriteHTMLCell(190,5,0,0,
487 492 Redmine::WikiFormatting.to_html(
488 493 Setting.text_formatting, page.content.text.to_s), "TLRB")
489 494 if page.attachments.any?
490 495 pdf.Ln
491 496 pdf.SetFontStyle('B',9)
492 497 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
493 498 pdf.Ln
494 499 for attachment in page.attachments
495 500 pdf.SetFontStyle('',8)
496 501 pdf.RDMCell(80,5, attachment.filename)
497 502 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
498 503 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
499 504 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
500 505 pdf.Ln
501 506 end
502 507 end
503 508 pdf.Output
504 509 end
505 510
506 511 class RDMPdfEncoding
507 512 include Redmine::I18n
508 513 def self.rdm_from_utf8(txt, encoding)
509 514 txt ||= ''
510 515 txt = Redmine::CodesetUtil.from_utf8(txt, encoding)
511 516 if txt.respond_to?(:force_encoding)
512 517 txt.force_encoding('ASCII-8BIT')
513 518 end
514 519 txt
515 520 end
516 521 end
517 522 end
518 523 end
519 524 end
General Comments 0
You need to be logged in to leave comments. Login now