annotate lib/redmine/export/pdf.rb @ 507:0c939c159af4 redmine-1.2

Update to Redmine 1.2.1 on 1.2-stable branch (Redmine SVN rev 6270)
author Chris Cannam
date Thu, 14 Jul 2011 10:32:19 +0100
parents cbce1fd3b1b7
children cbb26bc654de
rev   line source
Chris@0 1 # encoding: utf-8
Chris@0 2 #
Chris@0 3 # Redmine - project management software
Chris@441 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@441 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@441 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@0 20 require 'iconv'
Chris@0 21 require 'rfpdf/fpdf'
Chris@441 22 require 'fpdf/chinese'
Chris@441 23 require 'fpdf/japanese'
Chris@441 24 require 'fpdf/korean'
Chris@0 25
Chris@0 26 module Redmine
Chris@0 27 module Export
Chris@0 28 module PDF
Chris@0 29 include ActionView::Helpers::TextHelper
Chris@0 30 include ActionView::Helpers::NumberHelper
Chris@441 31
Chris@441 32 class ITCPDF < TCPDF
Chris@0 33 include Redmine::I18n
Chris@0 34 attr_accessor :footer_date
Chris@441 35
Chris@0 36 def initialize(lang)
Chris@0 37 set_language_if_valid lang
Chris@441 38 pdf_encoding = l(:general_pdf_encoding).upcase
Chris@441 39 if RUBY_VERSION < '1.9'
Chris@441 40 @ic = Iconv.new(pdf_encoding, 'UTF-8')
Chris@441 41 end
Chris@441 42 super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding)
Chris@507 43 case current_language.to_s.downcase
Chris@507 44 when 'vi'
Chris@507 45 @font_for_content = 'DejaVuSans'
Chris@507 46 @font_for_footer = 'DejaVuSans'
Chris@0 47 else
Chris@507 48 case pdf_encoding
Chris@507 49 when 'UTF-8'
Chris@507 50 @font_for_content = 'FreeSans'
Chris@507 51 @font_for_footer = 'FreeSans'
Chris@507 52 when 'CP949'
Chris@507 53 extend(PDF_Korean)
Chris@507 54 AddUHCFont()
Chris@507 55 @font_for_content = 'UHC'
Chris@507 56 @font_for_footer = 'UHC'
Chris@507 57 when 'CP932', 'SJIS', 'SHIFT_JIS'
Chris@507 58 extend(PDF_Japanese)
Chris@507 59 AddSJISFont()
Chris@507 60 @font_for_content = 'SJIS'
Chris@507 61 @font_for_footer = 'SJIS'
Chris@507 62 when 'GB18030'
Chris@507 63 extend(PDF_Chinese)
Chris@507 64 AddGBFont()
Chris@507 65 @font_for_content = 'GB'
Chris@507 66 @font_for_footer = 'GB'
Chris@507 67 when 'BIG5'
Chris@507 68 extend(PDF_Chinese)
Chris@507 69 AddBig5Font()
Chris@507 70 @font_for_content = 'Big5'
Chris@507 71 @font_for_footer = 'Big5'
Chris@507 72 else
Chris@507 73 @font_for_content = 'Arial'
Chris@507 74 @font_for_footer = 'Helvetica'
Chris@507 75 end
Chris@0 76 end
Chris@0 77 SetCreator(Redmine::Info.app_name)
Chris@0 78 SetFont(@font_for_content)
Chris@0 79 end
Chris@441 80
Chris@0 81 def SetFontStyle(style, size)
Chris@0 82 SetFont(@font_for_content, style, size)
Chris@0 83 end
Chris@441 84
Chris@0 85 def SetTitle(txt)
Chris@0 86 txt = begin
Chris@0 87 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
Chris@0 88 hextxt = "<FEFF" # FEFF is BOM
Chris@0 89 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
Chris@0 90 hextxt << ">"
Chris@0 91 rescue
Chris@0 92 txt
Chris@0 93 end || ''
Chris@0 94 super(txt)
Chris@0 95 end
Chris@441 96
Chris@0 97 def textstring(s)
Chris@0 98 # Format a text string
Chris@0 99 if s =~ /^</ # This means the string is hex-dumped.
Chris@0 100 return s
Chris@0 101 else
Chris@0 102 return '('+escape(s)+')'
Chris@0 103 end
Chris@0 104 end
Chris@441 105
Chris@441 106 def fix_text_encoding(txt)
Chris@441 107 RDMPdfEncoding::rdm_pdf_iconv(@ic, txt)
Chris@0 108 end
Chris@441 109
Chris@507 110 def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='')
Chris@507 111 Cell(w, h, fix_text_encoding(txt), border, ln, align, fill, link)
Chris@441 112 end
Chris@441 113
Chris@507 114 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
Chris@507 115 MultiCell(w, h, fix_text_encoding(txt), border, align, fill, ln)
Chris@441 116 end
Chris@441 117
Chris@0 118 def Footer
Chris@0 119 SetFont(@font_for_footer, 'I', 8)
Chris@0 120 SetY(-15)
Chris@0 121 SetX(15)
Chris@441 122 RDMCell(0, 5, @footer_date, 0, 0, 'L')
Chris@0 123 SetY(-15)
Chris@0 124 SetX(-30)
Chris@441 125 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
Chris@0 126 end
Chris@0 127 end
Chris@441 128
Chris@0 129 # Returns a PDF string of a list of issues
Chris@0 130 def issues_to_pdf(issues, project, query)
Chris@441 131 pdf = ITCPDF.new(current_language)
Chris@0 132 title = query.new_record? ? l(:label_issue_plural) : query.name
Chris@0 133 title = "#{project} - #{title}" if project
Chris@0 134 pdf.SetTitle(title)
Chris@441 135 pdf.alias_nb_pages
Chris@0 136 pdf.footer_date = format_date(Date.today)
Chris@441 137 pdf.SetAutoPageBreak(false)
Chris@0 138 pdf.AddPage("L")
Chris@441 139
Chris@441 140 # Landscape A4 = 210 x 297 mm
Chris@441 141 page_height = 210
Chris@441 142 page_width = 297
Chris@441 143 right_margin = 10
Chris@441 144 bottom_margin = 20
Chris@441 145 col_id_width = 10
Chris@441 146 row_height = 5
Chris@441 147
Chris@441 148 # column widths
Chris@441 149 table_width = page_width - right_margin - 10 # fixed left margin
Chris@0 150 col_width = []
Chris@0 151 unless query.columns.empty?
Chris@441 152 col_width = query.columns.collect do |c|
Chris@441 153 (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) && ['string', 'text'].include?(c.custom_field.field_format)))? 4.0 : 1.0
Chris@441 154 end
Chris@441 155 ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
Chris@0 156 col_width = col_width.collect {|w| w * ratio}
Chris@0 157 end
Chris@441 158
Chris@0 159 # title
Chris@441 160 pdf.SetFontStyle('B',11)
Chris@441 161 pdf.RDMCell(190,10, title)
Chris@0 162 pdf.Ln
Chris@441 163
Chris@0 164 # headers
Chris@0 165 pdf.SetFontStyle('B',8)
Chris@0 166 pdf.SetFillColor(230, 230, 230)
Chris@441 167
Chris@441 168 # render it background to find the max height used
Chris@441 169 base_x = pdf.GetX
Chris@441 170 base_y = pdf.GetY
Chris@441 171 max_height = issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
Chris@441 172 pdf.Rect(base_x, base_y, table_width, max_height, 'FD');
Chris@441 173 pdf.SetXY(base_x, base_y);
Chris@441 174
Chris@441 175 # write the cells on page
Chris@441 176 pdf.RDMCell(col_id_width, row_height, "#", "T", 0, 'C', 1)
Chris@441 177 issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
Chris@441 178 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
Chris@441 179 pdf.SetY(base_y + max_height);
Chris@441 180
Chris@0 181 # rows
Chris@0 182 pdf.SetFontStyle('',8)
Chris@0 183 pdf.SetFillColor(255, 255, 255)
Chris@0 184 previous_group = false
Chris@0 185 issues.each do |issue|
Chris@441 186 if query.grouped? &&
Chris@441 187 (group = query.group_by_column.value(issue)) != previous_group
Chris@0 188 pdf.SetFontStyle('B',9)
Chris@441 189 pdf.RDMCell(277, row_height,
chris@22 190 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
Chris@0 191 1, 1, 'L')
Chris@0 192 pdf.SetFontStyle('',8)
Chris@0 193 previous_group = group
Chris@0 194 end
Chris@441 195 # fetch all the row values
Chris@441 196 col_values = query.columns.collect do |column|
Chris@0 197 s = if column.is_a?(QueryCustomFieldColumn)
Chris@0 198 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
Chris@0 199 show_value(cv)
Chris@0 200 else
Chris@0 201 value = issue.send(column.name)
Chris@0 202 if value.is_a?(Date)
Chris@0 203 format_date(value)
Chris@0 204 elsif value.is_a?(Time)
Chris@0 205 format_time(value)
Chris@0 206 else
Chris@0 207 value
Chris@0 208 end
Chris@0 209 end
Chris@441 210 s.to_s
Chris@0 211 end
Chris@441 212
Chris@441 213 # render it off-page to find the max height used
Chris@441 214 base_x = pdf.GetX
Chris@441 215 base_y = pdf.GetY
Chris@441 216 pdf.SetY(2 * page_height)
Chris@441 217 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
Chris@441 218 pdf.SetXY(base_x, base_y)
Chris@441 219
Chris@441 220 # make new page if it doesn't fit on the current one
Chris@441 221 space_left = page_height - base_y - bottom_margin
Chris@441 222 if max_height > space_left
Chris@441 223 pdf.AddPage("L")
Chris@441 224 base_x = pdf.GetX
Chris@441 225 base_y = pdf.GetY
Chris@441 226 end
Chris@441 227
Chris@441 228 # write the cells on page
Chris@441 229 pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
Chris@441 230 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
Chris@441 231 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
Chris@441 232 pdf.SetY(base_y + max_height);
Chris@0 233 end
Chris@441 234
Chris@0 235 if issues.size == Setting.issues_export_limit.to_i
Chris@0 236 pdf.SetFontStyle('B',10)
Chris@441 237 pdf.RDMCell(0, row_height, '...')
Chris@0 238 end
Chris@0 239 pdf.Output
Chris@0 240 end
chris@22 241
Chris@441 242 # Renders MultiCells and returns the maximum height used
Chris@441 243 def issues_to_pdf_write_cells(pdf, col_values, col_widths,
Chris@441 244 row_height, head=false)
Chris@441 245 base_y = pdf.GetY
Chris@441 246 max_height = row_height
Chris@441 247 col_values.each_with_index do |column, i|
Chris@441 248 col_x = pdf.GetX
Chris@441 249 if head == true
Chris@441 250 pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1)
Chris@441 251 else
Chris@441 252 pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1)
Chris@441 253 end
Chris@441 254 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
Chris@441 255 pdf.SetXY(col_x + col_widths[i], base_y);
Chris@441 256 end
Chris@441 257 return max_height
Chris@441 258 end
Chris@441 259
Chris@441 260 # Draw lines to close the row (MultiCell border drawing in not uniform)
Chris@441 261 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y,
Chris@441 262 id_width, col_widths)
Chris@441 263 col_x = top_x + id_width
Chris@441 264 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
Chris@441 265 col_widths.each do |width|
Chris@441 266 col_x += width
Chris@441 267 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
Chris@441 268 end
Chris@441 269 pdf.Line(top_x, top_y, top_x, lower_y) # left border
Chris@441 270 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
Chris@441 271 end
Chris@441 272
Chris@0 273 # Returns a PDF string of a single issue
Chris@0 274 def issue_to_pdf(issue)
Chris@441 275 pdf = ITCPDF.new(current_language)
Chris@0 276 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
Chris@441 277 pdf.alias_nb_pages
Chris@0 278 pdf.footer_date = format_date(Date.today)
Chris@0 279 pdf.AddPage
Chris@441 280 pdf.SetFontStyle('B',11)
Chris@441 281 pdf.RDMMultiCell(190,5,
Chris@441 282 "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
Chris@0 283 pdf.Ln
Chris@441 284
Chris@0 285 pdf.SetFontStyle('B',9)
Chris@441 286 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
Chris@0 287 pdf.SetFontStyle('',9)
Chris@441 288 pdf.RDMCell(60,5, issue.status.to_s,"RT")
Chris@0 289 pdf.SetFontStyle('B',9)
Chris@441 290 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
Chris@0 291 pdf.SetFontStyle('',9)
Chris@441 292 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
Chris@0 293 pdf.Ln
Chris@441 294
Chris@0 295 pdf.SetFontStyle('B',9)
Chris@441 296 pdf.RDMCell(35,5, l(:field_author) + ":","L")
Chris@0 297 pdf.SetFontStyle('',9)
Chris@441 298 pdf.RDMCell(60,5, issue.author.to_s,"R")
Chris@0 299 pdf.SetFontStyle('B',9)
Chris@441 300 pdf.RDMCell(35,5, l(:field_category) + ":","L")
Chris@0 301 pdf.SetFontStyle('',9)
Chris@441 302 pdf.RDMCell(60,5, issue.category.to_s,"R")
Chris@441 303 pdf.Ln
Chris@441 304
Chris@0 305 pdf.SetFontStyle('B',9)
Chris@441 306 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
Chris@0 307 pdf.SetFontStyle('',9)
Chris@441 308 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
Chris@0 309 pdf.SetFontStyle('B',9)
Chris@441 310 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
Chris@0 311 pdf.SetFontStyle('',9)
Chris@441 312 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
Chris@0 313 pdf.Ln
Chris@441 314
Chris@0 315 pdf.SetFontStyle('B',9)
Chris@441 316 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
Chris@0 317 pdf.SetFontStyle('',9)
Chris@441 318 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
Chris@0 319 pdf.SetFontStyle('B',9)
Chris@441 320 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
Chris@0 321 pdf.SetFontStyle('',9)
Chris@441 322 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
Chris@0 323 pdf.Ln
Chris@441 324
Chris@0 325 for custom_value in issue.custom_field_values
Chris@0 326 pdf.SetFontStyle('B',9)
Chris@441 327 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
Chris@0 328 pdf.SetFontStyle('',9)
Chris@441 329 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
Chris@0 330 end
Chris@441 331
Chris@507 332 y0 = pdf.GetY
Chris@507 333
Chris@0 334 pdf.SetFontStyle('B',9)
Chris@441 335 pdf.RDMCell(35,5, l(:field_subject) + ":","LT")
Chris@0 336 pdf.SetFontStyle('',9)
Chris@441 337 pdf.RDMMultiCell(155,5, issue.subject,"RT")
Chris@507 338 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
Chris@441 339
Chris@0 340 pdf.SetFontStyle('B',9)
Chris@507 341 pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
Chris@0 342 pdf.SetFontStyle('',9)
Chris@507 343 pdf.RDMMultiCell(35+155, 5, issue.description.to_s, "LRB")
Chris@0 344 pdf.Ln
Chris@441 345
Chris@441 346 if issue.changesets.any? &&
Chris@441 347 User.current.allowed_to?(:view_changesets, issue.project)
Chris@0 348 pdf.SetFontStyle('B',9)
Chris@441 349 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
Chris@0 350 pdf.Ln
Chris@0 351 for changeset in issue.changesets
Chris@0 352 pdf.SetFontStyle('B',8)
Chris@507 353 csstr = "#{l(:label_revision)} #{changeset.format_identifier} - "
Chris@507 354 csstr += format_time(changeset.committed_on) + " - " + changeset.author.to_s
Chris@507 355 pdf.RDMCell(190, 5, csstr)
Chris@0 356 pdf.Ln
Chris@0 357 unless changeset.comments.blank?
Chris@0 358 pdf.SetFontStyle('',8)
Chris@441 359 pdf.RDMMultiCell(190,5, changeset.comments.to_s)
Chris@441 360 end
Chris@0 361 pdf.Ln
Chris@0 362 end
Chris@0 363 end
Chris@441 364
Chris@0 365 pdf.SetFontStyle('B',9)
Chris@441 366 pdf.RDMCell(190,5, l(:label_history), "B")
Chris@441 367 pdf.Ln
Chris@441 368 for journal in issue.journals.find(
Chris@441 369 :all, :include => [:user, :details],
Chris@441 370 :order => "#{Journal.table_name}.created_on ASC")
Chris@0 371 pdf.SetFontStyle('B',8)
Chris@441 372 pdf.RDMCell(190,5,
Chris@441 373 format_time(journal.created_on) + " - " + journal.user.name)
Chris@0 374 pdf.Ln
Chris@0 375 pdf.SetFontStyle('I',8)
Chris@0 376 for detail in journal.details
Chris@441 377 pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true))
Chris@0 378 end
Chris@0 379 if journal.notes?
Chris@441 380 pdf.Ln unless journal.details.empty?
Chris@0 381 pdf.SetFontStyle('',8)
Chris@441 382 pdf.RDMMultiCell(190,5, journal.notes.to_s)
Chris@441 383 end
Chris@0 384 pdf.Ln
Chris@0 385 end
Chris@441 386
Chris@0 387 if issue.attachments.any?
Chris@0 388 pdf.SetFontStyle('B',9)
Chris@441 389 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
Chris@0 390 pdf.Ln
Chris@0 391 for attachment in issue.attachments
Chris@0 392 pdf.SetFontStyle('',8)
Chris@441 393 pdf.RDMCell(80,5, attachment.filename)
Chris@441 394 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
Chris@441 395 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
Chris@441 396 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
Chris@0 397 pdf.Ln
Chris@0 398 end
Chris@0 399 end
Chris@0 400 pdf.Output
Chris@0 401 end
chris@22 402
Chris@441 403 class RDMPdfEncoding
Chris@441 404 include Redmine::I18n
Chris@441 405 def self.rdm_pdf_iconv(ic, txt)
Chris@441 406 txt ||= ''
Chris@441 407 if txt.respond_to?(:force_encoding)
Chris@441 408 txt.force_encoding('UTF-8')
Chris@441 409 if l(:general_pdf_encoding).upcase != 'UTF-8'
Chris@441 410 txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace,
Chris@441 411 :undef => :replace, :replace => '?')
Chris@441 412 else
Chris@441 413 txt = Redmine::CodesetUtil.replace_invalid_utf8(txt)
Chris@441 414 end
Chris@441 415 txt.force_encoding('ASCII-8BIT')
Chris@507 416 elsif RUBY_PLATFORM == 'java'
Chris@507 417 begin
Chris@507 418 ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
Chris@507 419 txt = ic.iconv(txt)
Chris@507 420 rescue
Chris@507 421 txt = txt.gsub(%r{[^\r\n\t\x20-\x7e]}, '?')
Chris@507 422 end
Chris@441 423 else
Chris@441 424 ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
Chris@441 425 txtar = ""
Chris@441 426 begin
Chris@441 427 txtar += ic.iconv(txt)
Chris@441 428 rescue Iconv::IllegalSequence
Chris@441 429 txtar += $!.success
Chris@441 430 txt = '?' + $!.failed[1,$!.failed.length]
Chris@441 431 retry
Chris@441 432 rescue
Chris@441 433 txtar += $!.success
Chris@441 434 end
Chris@441 435 txt = txtar
Chris@441 436 end
Chris@441 437 txt
Chris@441 438 end
Chris@441 439 end
Chris@0 440 end
Chris@0 441 end
Chris@0 442 end