3 # Redmine - project management software
4 # Copyright (C) 2006-2009 Jean-Philippe Lang
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 require 'fpdf/chinese'
23 require 'fpdf/japanese'
29 include ActionView::Helpers::TextHelper
30 include ActionView::Helpers::NumberHelper
34 attr_accessor :footer_date
38 set_language_if_valid lang
39 @font_for_content = 'FreeSans'
40 @font_for_footer = 'FreeSans'
41 SetCreator(Redmine::Info.app_name)
42 SetFont(@font_for_content)
45 def SetFontStyle(style, size)
46 SetFont(@font_for_content, style, size)
51 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
52 hextxt = "<FEFF" # FEFF is BOM
53 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
62 # Format a text string
63 if s =~ /^</ # This means the string is hex-dumped.
66 return '('+escape(s)+')'
71 alias RDMMultiCell MultiCell
74 SetFont(@font_for_footer, 'I', 8)
77 RDMCell(0, 5, @footer_date, 0, 0, 'L')
80 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
86 attr_accessor :footer_date
90 set_language_if_valid lang
91 case l(:general_pdf_encoding).upcase
95 @font_for_content = 'UHC'
96 @font_for_footer = 'UHC'
100 @font_for_content = 'SJIS'
101 @font_for_footer = 'SJIS'
105 @font_for_content = 'GB'
106 @font_for_footer = 'GB'
110 @font_for_content = 'Big5'
111 @font_for_footer = 'Big5'
113 @font_for_content = 'Arial'
114 @font_for_footer = 'Helvetica'
116 SetCreator(Redmine::Info.app_name)
117 SetFont(@font_for_content)
120 def SetFontStyle(style, size)
121 SetFont(@font_for_content, style, size)
126 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
127 hextxt = "<FEFF" # FEFF is BOM
128 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
137 # Format a text string
138 if s =~ /^</ # This means the string is hex-dumped.
141 return '('+escape(s)+')'
145 def fix_text_encoding(txt)
147 if txt.respond_to?(:force_encoding)
148 txt.force_encoding('UTF-8')
149 txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace,
150 :undef => :replace, :replace => '?')
151 txt.force_encoding('ASCII-8BIT')
153 @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
156 txtar += @ic.iconv(txt)
157 rescue Iconv::IllegalSequence
159 txt = '?' + $!.failed[1,$!.failed.length]
169 def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
170 Cell(w,h,fix_text_encoding(txt),border,ln,align,fill,link)
173 def RDMMultiCell(w,h=0,txt='',border=0,align='',fill=0)
174 MultiCell(w,h,fix_text_encoding(txt),border,align,fill)
178 SetFont(@font_for_footer, 'I', 8)
181 RDMCell(0, 5, @footer_date, 0, 0, 'L')
184 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
186 alias alias_nb_pages AliasNbPages
189 # Returns a PDF string of a list of issues
190 def issues_to_pdf(issues, project, query)
191 if l(:general_pdf_encoding).upcase != 'UTF-8'
192 pdf = IFPDF.new(current_language)
194 pdf = ITCPDF.new(current_language)
196 title = query.new_record? ? l(:label_issue_plural) : query.name
197 title = "#{project} - #{title}" if project
200 pdf.footer_date = format_date(Date.today)
201 pdf.SetAutoPageBreak(false)
204 # Landscape A4 = 210 x 297 mm
213 table_width = page_width - right_margin - 10 # fixed left margin
215 unless query.columns.empty?
216 col_width = query.columns.collect do |c|
217 (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) && ['string', 'text'].include?(c.custom_field.field_format)))? 4.0 : 1.0
219 ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
220 col_width = col_width.collect {|w| w * ratio}
224 pdf.SetFontStyle('B',11)
225 pdf.RDMCell(190,10, title)
229 pdf.SetFontStyle('B',8)
230 pdf.SetFillColor(230, 230, 230)
231 pdf.RDMCell(col_id_width, row_height, "#", 1, 0, 'C', 1)
232 query.columns.each_with_index do |column, i|
233 pdf.RDMCell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
238 pdf.SetFontStyle('',8)
239 pdf.SetFillColor(255, 255, 255)
240 previous_group = false
241 issues.each do |issue|
243 (group = query.group_by_column.value(issue)) != previous_group
244 pdf.SetFontStyle('B',9)
245 pdf.RDMCell(277, row_height,
246 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
248 pdf.SetFontStyle('',8)
249 previous_group = group
251 # fetch all the row values
252 col_values = query.columns.collect do |column|
253 s = if column.is_a?(QueryCustomFieldColumn)
254 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
257 value = issue.send(column.name)
260 elsif value.is_a?(Time)
269 # render it off-page to find the max height used
272 pdf.SetY(2 * page_height)
273 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
274 pdf.SetXY(base_x, base_y)
276 # make new page if it doesn't fit on the current one
277 space_left = page_height - base_y - bottom_margin
278 if max_height > space_left
284 # write the cells on page
285 pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
286 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
287 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
288 pdf.SetY(base_y + max_height);
291 if issues.size == Setting.issues_export_limit.to_i
292 pdf.SetFontStyle('B',10)
293 pdf.RDMCell(0, row_height, '...')
298 # Renders MultiCells and returns the maximum height used
299 def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height)
301 max_height = row_height
302 col_values.each_with_index do |column, i|
304 pdf.RDMMultiCell(col_widths[i], row_height, col_values[i], "T", 'L', 1)
305 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
306 pdf.SetXY(col_x + col_widths[i], base_y);
311 # Draw lines to close the row (MultiCell border drawing in not uniform)
312 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y, id_width, col_widths)
313 col_x = top_x + id_width
314 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
315 col_widths.each do |width|
317 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
319 pdf.Line(top_x, top_y, top_x, lower_y) # left border
320 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
323 # Returns a PDF string of a single issue
324 def issue_to_pdf(issue)
325 if l(:general_pdf_encoding).upcase != 'UTF-8'
326 pdf = IFPDF.new(current_language)
328 pdf = ITCPDF.new(current_language)
330 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
332 pdf.footer_date = format_date(Date.today)
334 pdf.SetFontStyle('B',11)
335 pdf.RDMMultiCell(190,5, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
340 pdf.SetFontStyle('B',9)
341 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
342 pdf.SetFontStyle('',9)
343 pdf.RDMCell(60,5, issue.status.to_s,"RT")
344 pdf.SetFontStyle('B',9)
345 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
346 pdf.SetFontStyle('',9)
347 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
350 pdf.SetFontStyle('B',9)
351 pdf.RDMCell(35,5, l(:field_author) + ":","L")
352 pdf.SetFontStyle('',9)
353 pdf.RDMCell(60,5, issue.author.to_s,"R")
354 pdf.SetFontStyle('B',9)
355 pdf.RDMCell(35,5, l(:field_category) + ":","L")
356 pdf.SetFontStyle('',9)
357 pdf.RDMCell(60,5, issue.category.to_s,"R")
360 pdf.SetFontStyle('B',9)
361 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
362 pdf.SetFontStyle('',9)
363 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
364 pdf.SetFontStyle('B',9)
365 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
366 pdf.SetFontStyle('',9)
367 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
370 pdf.SetFontStyle('B',9)
371 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
372 pdf.SetFontStyle('',9)
373 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
374 pdf.SetFontStyle('B',9)
375 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
376 pdf.SetFontStyle('',9)
377 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
380 for custom_value in issue.custom_field_values
381 pdf.SetFontStyle('B',9)
382 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
383 pdf.SetFontStyle('',9)
384 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
387 pdf.SetFontStyle('B',9)
388 pdf.RDMCell(35,5, l(:field_subject) + ":","LT")
389 pdf.SetFontStyle('',9)
390 pdf.RDMMultiCell(155,5, issue.subject,"RT")
392 pdf.SetFontStyle('B',9)
393 pdf.RDMCell(35,5, l(:field_description) + ":","LT")
394 pdf.SetFontStyle('',9)
395 pdf.RDMMultiCell(155,5, issue.description.to_s,"RT")
397 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
398 pdf.Line(pdf.GetX, pdf.GetY, pdf.GetX + 190, pdf.GetY)
401 if issue.changesets.any? &&
402 User.current.allowed_to?(:view_changesets, issue.project)
403 pdf.SetFontStyle('B',9)
404 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
406 for changeset in issue.changesets
407 pdf.SetFontStyle('B',8)
409 format_time(changeset.committed_on) + " - " + changeset.author.to_s)
411 unless changeset.comments.blank?
412 pdf.SetFontStyle('',8)
413 pdf.RDMMultiCell(190,5, changeset.comments.to_s)
419 pdf.SetFontStyle('B',9)
420 pdf.RDMCell(190,5, l(:label_history), "B")
422 for journal in issue.journals.find(
423 :all, :include => [:user, :details],
424 :order => "#{Journal.table_name}.created_on ASC")
425 pdf.SetFontStyle('B',8)
427 format_time(journal.created_on) + " - " + journal.user.name)
429 pdf.SetFontStyle('I',8)
430 for detail in journal.details
431 pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true))
434 pdf.Ln unless journal.details.empty?
435 pdf.SetFontStyle('',8)
436 pdf.RDMMultiCell(190,5, journal.notes.to_s)
441 if issue.attachments.any?
442 pdf.SetFontStyle('B',9)
443 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
445 for attachment in issue.attachments
446 pdf.SetFontStyle('',8)
447 pdf.RDMCell(80,5, attachment.filename)
448 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
449 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
450 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")