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