annotate .svn/pristine/aa/aa6f4c5dea2bc16ad3aaedef8479df833e7780fa.svn-base @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents
children
rev   line source
Chris@1517 1 # encoding: utf-8
Chris@1517 2 #
Chris@1517 3 # Redmine - project management software
Chris@1517 4 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 5 #
Chris@1517 6 # This program is free software; you can redistribute it and/or
Chris@1517 7 # modify it under the terms of the GNU General Public License
Chris@1517 8 # as published by the Free Software Foundation; either version 2
Chris@1517 9 # of the License, or (at your option) any later version.
Chris@1517 10 #
Chris@1517 11 # This program is distributed in the hope that it will be useful,
Chris@1517 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 14 # GNU General Public License for more details.
Chris@1517 15 #
Chris@1517 16 # You should have received a copy of the GNU General Public License
Chris@1517 17 # along with this program; if not, write to the Free Software
Chris@1517 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 19
Chris@1517 20 require 'tcpdf'
Chris@1517 21 require 'fpdf/chinese'
Chris@1517 22 require 'fpdf/japanese'
Chris@1517 23 require 'fpdf/korean'
Chris@1517 24
Chris@1517 25 if RUBY_VERSION < '1.9'
Chris@1517 26 require 'iconv'
Chris@1517 27 end
Chris@1517 28
Chris@1517 29 module Redmine
Chris@1517 30 module Export
Chris@1517 31 module PDF
Chris@1517 32 include ActionView::Helpers::TextHelper
Chris@1517 33 include ActionView::Helpers::NumberHelper
Chris@1517 34 include IssuesHelper
Chris@1517 35
Chris@1517 36 class ITCPDF < TCPDF
Chris@1517 37 include Redmine::I18n
Chris@1517 38 attr_accessor :footer_date
Chris@1517 39
Chris@1517 40 def initialize(lang, orientation='P')
Chris@1517 41 @@k_path_cache = Rails.root.join('tmp', 'pdf')
Chris@1517 42 FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache)
Chris@1517 43 set_language_if_valid lang
Chris@1517 44 pdf_encoding = l(:general_pdf_encoding).upcase
Chris@1517 45 super(orientation, 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding)
Chris@1517 46 case current_language.to_s.downcase
Chris@1517 47 when 'vi'
Chris@1517 48 @font_for_content = 'DejaVuSans'
Chris@1517 49 @font_for_footer = 'DejaVuSans'
Chris@1517 50 else
Chris@1517 51 case pdf_encoding
Chris@1517 52 when 'UTF-8'
Chris@1517 53 @font_for_content = 'FreeSans'
Chris@1517 54 @font_for_footer = 'FreeSans'
Chris@1517 55 when 'CP949'
Chris@1517 56 extend(PDF_Korean)
Chris@1517 57 AddUHCFont()
Chris@1517 58 @font_for_content = 'UHC'
Chris@1517 59 @font_for_footer = 'UHC'
Chris@1517 60 when 'CP932', 'SJIS', 'SHIFT_JIS'
Chris@1517 61 extend(PDF_Japanese)
Chris@1517 62 AddSJISFont()
Chris@1517 63 @font_for_content = 'SJIS'
Chris@1517 64 @font_for_footer = 'SJIS'
Chris@1517 65 when 'GB18030'
Chris@1517 66 extend(PDF_Chinese)
Chris@1517 67 AddGBFont()
Chris@1517 68 @font_for_content = 'GB'
Chris@1517 69 @font_for_footer = 'GB'
Chris@1517 70 when 'BIG5'
Chris@1517 71 extend(PDF_Chinese)
Chris@1517 72 AddBig5Font()
Chris@1517 73 @font_for_content = 'Big5'
Chris@1517 74 @font_for_footer = 'Big5'
Chris@1517 75 else
Chris@1517 76 @font_for_content = 'Arial'
Chris@1517 77 @font_for_footer = 'Helvetica'
Chris@1517 78 end
Chris@1517 79 end
Chris@1517 80 SetCreator(Redmine::Info.app_name)
Chris@1517 81 SetFont(@font_for_content)
Chris@1517 82 @outlines = []
Chris@1517 83 @outlineRoot = nil
Chris@1517 84 end
Chris@1517 85
Chris@1517 86 def SetFontStyle(style, size)
Chris@1517 87 SetFont(@font_for_content, style, size)
Chris@1517 88 end
Chris@1517 89
Chris@1517 90 def SetTitle(txt)
Chris@1517 91 txt = begin
Chris@1517 92 utf16txt = to_utf16(txt)
Chris@1517 93 hextxt = "<FEFF" # FEFF is BOM
Chris@1517 94 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
Chris@1517 95 hextxt << ">"
Chris@1517 96 rescue
Chris@1517 97 txt
Chris@1517 98 end || ''
Chris@1517 99 super(txt)
Chris@1517 100 end
Chris@1517 101
Chris@1517 102 def textstring(s)
Chris@1517 103 # Format a text string
Chris@1517 104 if s =~ /^</ # This means the string is hex-dumped.
Chris@1517 105 return s
Chris@1517 106 else
Chris@1517 107 return '('+escape(s)+')'
Chris@1517 108 end
Chris@1517 109 end
Chris@1517 110
Chris@1517 111 def fix_text_encoding(txt)
Chris@1517 112 RDMPdfEncoding::rdm_from_utf8(txt, l(:general_pdf_encoding))
Chris@1517 113 end
Chris@1517 114
Chris@1517 115 def formatted_text(text)
Chris@1517 116 html = Redmine::WikiFormatting.to_html(Setting.text_formatting, text)
Chris@1517 117 # Strip {{toc}} tags
Chris@1517 118 html.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i, '')
Chris@1517 119 html
Chris@1517 120 end
Chris@1517 121
Chris@1517 122 # Encodes an UTF-8 string to UTF-16BE
Chris@1517 123 def to_utf16(str)
Chris@1517 124 if str.respond_to?(:encode)
Chris@1517 125 str.encode('UTF-16BE')
Chris@1517 126 else
Chris@1517 127 Iconv.conv('UTF-16BE', 'UTF-8', str)
Chris@1517 128 end
Chris@1517 129 end
Chris@1517 130
Chris@1517 131 def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='')
Chris@1517 132 Cell(w, h, fix_text_encoding(txt), border, ln, align, fill, link)
Chris@1517 133 end
Chris@1517 134
Chris@1517 135 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
Chris@1517 136 MultiCell(w, h, fix_text_encoding(txt), border, align, fill, ln)
Chris@1517 137 end
Chris@1517 138
Chris@1517 139 def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
Chris@1517 140 @attachments = attachments
Chris@1517 141 writeHTMLCell(w, h, x, y,
Chris@1517 142 fix_text_encoding(formatted_text(txt)),
Chris@1517 143 border, ln, fill)
Chris@1517 144 end
Chris@1517 145
Chris@1517 146 def getImageFilename(attrname)
Chris@1517 147 # attrname: general_pdf_encoding string file/uri name
Chris@1517 148 atta = RDMPdfEncoding.attach(@attachments, attrname, l(:general_pdf_encoding))
Chris@1517 149 if atta
Chris@1517 150 return atta.diskfile
Chris@1517 151 else
Chris@1517 152 return nil
Chris@1517 153 end
Chris@1517 154 end
Chris@1517 155
Chris@1517 156 def Footer
Chris@1517 157 SetFont(@font_for_footer, 'I', 8)
Chris@1517 158 SetY(-15)
Chris@1517 159 SetX(15)
Chris@1517 160 RDMCell(0, 5, @footer_date, 0, 0, 'L')
Chris@1517 161 SetY(-15)
Chris@1517 162 SetX(-30)
Chris@1517 163 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
Chris@1517 164 end
Chris@1517 165
Chris@1517 166 def Bookmark(txt, level=0, y=0)
Chris@1517 167 if (y == -1)
Chris@1517 168 y = GetY()
Chris@1517 169 end
Chris@1517 170 @outlines << {:t => txt, :l => level, :p => PageNo(), :y => (@h - y)*@k}
Chris@1517 171 end
Chris@1517 172
Chris@1517 173 def bookmark_title(txt)
Chris@1517 174 txt = begin
Chris@1517 175 utf16txt = to_utf16(txt)
Chris@1517 176 hextxt = "<FEFF" # FEFF is BOM
Chris@1517 177 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
Chris@1517 178 hextxt << ">"
Chris@1517 179 rescue
Chris@1517 180 txt
Chris@1517 181 end || ''
Chris@1517 182 end
Chris@1517 183
Chris@1517 184 def putbookmarks
Chris@1517 185 nb=@outlines.size
Chris@1517 186 return if (nb==0)
Chris@1517 187 lru=[]
Chris@1517 188 level=0
Chris@1517 189 @outlines.each_with_index do |o, i|
Chris@1517 190 if(o[:l]>0)
Chris@1517 191 parent=lru[o[:l]-1]
Chris@1517 192 #Set parent and last pointers
Chris@1517 193 @outlines[i][:parent]=parent
Chris@1517 194 @outlines[parent][:last]=i
Chris@1517 195 if (o[:l]>level)
Chris@1517 196 #Level increasing: set first pointer
Chris@1517 197 @outlines[parent][:first]=i
Chris@1517 198 end
Chris@1517 199 else
Chris@1517 200 @outlines[i][:parent]=nb
Chris@1517 201 end
Chris@1517 202 if (o[:l]<=level && i>0)
Chris@1517 203 #Set prev and next pointers
Chris@1517 204 prev=lru[o[:l]]
Chris@1517 205 @outlines[prev][:next]=i
Chris@1517 206 @outlines[i][:prev]=prev
Chris@1517 207 end
Chris@1517 208 lru[o[:l]]=i
Chris@1517 209 level=o[:l]
Chris@1517 210 end
Chris@1517 211 #Outline items
Chris@1517 212 n=self.n+1
Chris@1517 213 @outlines.each_with_index do |o, i|
Chris@1517 214 newobj()
Chris@1517 215 out('<</Title '+bookmark_title(o[:t]))
Chris@1517 216 out("/Parent #{n+o[:parent]} 0 R")
Chris@1517 217 if (o[:prev])
Chris@1517 218 out("/Prev #{n+o[:prev]} 0 R")
Chris@1517 219 end
Chris@1517 220 if (o[:next])
Chris@1517 221 out("/Next #{n+o[:next]} 0 R")
Chris@1517 222 end
Chris@1517 223 if (o[:first])
Chris@1517 224 out("/First #{n+o[:first]} 0 R")
Chris@1517 225 end
Chris@1517 226 if (o[:last])
Chris@1517 227 out("/Last #{n+o[:last]} 0 R")
Chris@1517 228 end
Chris@1517 229 out("/Dest [%d 0 R /XYZ 0 %.2f null]" % [1+2*o[:p], o[:y]])
Chris@1517 230 out('/Count 0>>')
Chris@1517 231 out('endobj')
Chris@1517 232 end
Chris@1517 233 #Outline root
Chris@1517 234 newobj()
Chris@1517 235 @outlineRoot=self.n
Chris@1517 236 out("<</Type /Outlines /First #{n} 0 R");
Chris@1517 237 out("/Last #{n+lru[0]} 0 R>>");
Chris@1517 238 out('endobj');
Chris@1517 239 end
Chris@1517 240
Chris@1517 241 def putresources()
Chris@1517 242 super
Chris@1517 243 putbookmarks()
Chris@1517 244 end
Chris@1517 245
Chris@1517 246 def putcatalog()
Chris@1517 247 super
Chris@1517 248 if(@outlines.size > 0)
Chris@1517 249 out("/Outlines #{@outlineRoot} 0 R");
Chris@1517 250 out('/PageMode /UseOutlines');
Chris@1517 251 end
Chris@1517 252 end
Chris@1517 253 end
Chris@1517 254
Chris@1517 255 # fetch row values
Chris@1517 256 def fetch_row_values(issue, query, level)
Chris@1517 257 query.inline_columns.collect do |column|
Chris@1517 258 s = if column.is_a?(QueryCustomFieldColumn)
Chris@1517 259 cv = issue.visible_custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
Chris@1517 260 show_value(cv, false)
Chris@1517 261 else
Chris@1517 262 value = issue.send(column.name)
Chris@1517 263 if column.name == :subject
Chris@1517 264 value = " " * level + value
Chris@1517 265 end
Chris@1517 266 if value.is_a?(Date)
Chris@1517 267 format_date(value)
Chris@1517 268 elsif value.is_a?(Time)
Chris@1517 269 format_time(value)
Chris@1517 270 else
Chris@1517 271 value
Chris@1517 272 end
Chris@1517 273 end
Chris@1517 274 s.to_s
Chris@1517 275 end
Chris@1517 276 end
Chris@1517 277
Chris@1517 278 # calculate columns width
Chris@1517 279 def calc_col_width(issues, query, table_width, pdf)
Chris@1517 280 # calculate statistics
Chris@1517 281 # by captions
Chris@1517 282 pdf.SetFontStyle('B',8)
Chris@1517 283 col_padding = pdf.GetStringWidth('OO')
Chris@1517 284 col_width_min = query.inline_columns.map {|v| pdf.GetStringWidth(v.caption) + col_padding}
Chris@1517 285 col_width_max = Array.new(col_width_min)
Chris@1517 286 col_width_avg = Array.new(col_width_min)
Chris@1517 287 word_width_max = query.inline_columns.map {|c|
Chris@1517 288 n = 10
Chris@1517 289 c.caption.split.each {|w|
Chris@1517 290 x = pdf.GetStringWidth(w) + col_padding
Chris@1517 291 n = x if n < x
Chris@1517 292 }
Chris@1517 293 n
Chris@1517 294 }
Chris@1517 295
Chris@1517 296 # by properties of issues
Chris@1517 297 pdf.SetFontStyle('',8)
Chris@1517 298 col_padding = pdf.GetStringWidth('OO')
Chris@1517 299 k = 1
Chris@1517 300 issue_list(issues) {|issue, level|
Chris@1517 301 k += 1
Chris@1517 302 values = fetch_row_values(issue, query, level)
Chris@1517 303 values.each_with_index {|v,i|
Chris@1517 304 n = pdf.GetStringWidth(v) + col_padding
Chris@1517 305 col_width_max[i] = n if col_width_max[i] < n
Chris@1517 306 col_width_min[i] = n if col_width_min[i] > n
Chris@1517 307 col_width_avg[i] += n
Chris@1517 308 v.split.each {|w|
Chris@1517 309 x = pdf.GetStringWidth(w) + col_padding
Chris@1517 310 word_width_max[i] = x if word_width_max[i] < x
Chris@1517 311 }
Chris@1517 312 }
Chris@1517 313 }
Chris@1517 314 col_width_avg.map! {|x| x / k}
Chris@1517 315
Chris@1517 316 # calculate columns width
Chris@1517 317 ratio = table_width / col_width_avg.inject(0, :+)
Chris@1517 318 col_width = col_width_avg.map {|w| w * ratio}
Chris@1517 319
Chris@1517 320 # correct max word width if too many columns
Chris@1517 321 ratio = table_width / word_width_max.inject(0, :+)
Chris@1517 322 word_width_max.map! {|v| v * ratio} if ratio < 1
Chris@1517 323
Chris@1517 324 # correct and lock width of some columns
Chris@1517 325 done = 1
Chris@1517 326 col_fix = []
Chris@1517 327 col_width.each_with_index do |w,i|
Chris@1517 328 if w > col_width_max[i]
Chris@1517 329 col_width[i] = col_width_max[i]
Chris@1517 330 col_fix[i] = 1
Chris@1517 331 done = 0
Chris@1517 332 elsif w < word_width_max[i]
Chris@1517 333 col_width[i] = word_width_max[i]
Chris@1517 334 col_fix[i] = 1
Chris@1517 335 done = 0
Chris@1517 336 else
Chris@1517 337 col_fix[i] = 0
Chris@1517 338 end
Chris@1517 339 end
Chris@1517 340
Chris@1517 341 # iterate while need to correct and lock coluns width
Chris@1517 342 while done == 0
Chris@1517 343 # calculate free & locked columns width
Chris@1517 344 done = 1
Chris@1517 345 fix_col_width = 0
Chris@1517 346 free_col_width = 0
Chris@1517 347 col_width.each_with_index do |w,i|
Chris@1517 348 if col_fix[i] == 1
Chris@1517 349 fix_col_width += w
Chris@1517 350 else
Chris@1517 351 free_col_width += w
Chris@1517 352 end
Chris@1517 353 end
Chris@1517 354
Chris@1517 355 # calculate column normalizing ratio
Chris@1517 356 if free_col_width == 0
Chris@1517 357 ratio = table_width / col_width.inject(0, :+)
Chris@1517 358 else
Chris@1517 359 ratio = (table_width - fix_col_width) / free_col_width
Chris@1517 360 end
Chris@1517 361
Chris@1517 362 # correct columns width
Chris@1517 363 col_width.each_with_index do |w,i|
Chris@1517 364 if col_fix[i] == 0
Chris@1517 365 col_width[i] = w * ratio
Chris@1517 366
Chris@1517 367 # check if column width less then max word width
Chris@1517 368 if col_width[i] < word_width_max[i]
Chris@1517 369 col_width[i] = word_width_max[i]
Chris@1517 370 col_fix[i] = 1
Chris@1517 371 done = 0
Chris@1517 372 elsif col_width[i] > col_width_max[i]
Chris@1517 373 col_width[i] = col_width_max[i]
Chris@1517 374 col_fix[i] = 1
Chris@1517 375 done = 0
Chris@1517 376 end
Chris@1517 377 end
Chris@1517 378 end
Chris@1517 379 end
Chris@1517 380 col_width
Chris@1517 381 end
Chris@1517 382
Chris@1517 383 def render_table_header(pdf, query, col_width, row_height, table_width)
Chris@1517 384 # headers
Chris@1517 385 pdf.SetFontStyle('B',8)
Chris@1517 386 pdf.SetFillColor(230, 230, 230)
Chris@1517 387
Chris@1517 388 # render it background to find the max height used
Chris@1517 389 base_x = pdf.GetX
Chris@1517 390 base_y = pdf.GetY
Chris@1517 391 max_height = issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, row_height, true)
Chris@1517 392 pdf.Rect(base_x, base_y, table_width, max_height, 'FD');
Chris@1517 393 pdf.SetXY(base_x, base_y);
Chris@1517 394
Chris@1517 395 # write the cells on page
Chris@1517 396 issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, row_height, true)
Chris@1517 397 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, 0, col_width)
Chris@1517 398 pdf.SetY(base_y + max_height);
Chris@1517 399
Chris@1517 400 # rows
Chris@1517 401 pdf.SetFontStyle('',8)
Chris@1517 402 pdf.SetFillColor(255, 255, 255)
Chris@1517 403 end
Chris@1517 404
Chris@1517 405 # Returns a PDF string of a list of issues
Chris@1517 406 def issues_to_pdf(issues, project, query)
Chris@1517 407 pdf = ITCPDF.new(current_language, "L")
Chris@1517 408 title = query.new_record? ? l(:label_issue_plural) : query.name
Chris@1517 409 title = "#{project} - #{title}" if project
Chris@1517 410 pdf.SetTitle(title)
Chris@1517 411 pdf.alias_nb_pages
Chris@1517 412 pdf.footer_date = format_date(Date.today)
Chris@1517 413 pdf.SetAutoPageBreak(false)
Chris@1517 414 pdf.AddPage("L")
Chris@1517 415
Chris@1517 416 # Landscape A4 = 210 x 297 mm
Chris@1517 417 page_height = 210
Chris@1517 418 page_width = 297
Chris@1517 419 left_margin = 10
Chris@1517 420 right_margin = 10
Chris@1517 421 bottom_margin = 20
Chris@1517 422 row_height = 4
Chris@1517 423
Chris@1517 424 # column widths
Chris@1517 425 table_width = page_width - right_margin - left_margin
Chris@1517 426 col_width = []
Chris@1517 427 unless query.inline_columns.empty?
Chris@1517 428 col_width = calc_col_width(issues, query, table_width, pdf)
Chris@1517 429 table_width = col_width.inject(0, :+)
Chris@1517 430 end
Chris@1517 431
Chris@1517 432 # use full width if the description is displayed
Chris@1517 433 if table_width > 0 && query.has_column?(:description)
Chris@1517 434 col_width = col_width.map {|w| w * (page_width - right_margin - left_margin) / table_width}
Chris@1517 435 table_width = col_width.inject(0, :+)
Chris@1517 436 end
Chris@1517 437
Chris@1517 438 # title
Chris@1517 439 pdf.SetFontStyle('B',11)
Chris@1517 440 pdf.RDMCell(190,10, title)
Chris@1517 441 pdf.Ln
Chris@1517 442 render_table_header(pdf, query, col_width, row_height, table_width)
Chris@1517 443 previous_group = false
Chris@1517 444 issue_list(issues) do |issue, level|
Chris@1517 445 if query.grouped? &&
Chris@1517 446 (group = query.group_by_column.value(issue)) != previous_group
Chris@1517 447 pdf.SetFontStyle('B',10)
Chris@1517 448 group_label = group.blank? ? 'None' : group.to_s.dup
Chris@1517 449 group_label << " (#{query.issue_count_by_group[group]})"
Chris@1517 450 pdf.Bookmark group_label, 0, -1
Chris@1517 451 pdf.RDMCell(table_width, row_height * 2, group_label, 1, 1, 'L')
Chris@1517 452 pdf.SetFontStyle('',8)
Chris@1517 453 previous_group = group
Chris@1517 454 end
Chris@1517 455
Chris@1517 456 # fetch row values
Chris@1517 457 col_values = fetch_row_values(issue, query, level)
Chris@1517 458
Chris@1517 459 # render it off-page to find the max height used
Chris@1517 460 base_x = pdf.GetX
Chris@1517 461 base_y = pdf.GetY
Chris@1517 462 pdf.SetY(2 * page_height)
Chris@1517 463 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
Chris@1517 464 pdf.SetXY(base_x, base_y)
Chris@1517 465
Chris@1517 466 # make new page if it doesn't fit on the current one
Chris@1517 467 space_left = page_height - base_y - bottom_margin
Chris@1517 468 if max_height > space_left
Chris@1517 469 pdf.AddPage("L")
Chris@1517 470 render_table_header(pdf, query, col_width, row_height, table_width)
Chris@1517 471 base_x = pdf.GetX
Chris@1517 472 base_y = pdf.GetY
Chris@1517 473 end
Chris@1517 474
Chris@1517 475 # write the cells on page
Chris@1517 476 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
Chris@1517 477 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, 0, col_width)
Chris@1517 478 pdf.SetY(base_y + max_height);
Chris@1517 479
Chris@1517 480 if query.has_column?(:description) && issue.description?
Chris@1517 481 pdf.SetX(10)
Chris@1517 482 pdf.SetAutoPageBreak(true, 20)
Chris@1517 483 pdf.RDMwriteHTMLCell(0, 5, 10, 0, issue.description.to_s, issue.attachments, "LRBT")
Chris@1517 484 pdf.SetAutoPageBreak(false)
Chris@1517 485 end
Chris@1517 486 end
Chris@1517 487
Chris@1517 488 if issues.size == Setting.issues_export_limit.to_i
Chris@1517 489 pdf.SetFontStyle('B',10)
Chris@1517 490 pdf.RDMCell(0, row_height, '...')
Chris@1517 491 end
Chris@1517 492 pdf.Output
Chris@1517 493 end
Chris@1517 494
Chris@1517 495 # Renders MultiCells and returns the maximum height used
Chris@1517 496 def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height, head=false)
Chris@1517 497 base_y = pdf.GetY
Chris@1517 498 max_height = row_height
Chris@1517 499 col_values.each_with_index do |column, i|
Chris@1517 500 col_x = pdf.GetX
Chris@1517 501 if head == true
Chris@1517 502 pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1)
Chris@1517 503 else
Chris@1517 504 pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1)
Chris@1517 505 end
Chris@1517 506 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
Chris@1517 507 pdf.SetXY(col_x + col_widths[i], base_y);
Chris@1517 508 end
Chris@1517 509 return max_height
Chris@1517 510 end
Chris@1517 511
Chris@1517 512 # Draw lines to close the row (MultiCell border drawing in not uniform)
Chris@1517 513 #
Chris@1517 514 # parameter "col_id_width" is not used. it is kept for compatibility.
Chris@1517 515 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y,
Chris@1517 516 col_id_width, col_widths)
Chris@1517 517 col_x = top_x
Chris@1517 518 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
Chris@1517 519 col_widths.each do |width|
Chris@1517 520 col_x += width
Chris@1517 521 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
Chris@1517 522 end
Chris@1517 523 pdf.Line(top_x, top_y, top_x, lower_y) # left border
Chris@1517 524 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
Chris@1517 525 end
Chris@1517 526
Chris@1517 527 # Returns a PDF string of a single issue
Chris@1517 528 def issue_to_pdf(issue, assoc={})
Chris@1517 529 pdf = ITCPDF.new(current_language)
Chris@1517 530 pdf.SetTitle("#{issue.project} - #{issue.tracker} ##{issue.id}")
Chris@1517 531 pdf.alias_nb_pages
Chris@1517 532 pdf.footer_date = format_date(Date.today)
Chris@1517 533 pdf.AddPage
Chris@1517 534 pdf.SetFontStyle('B',11)
Chris@1517 535 buf = "#{issue.project} - #{issue.tracker} ##{issue.id}"
Chris@1517 536 pdf.RDMMultiCell(190, 5, buf)
Chris@1517 537 pdf.SetFontStyle('',8)
Chris@1517 538 base_x = pdf.GetX
Chris@1517 539 i = 1
Chris@1517 540 issue.ancestors.visible.each do |ancestor|
Chris@1517 541 pdf.SetX(base_x + i)
Chris@1517 542 buf = "#{ancestor.tracker} # #{ancestor.id} (#{ancestor.status.to_s}): #{ancestor.subject}"
Chris@1517 543 pdf.RDMMultiCell(190 - i, 5, buf)
Chris@1517 544 i += 1 if i < 35
Chris@1517 545 end
Chris@1517 546 pdf.SetFontStyle('B',11)
Chris@1517 547 pdf.RDMMultiCell(190 - i, 5, issue.subject.to_s)
Chris@1517 548 pdf.SetFontStyle('',8)
Chris@1517 549 pdf.RDMMultiCell(190, 5, "#{format_time(issue.created_on)} - #{issue.author}")
Chris@1517 550 pdf.Ln
Chris@1517 551
Chris@1517 552 left = []
Chris@1517 553 left << [l(:field_status), issue.status]
Chris@1517 554 left << [l(:field_priority), issue.priority]
Chris@1517 555 left << [l(:field_assigned_to), issue.assigned_to] unless issue.disabled_core_fields.include?('assigned_to_id')
Chris@1517 556 left << [l(:field_category), issue.category] unless issue.disabled_core_fields.include?('category_id')
Chris@1517 557 left << [l(:field_fixed_version), issue.fixed_version] unless issue.disabled_core_fields.include?('fixed_version_id')
Chris@1517 558
Chris@1517 559 right = []
Chris@1517 560 right << [l(:field_start_date), format_date(issue.start_date)] unless issue.disabled_core_fields.include?('start_date')
Chris@1517 561 right << [l(:field_due_date), format_date(issue.due_date)] unless issue.disabled_core_fields.include?('due_date')
Chris@1517 562 right << [l(:field_done_ratio), "#{issue.done_ratio}%"] unless issue.disabled_core_fields.include?('done_ratio')
Chris@1517 563 right << [l(:field_estimated_hours), l_hours(issue.estimated_hours)] unless issue.disabled_core_fields.include?('estimated_hours')
Chris@1517 564 right << [l(:label_spent_time), l_hours(issue.total_spent_hours)] if User.current.allowed_to?(:view_time_entries, issue.project)
Chris@1517 565
Chris@1517 566 rows = left.size > right.size ? left.size : right.size
Chris@1517 567 while left.size < rows
Chris@1517 568 left << nil
Chris@1517 569 end
Chris@1517 570 while right.size < rows
Chris@1517 571 right << nil
Chris@1517 572 end
Chris@1517 573
Chris@1517 574 half = (issue.visible_custom_field_values.size / 2.0).ceil
Chris@1517 575 issue.visible_custom_field_values.each_with_index do |custom_value, i|
Chris@1517 576 (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value, false)]
Chris@1517 577 end
Chris@1517 578
Chris@1517 579 rows = left.size > right.size ? left.size : right.size
Chris@1517 580 rows.times do |i|
Chris@1517 581 item = left[i]
Chris@1517 582 pdf.SetFontStyle('B',9)
Chris@1517 583 pdf.RDMCell(35,5, item ? "#{item.first}:" : "", i == 0 ? "LT" : "L")
Chris@1517 584 pdf.SetFontStyle('',9)
Chris@1517 585 pdf.RDMCell(60,5, item ? item.last.to_s : "", i == 0 ? "RT" : "R")
Chris@1517 586
Chris@1517 587 item = right[i]
Chris@1517 588 pdf.SetFontStyle('B',9)
Chris@1517 589 pdf.RDMCell(35,5, item ? "#{item.first}:" : "", i == 0 ? "LT" : "L")
Chris@1517 590 pdf.SetFontStyle('',9)
Chris@1517 591 pdf.RDMCell(60,5, item ? item.last.to_s : "", i == 0 ? "RT" : "R")
Chris@1517 592 pdf.Ln
Chris@1517 593 end
Chris@1517 594
Chris@1517 595 pdf.SetFontStyle('B',9)
Chris@1517 596 pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
Chris@1517 597 pdf.SetFontStyle('',9)
Chris@1517 598
Chris@1517 599 # Set resize image scale
Chris@1517 600 pdf.SetImageScale(1.6)
Chris@1517 601 pdf.RDMwriteHTMLCell(35+155, 5, 0, 0,
Chris@1517 602 issue.description.to_s, issue.attachments, "LRB")
Chris@1517 603
Chris@1517 604 unless issue.leaf?
Chris@1517 605 # for CJK
Chris@1517 606 truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 90 : 65 )
Chris@1517 607 pdf.SetFontStyle('B',9)
Chris@1517 608 pdf.RDMCell(35+155,5, l(:label_subtask_plural) + ":", "LTR")
Chris@1517 609 pdf.Ln
Chris@1517 610 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
Chris@1517 611 buf = "#{child.tracker} # #{child.id}: #{child.subject}".
Chris@1517 612 truncate(truncate_length)
Chris@1517 613 level = 10 if level >= 10
Chris@1517 614 pdf.SetFontStyle('',8)
Chris@1517 615 pdf.RDMCell(35+135,5, (level >=1 ? " " * level : "") + buf, "L")
Chris@1517 616 pdf.SetFontStyle('B',8)
Chris@1517 617 pdf.RDMCell(20,5, child.status.to_s, "R")
Chris@1517 618 pdf.Ln
Chris@1517 619 end
Chris@1517 620 end
Chris@1517 621
Chris@1517 622 relations = issue.relations.select { |r| r.other_issue(issue).visible? }
Chris@1517 623 unless relations.empty?
Chris@1517 624 # for CJK
Chris@1517 625 truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 80 : 60 )
Chris@1517 626 pdf.SetFontStyle('B',9)
Chris@1517 627 pdf.RDMCell(35+155,5, l(:label_related_issues) + ":", "LTR")
Chris@1517 628 pdf.Ln
Chris@1517 629 relations.each do |relation|
Chris@1517 630 buf = ""
Chris@1517 631 buf += "#{l(relation.label_for(issue))} "
Chris@1517 632 if relation.delay && relation.delay != 0
Chris@1517 633 buf += "(#{l('datetime.distance_in_words.x_days', :count => relation.delay)}) "
Chris@1517 634 end
Chris@1517 635 if Setting.cross_project_issue_relations?
Chris@1517 636 buf += "#{relation.other_issue(issue).project} - "
Chris@1517 637 end
Chris@1517 638 buf += "#{relation.other_issue(issue).tracker}" +
Chris@1517 639 " # #{relation.other_issue(issue).id}: #{relation.other_issue(issue).subject}"
Chris@1517 640 buf = buf.truncate(truncate_length)
Chris@1517 641 pdf.SetFontStyle('', 8)
Chris@1517 642 pdf.RDMCell(35+155-60, 5, buf, "L")
Chris@1517 643 pdf.SetFontStyle('B',8)
Chris@1517 644 pdf.RDMCell(20,5, relation.other_issue(issue).status.to_s, "")
Chris@1517 645 pdf.RDMCell(20,5, format_date(relation.other_issue(issue).start_date), "")
Chris@1517 646 pdf.RDMCell(20,5, format_date(relation.other_issue(issue).due_date), "R")
Chris@1517 647 pdf.Ln
Chris@1517 648 end
Chris@1517 649 end
Chris@1517 650 pdf.RDMCell(190,5, "", "T")
Chris@1517 651 pdf.Ln
Chris@1517 652
Chris@1517 653 if issue.changesets.any? &&
Chris@1517 654 User.current.allowed_to?(:view_changesets, issue.project)
Chris@1517 655 pdf.SetFontStyle('B',9)
Chris@1517 656 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
Chris@1517 657 pdf.Ln
Chris@1517 658 for changeset in issue.changesets
Chris@1517 659 pdf.SetFontStyle('B',8)
Chris@1517 660 csstr = "#{l(:label_revision)} #{changeset.format_identifier} - "
Chris@1517 661 csstr += format_time(changeset.committed_on) + " - " + changeset.author.to_s
Chris@1517 662 pdf.RDMCell(190, 5, csstr)
Chris@1517 663 pdf.Ln
Chris@1517 664 unless changeset.comments.blank?
Chris@1517 665 pdf.SetFontStyle('',8)
Chris@1517 666 pdf.RDMwriteHTMLCell(190,5,0,0,
Chris@1517 667 changeset.comments.to_s, issue.attachments, "")
Chris@1517 668 end
Chris@1517 669 pdf.Ln
Chris@1517 670 end
Chris@1517 671 end
Chris@1517 672
Chris@1517 673 if assoc[:journals].present?
Chris@1517 674 pdf.SetFontStyle('B',9)
Chris@1517 675 pdf.RDMCell(190,5, l(:label_history), "B")
Chris@1517 676 pdf.Ln
Chris@1517 677 assoc[:journals].each do |journal|
Chris@1517 678 pdf.SetFontStyle('B',8)
Chris@1517 679 title = "##{journal.indice} - #{format_time(journal.created_on)} - #{journal.user}"
Chris@1517 680 title << " (#{l(:field_private_notes)})" if journal.private_notes?
Chris@1517 681 pdf.RDMCell(190,5, title)
Chris@1517 682 pdf.Ln
Chris@1517 683 pdf.SetFontStyle('I',8)
Chris@1517 684 details_to_strings(journal.visible_details, true).each do |string|
Chris@1517 685 pdf.RDMMultiCell(190,5, "- " + string)
Chris@1517 686 end
Chris@1517 687 if journal.notes?
Chris@1517 688 pdf.Ln unless journal.details.empty?
Chris@1517 689 pdf.SetFontStyle('',8)
Chris@1517 690 pdf.RDMwriteHTMLCell(190,5,0,0,
Chris@1517 691 journal.notes.to_s, issue.attachments, "")
Chris@1517 692 end
Chris@1517 693 pdf.Ln
Chris@1517 694 end
Chris@1517 695 end
Chris@1517 696
Chris@1517 697 if issue.attachments.any?
Chris@1517 698 pdf.SetFontStyle('B',9)
Chris@1517 699 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
Chris@1517 700 pdf.Ln
Chris@1517 701 for attachment in issue.attachments
Chris@1517 702 pdf.SetFontStyle('',8)
Chris@1517 703 pdf.RDMCell(80,5, attachment.filename)
Chris@1517 704 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
Chris@1517 705 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
Chris@1517 706 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
Chris@1517 707 pdf.Ln
Chris@1517 708 end
Chris@1517 709 end
Chris@1517 710 pdf.Output
Chris@1517 711 end
Chris@1517 712
Chris@1517 713 # Returns a PDF string of a set of wiki pages
Chris@1517 714 def wiki_pages_to_pdf(pages, project)
Chris@1517 715 pdf = ITCPDF.new(current_language)
Chris@1517 716 pdf.SetTitle(project.name)
Chris@1517 717 pdf.alias_nb_pages
Chris@1517 718 pdf.footer_date = format_date(Date.today)
Chris@1517 719 pdf.AddPage
Chris@1517 720 pdf.SetFontStyle('B',11)
Chris@1517 721 pdf.RDMMultiCell(190,5, project.name)
Chris@1517 722 pdf.Ln
Chris@1517 723 # Set resize image scale
Chris@1517 724 pdf.SetImageScale(1.6)
Chris@1517 725 pdf.SetFontStyle('',9)
Chris@1517 726 write_page_hierarchy(pdf, pages.group_by(&:parent_id))
Chris@1517 727 pdf.Output
Chris@1517 728 end
Chris@1517 729
Chris@1517 730 # Returns a PDF string of a single wiki page
Chris@1517 731 def wiki_page_to_pdf(page, project)
Chris@1517 732 pdf = ITCPDF.new(current_language)
Chris@1517 733 pdf.SetTitle("#{project} - #{page.title}")
Chris@1517 734 pdf.alias_nb_pages
Chris@1517 735 pdf.footer_date = format_date(Date.today)
Chris@1517 736 pdf.AddPage
Chris@1517 737 pdf.SetFontStyle('B',11)
Chris@1517 738 pdf.RDMMultiCell(190,5,
Chris@1517 739 "#{project} - #{page.title} - # #{page.content.version}")
Chris@1517 740 pdf.Ln
Chris@1517 741 # Set resize image scale
Chris@1517 742 pdf.SetImageScale(1.6)
Chris@1517 743 pdf.SetFontStyle('',9)
Chris@1517 744 write_wiki_page(pdf, page)
Chris@1517 745 pdf.Output
Chris@1517 746 end
Chris@1517 747
Chris@1517 748 def write_page_hierarchy(pdf, pages, node=nil, level=0)
Chris@1517 749 if pages[node]
Chris@1517 750 pages[node].each do |page|
Chris@1517 751 if @new_page
Chris@1517 752 pdf.AddPage
Chris@1517 753 else
Chris@1517 754 @new_page = true
Chris@1517 755 end
Chris@1517 756 pdf.Bookmark page.title, level
Chris@1517 757 write_wiki_page(pdf, page)
Chris@1517 758 write_page_hierarchy(pdf, pages, page.id, level + 1) if pages[page.id]
Chris@1517 759 end
Chris@1517 760 end
Chris@1517 761 end
Chris@1517 762
Chris@1517 763 def write_wiki_page(pdf, page)
Chris@1517 764 pdf.RDMwriteHTMLCell(190,5,0,0,
Chris@1517 765 page.content.text.to_s, page.attachments, 0)
Chris@1517 766 if page.attachments.any?
Chris@1517 767 pdf.Ln
Chris@1517 768 pdf.SetFontStyle('B',9)
Chris@1517 769 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
Chris@1517 770 pdf.Ln
Chris@1517 771 for attachment in page.attachments
Chris@1517 772 pdf.SetFontStyle('',8)
Chris@1517 773 pdf.RDMCell(80,5, attachment.filename)
Chris@1517 774 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
Chris@1517 775 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
Chris@1517 776 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
Chris@1517 777 pdf.Ln
Chris@1517 778 end
Chris@1517 779 end
Chris@1517 780 end
Chris@1517 781
Chris@1517 782 class RDMPdfEncoding
Chris@1517 783 def self.rdm_from_utf8(txt, encoding)
Chris@1517 784 txt ||= ''
Chris@1517 785 txt = Redmine::CodesetUtil.from_utf8(txt, encoding)
Chris@1517 786 if txt.respond_to?(:force_encoding)
Chris@1517 787 txt.force_encoding('ASCII-8BIT')
Chris@1517 788 end
Chris@1517 789 txt
Chris@1517 790 end
Chris@1517 791
Chris@1517 792 def self.attach(attachments, filename, encoding)
Chris@1517 793 filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
Chris@1517 794 atta = nil
Chris@1517 795 if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i
Chris@1517 796 atta = Attachment.latest_attach(attachments, filename_utf8)
Chris@1517 797 end
Chris@1517 798 if atta && atta.readable? && atta.visible?
Chris@1517 799 return atta
Chris@1517 800 else
Chris@1517 801 return nil
Chris@1517 802 end
Chris@1517 803 end
Chris@1517 804 end
Chris@1517 805 end
Chris@1517 806 end
Chris@1517 807 end