annotate .svn/pristine/a5/a5eae59bbce6cca98bb354eb803d70ece1bc50d0.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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