annotate lib/redmine/export/pdf.rb @ 853:969bb872d4bf feature_142

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