annotate .svn/pristine/19/194013fb324f2790d7cffbbc3147b775a3c4dd89.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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