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 current_language.to_s.downcase
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)
146 @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
149 txtar = txt.split('\\')
150 txtar << '' if txt[-1] == ?\\
151 txtar.collect {|x| @ic.iconv(x)}.join('\\').gsub(/\\/, "\\\\\\\\")
158 def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
159 Cell(w,h,fix_text_encoding(txt),border,ln,align,fill,link)
162 def RDMMultiCell(w,h=0,txt='',border=0,align='',fill=0)
163 MultiCell(w,h,fix_text_encoding(txt),border,align,fill)
167 SetFont(@font_for_footer, 'I', 8)
170 RDMCell(0, 5, @footer_date, 0, 0, 'L')
173 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
175 alias alias_nb_pages AliasNbPages
178 # Returns a PDF string of a list of issues
179 def issues_to_pdf(issues, project, query)
180 if ( current_language.to_s.downcase == 'ko' ||
181 current_language.to_s.downcase == 'ja' ||
182 current_language.to_s.downcase == 'zh' ||
183 current_language.to_s.downcase == 'zh-tw' ||
184 current_language.to_s.downcase == 'th' )
185 pdf = IFPDF.new(current_language)
187 pdf = ITCPDF.new(current_language)
189 title = query.new_record? ? l(:label_issue_plural) : query.name
190 title = "#{project} - #{title}" if project
193 pdf.footer_date = format_date(Date.today)
198 unless query.columns.empty?
199 col_width = query.columns.collect {|column| column.name == :subject ? 4.0 : 1.0 }
200 ratio = 262.0 / col_width.inject(0) {|s,w| s += w}
201 col_width = col_width.collect {|w| w * ratio}
205 pdf.SetFontStyle('B',11)
206 pdf.RDMCell(190,10, title)
210 pdf.SetFontStyle('B',8)
211 pdf.SetFillColor(230, 230, 230)
212 pdf.RDMCell(15, row_height, "#", 1, 0, 'L', 1)
213 query.columns.each_with_index do |column, i|
214 pdf.RDMCell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
219 pdf.SetFontStyle('',8)
220 pdf.SetFillColor(255, 255, 255)
221 previous_group = false
222 issues.each do |issue|
224 (group = query.group_by_column.value(issue)) != previous_group
225 pdf.SetFontStyle('B',9)
226 pdf.RDMCell(277, row_height,
227 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
229 pdf.SetFontStyle('',8)
230 previous_group = group
232 pdf.RDMCell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
233 query.columns.each_with_index do |column, i|
234 s = if column.is_a?(QueryCustomFieldColumn)
235 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
238 value = issue.send(column.name)
241 elsif value.is_a?(Time)
247 pdf.RDMCell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
251 if issues.size == Setting.issues_export_limit.to_i
252 pdf.SetFontStyle('B',10)
253 pdf.RDMCell(0, row_height, '...')
258 # Returns a PDF string of a single issue
259 def issue_to_pdf(issue)
260 if ( current_language.to_s.downcase == 'ko' ||
261 current_language.to_s.downcase == 'ja' ||
262 current_language.to_s.downcase == 'zh' ||
263 current_language.to_s.downcase == 'zh-tw' ||
264 current_language.to_s.downcase == 'th' )
265 pdf = IFPDF.new(current_language)
267 pdf = ITCPDF.new(current_language)
269 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
271 pdf.footer_date = format_date(Date.today)
274 pdf.SetFontStyle('B',11)
276 "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
281 pdf.SetFontStyle('B',9)
282 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
283 pdf.SetFontStyle('',9)
284 pdf.RDMCell(60,5, issue.status.to_s,"RT")
285 pdf.SetFontStyle('B',9)
286 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
287 pdf.SetFontStyle('',9)
288 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
291 pdf.SetFontStyle('B',9)
292 pdf.RDMCell(35,5, l(:field_author) + ":","L")
293 pdf.SetFontStyle('',9)
294 pdf.RDMCell(60,5, issue.author.to_s,"R")
295 pdf.SetFontStyle('B',9)
296 pdf.RDMCell(35,5, l(:field_category) + ":","L")
297 pdf.SetFontStyle('',9)
298 pdf.RDMCell(60,5, issue.category.to_s,"R")
301 pdf.SetFontStyle('B',9)
302 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
303 pdf.SetFontStyle('',9)
304 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
305 pdf.SetFontStyle('B',9)
306 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
307 pdf.SetFontStyle('',9)
308 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
311 pdf.SetFontStyle('B',9)
312 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
313 pdf.SetFontStyle('',9)
314 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
315 pdf.SetFontStyle('B',9)
316 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
317 pdf.SetFontStyle('',9)
318 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
321 for custom_value in issue.custom_field_values
322 pdf.SetFontStyle('B',9)
323 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
324 pdf.SetFontStyle('',9)
325 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
328 pdf.SetFontStyle('B',9)
329 pdf.RDMCell(35,5, l(:field_subject) + ":","LTB")
330 pdf.SetFontStyle('',9)
331 pdf.RDMCell(155,5, issue.subject,"RTB")
334 pdf.SetFontStyle('B',9)
335 pdf.RDMCell(35,5, l(:field_description) + ":")
336 pdf.SetFontStyle('',9)
337 pdf.RDMMultiCell(155,5, issue.description.to_s,"BR")
339 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
340 pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
343 if issue.changesets.any? &&
344 User.current.allowed_to?(:view_changesets, issue.project)
345 pdf.SetFontStyle('B',9)
346 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
348 for changeset in issue.changesets
349 pdf.SetFontStyle('B',8)
351 format_time(changeset.committed_on) + " - " + changeset.author.to_s)
353 unless changeset.comments.blank?
354 pdf.SetFontStyle('',8)
355 pdf.RDMMultiCell(190,5, changeset.comments.to_s)
361 pdf.SetFontStyle('B',9)
362 pdf.RDMCell(190,5, l(:label_history), "B")
364 for journal in issue.journals.find(
365 :all, :include => [:user, :details],
366 :order => "#{Journal.table_name}.created_on ASC")
367 pdf.SetFontStyle('B',8)
369 format_time(journal.created_on) + " - " + journal.user.name)
371 pdf.SetFontStyle('I',8)
372 for detail in journal.details
373 pdf.RDMCell(190,5, "- " + show_detail(detail, true))
377 pdf.SetFontStyle('',8)
378 pdf.RDMMultiCell(190,5, journal.notes.to_s)
383 if issue.attachments.any?
384 pdf.SetFontStyle('B',9)
385 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
387 for attachment in issue.attachments
388 pdf.SetFontStyle('',8)
389 pdf.RDMCell(80,5, attachment.filename)
390 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
391 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
392 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")