annotate .svn/pristine/78/78dfa12979bae9d902d8a61dfe74ca64de496f0f.svn-base @ 1464:261b3d9a4903 redmine-2.4

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