Chris@0: # encoding: utf-8 Chris@0: # Chris@0: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@1115: require 'tcpdf' Chris@441: require 'fpdf/chinese' Chris@441: require 'fpdf/japanese' Chris@441: require 'fpdf/korean' Chris@0: Chris@1464: if RUBY_VERSION < '1.9' Chris@1464: require 'iconv' Chris@1464: end Chris@1464: Chris@0: module Redmine Chris@0: module Export Chris@0: module PDF Chris@0: include ActionView::Helpers::TextHelper Chris@0: include ActionView::Helpers::NumberHelper Chris@909: include IssuesHelper Chris@441: Chris@441: class ITCPDF < TCPDF Chris@0: include Redmine::I18n Chris@0: attr_accessor :footer_date Chris@441: Chris@1115: def initialize(lang, orientation='P') Chris@909: @@k_path_cache = Rails.root.join('tmp', 'pdf') Chris@909: FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache) Chris@0: set_language_if_valid lang Chris@441: pdf_encoding = l(:general_pdf_encoding).upcase Chris@1115: super(orientation, 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding) Chris@507: case current_language.to_s.downcase Chris@507: when 'vi' Chris@507: @font_for_content = 'DejaVuSans' Chris@507: @font_for_footer = 'DejaVuSans' Chris@0: else Chris@507: case pdf_encoding Chris@507: when 'UTF-8' Chris@507: @font_for_content = 'FreeSans' Chris@507: @font_for_footer = 'FreeSans' Chris@507: when 'CP949' Chris@507: extend(PDF_Korean) Chris@507: AddUHCFont() Chris@507: @font_for_content = 'UHC' Chris@507: @font_for_footer = 'UHC' Chris@507: when 'CP932', 'SJIS', 'SHIFT_JIS' Chris@507: extend(PDF_Japanese) Chris@507: AddSJISFont() Chris@507: @font_for_content = 'SJIS' Chris@507: @font_for_footer = 'SJIS' Chris@507: when 'GB18030' Chris@507: extend(PDF_Chinese) Chris@507: AddGBFont() Chris@507: @font_for_content = 'GB' Chris@507: @font_for_footer = 'GB' Chris@507: when 'BIG5' Chris@507: extend(PDF_Chinese) Chris@507: AddBig5Font() Chris@507: @font_for_content = 'Big5' Chris@507: @font_for_footer = 'Big5' Chris@507: else Chris@507: @font_for_content = 'Arial' Chris@507: @font_for_footer = 'Helvetica' Chris@507: end Chris@0: end Chris@0: SetCreator(Redmine::Info.app_name) Chris@0: SetFont(@font_for_content) Chris@1115: @outlines = [] Chris@1115: @outlineRoot = nil Chris@0: end Chris@441: Chris@0: def SetFontStyle(style, size) Chris@0: SetFont(@font_for_content, style, size) Chris@0: end Chris@441: Chris@0: def SetTitle(txt) Chris@0: txt = begin Chris@1464: utf16txt = to_utf16(txt) Chris@0: hextxt = "" Chris@0: rescue Chris@0: txt Chris@0: end || '' Chris@0: super(txt) Chris@0: end Chris@441: Chris@0: def textstring(s) Chris@0: # Format a text string Chris@0: if s =~ /^\{\{([<>]?)toc\}\}<\/p>/i, '') Chris@1294: html Chris@1294: end Chris@1294: Chris@1464: # Encodes an UTF-8 string to UTF-16BE Chris@1464: def to_utf16(str) Chris@1464: if str.respond_to?(:encode) Chris@1464: str.encode('UTF-16BE') Chris@1464: else Chris@1464: Iconv.conv('UTF-16BE', 'UTF-8', str) Chris@1464: end Chris@1464: end Chris@1464: Chris@507: def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='') Chris@507: Cell(w, h, fix_text_encoding(txt), border, ln, align, fill, link) Chris@441: end Chris@441: Chris@507: def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1) Chris@507: MultiCell(w, h, fix_text_encoding(txt), border, align, fill, ln) Chris@441: end Chris@441: Chris@909: def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0) Chris@909: @attachments = attachments Chris@909: writeHTMLCell(w, h, x, y, Chris@1294: fix_text_encoding(formatted_text(txt)), Chris@909: border, ln, fill) Chris@909: end Chris@909: Chris@909: def getImageFilename(attrname) Chris@909: # attrname: general_pdf_encoding string file/uri name Chris@909: atta = RDMPdfEncoding.attach(@attachments, attrname, l(:general_pdf_encoding)) Chris@909: if atta Chris@909: return atta.diskfile Chris@909: else Chris@909: return nil Chris@909: end Chris@909: end Chris@909: Chris@0: def Footer Chris@0: SetFont(@font_for_footer, 'I', 8) Chris@0: SetY(-15) Chris@0: SetX(15) Chris@441: RDMCell(0, 5, @footer_date, 0, 0, 'L') Chris@0: SetY(-15) Chris@0: SetX(-30) Chris@441: RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C') Chris@0: end Chris@1115: Chris@1115: def Bookmark(txt, level=0, y=0) Chris@1115: if (y == -1) Chris@1115: y = GetY() Chris@1115: end Chris@1115: @outlines << {:t => txt, :l => level, :p => PageNo(), :y => (@h - y)*@k} Chris@1115: end Chris@1115: Chris@1115: def bookmark_title(txt) Chris@1115: txt = begin Chris@1464: utf16txt = to_utf16(txt) Chris@1115: hextxt = "" Chris@1115: rescue Chris@1115: txt Chris@1115: end || '' Chris@1115: end Chris@1115: Chris@1115: def putbookmarks Chris@1115: nb=@outlines.size Chris@1115: return if (nb==0) Chris@1115: lru=[] Chris@1115: level=0 Chris@1115: @outlines.each_with_index do |o, i| Chris@1115: if(o[:l]>0) Chris@1115: parent=lru[o[:l]-1] Chris@1115: #Set parent and last pointers Chris@1115: @outlines[i][:parent]=parent Chris@1115: @outlines[parent][:last]=i Chris@1115: if (o[:l]>level) Chris@1115: #Level increasing: set first pointer Chris@1115: @outlines[parent][:first]=i Chris@1115: end Chris@1115: else Chris@1115: @outlines[i][:parent]=nb Chris@1115: end Chris@1115: if (o[:l]<=level && i>0) Chris@1115: #Set prev and next pointers Chris@1115: prev=lru[o[:l]] Chris@1115: @outlines[prev][:next]=i Chris@1115: @outlines[i][:prev]=prev Chris@1115: end Chris@1115: lru[o[:l]]=i Chris@1115: level=o[:l] Chris@1115: end Chris@1115: #Outline items Chris@1115: n=self.n+1 Chris@1115: @outlines.each_with_index do |o, i| Chris@1115: newobj() Chris@1115: out('<>') Chris@1115: out('endobj') Chris@1115: end Chris@1115: #Outline root Chris@1115: newobj() Chris@1115: @outlineRoot=self.n Chris@1115: out("<>"); Chris@1115: out('endobj'); Chris@1115: end Chris@1115: Chris@1115: def putresources() Chris@1115: super Chris@1115: putbookmarks() Chris@1115: end Chris@1115: Chris@1115: def putcatalog() Chris@1115: super Chris@1115: if(@outlines.size > 0) Chris@1115: out("/Outlines #{@outlineRoot} 0 R"); Chris@1115: out('/PageMode /UseOutlines'); Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: # fetch row values Chris@1115: def fetch_row_values(issue, query, level) Chris@1115: query.inline_columns.collect do |column| Chris@1115: s = if column.is_a?(QueryCustomFieldColumn) Chris@1464: cv = issue.visible_custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id} Chris@1517: show_value(cv, false) Chris@1115: else Chris@1115: value = issue.send(column.name) Chris@1115: if column.name == :subject Chris@1115: value = " " * level + value Chris@1115: end Chris@1115: if value.is_a?(Date) Chris@1115: format_date(value) Chris@1115: elsif value.is_a?(Time) Chris@1115: format_time(value) Chris@1115: else Chris@1115: value Chris@1115: end Chris@1115: end Chris@1115: s.to_s Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: # calculate columns width Chris@1115: def calc_col_width(issues, query, table_width, pdf) Chris@1115: # calculate statistics Chris@1115: # by captions Chris@1115: pdf.SetFontStyle('B',8) Chris@1115: col_padding = pdf.GetStringWidth('OO') Chris@1115: col_width_min = query.inline_columns.map {|v| pdf.GetStringWidth(v.caption) + col_padding} Chris@1115: col_width_max = Array.new(col_width_min) Chris@1115: col_width_avg = Array.new(col_width_min) Chris@1115: word_width_max = query.inline_columns.map {|c| Chris@1115: n = 10 Chris@1115: c.caption.split.each {|w| Chris@1115: x = pdf.GetStringWidth(w) + col_padding Chris@1115: n = x if n < x Chris@1115: } Chris@1115: n Chris@1115: } Chris@1115: Chris@1115: # by properties of issues Chris@1115: pdf.SetFontStyle('',8) Chris@1115: col_padding = pdf.GetStringWidth('OO') Chris@1115: k = 1 Chris@1115: issue_list(issues) {|issue, level| Chris@1115: k += 1 Chris@1115: values = fetch_row_values(issue, query, level) Chris@1115: values.each_with_index {|v,i| Chris@1115: n = pdf.GetStringWidth(v) + col_padding Chris@1115: col_width_max[i] = n if col_width_max[i] < n Chris@1115: col_width_min[i] = n if col_width_min[i] > n Chris@1115: col_width_avg[i] += n Chris@1115: v.split.each {|w| Chris@1115: x = pdf.GetStringWidth(w) + col_padding Chris@1115: word_width_max[i] = x if word_width_max[i] < x Chris@1115: } Chris@1115: } Chris@1115: } Chris@1115: col_width_avg.map! {|x| x / k} Chris@1115: Chris@1115: # calculate columns width Chris@1464: ratio = table_width / col_width_avg.inject(0, :+) Chris@1115: col_width = col_width_avg.map {|w| w * ratio} Chris@1115: Chris@1115: # correct max word width if too many columns Chris@1464: ratio = table_width / word_width_max.inject(0, :+) Chris@1115: word_width_max.map! {|v| v * ratio} if ratio < 1 Chris@1115: Chris@1115: # correct and lock width of some columns Chris@1115: done = 1 Chris@1115: col_fix = [] Chris@1115: col_width.each_with_index do |w,i| Chris@1115: if w > col_width_max[i] Chris@1115: col_width[i] = col_width_max[i] Chris@1115: col_fix[i] = 1 Chris@1115: done = 0 Chris@1115: elsif w < word_width_max[i] Chris@1115: col_width[i] = word_width_max[i] Chris@1115: col_fix[i] = 1 Chris@1115: done = 0 Chris@1115: else Chris@1115: col_fix[i] = 0 Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: # iterate while need to correct and lock coluns width Chris@1115: while done == 0 Chris@1115: # calculate free & locked columns width Chris@1115: done = 1 Chris@1115: fix_col_width = 0 Chris@1115: free_col_width = 0 Chris@1115: col_width.each_with_index do |w,i| Chris@1115: if col_fix[i] == 1 Chris@1115: fix_col_width += w Chris@1115: else Chris@1115: free_col_width += w Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: # calculate column normalizing ratio Chris@1115: if free_col_width == 0 Chris@1464: ratio = table_width / col_width.inject(0, :+) Chris@1115: else Chris@1115: ratio = (table_width - fix_col_width) / free_col_width Chris@1115: end Chris@1115: Chris@1115: # correct columns width Chris@1115: col_width.each_with_index do |w,i| Chris@1115: if col_fix[i] == 0 Chris@1115: col_width[i] = w * ratio Chris@1115: Chris@1115: # check if column width less then max word width Chris@1115: if col_width[i] < word_width_max[i] Chris@1115: col_width[i] = word_width_max[i] Chris@1115: col_fix[i] = 1 Chris@1115: done = 0 Chris@1115: elsif col_width[i] > col_width_max[i] Chris@1115: col_width[i] = col_width_max[i] Chris@1115: col_fix[i] = 1 Chris@1115: done = 0 Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: col_width Chris@1115: end Chris@1115: Chris@1464: def render_table_header(pdf, query, col_width, row_height, table_width) Chris@1115: # headers Chris@1115: pdf.SetFontStyle('B',8) Chris@1115: pdf.SetFillColor(230, 230, 230) Chris@1115: Chris@1115: # render it background to find the max height used Chris@1115: base_x = pdf.GetX Chris@1115: base_y = pdf.GetY Chris@1115: max_height = issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, row_height, true) Chris@1464: pdf.Rect(base_x, base_y, table_width, max_height, 'FD'); Chris@1115: pdf.SetXY(base_x, base_y); Chris@1115: Chris@1115: # write the cells on page Chris@1115: issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, row_height, true) Chris@1464: issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, 0, col_width) Chris@1115: pdf.SetY(base_y + max_height); Chris@1115: Chris@1115: # rows Chris@1115: pdf.SetFontStyle('',8) Chris@1115: pdf.SetFillColor(255, 255, 255) Chris@0: end Chris@441: Chris@0: # Returns a PDF string of a list of issues Chris@0: def issues_to_pdf(issues, project, query) Chris@1115: pdf = ITCPDF.new(current_language, "L") Chris@0: title = query.new_record? ? l(:label_issue_plural) : query.name Chris@0: title = "#{project} - #{title}" if project Chris@0: pdf.SetTitle(title) Chris@441: pdf.alias_nb_pages Chris@0: pdf.footer_date = format_date(Date.today) Chris@441: pdf.SetAutoPageBreak(false) Chris@0: pdf.AddPage("L") Chris@441: Chris@441: # Landscape A4 = 210 x 297 mm Chris@441: page_height = 210 Chris@441: page_width = 297 Chris@1464: left_margin = 10 Chris@441: right_margin = 10 Chris@441: bottom_margin = 20 Chris@1115: row_height = 4 Chris@441: Chris@441: # column widths Chris@1464: table_width = page_width - right_margin - left_margin Chris@0: col_width = [] Chris@1115: unless query.inline_columns.empty? Chris@1464: col_width = calc_col_width(issues, query, table_width, pdf) Chris@1464: table_width = col_width.inject(0, :+) Chris@1115: end Chris@1115: Chris@1464: # use full width if the description is displayed Chris@1115: if table_width > 0 && query.has_column?(:description) Chris@1464: col_width = col_width.map {|w| w * (page_width - right_margin - left_margin) / table_width} Chris@1464: table_width = col_width.inject(0, :+) Chris@0: end Chris@441: Chris@0: # title Chris@441: pdf.SetFontStyle('B',11) Chris@441: pdf.RDMCell(190,10, title) Chris@0: pdf.Ln Chris@1464: render_table_header(pdf, query, col_width, row_height, table_width) Chris@0: previous_group = false Chris@909: issue_list(issues) do |issue, level| Chris@441: if query.grouped? && Chris@441: (group = query.group_by_column.value(issue)) != previous_group Chris@1115: pdf.SetFontStyle('B',10) Chris@1115: group_label = group.blank? ? 'None' : group.to_s.dup Chris@1115: group_label << " (#{query.issue_count_by_group[group]})" Chris@1115: pdf.Bookmark group_label, 0, -1 Chris@1464: pdf.RDMCell(table_width, row_height * 2, group_label, 1, 1, 'L') Chris@0: pdf.SetFontStyle('',8) Chris@0: previous_group = group Chris@0: end Chris@1115: Chris@1115: # fetch row values Chris@1115: col_values = fetch_row_values(issue, query, level) Chris@441: Chris@441: # render it off-page to find the max height used Chris@441: base_x = pdf.GetX Chris@441: base_y = pdf.GetY Chris@441: pdf.SetY(2 * page_height) Chris@441: max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height) Chris@441: pdf.SetXY(base_x, base_y) Chris@441: Chris@441: # make new page if it doesn't fit on the current one Chris@441: space_left = page_height - base_y - bottom_margin Chris@441: if max_height > space_left Chris@441: pdf.AddPage("L") Chris@1464: render_table_header(pdf, query, col_width, row_height, table_width) Chris@441: base_x = pdf.GetX Chris@441: base_y = pdf.GetY Chris@441: end Chris@441: Chris@441: # write the cells on page Chris@441: issues_to_pdf_write_cells(pdf, col_values, col_width, row_height) Chris@1464: issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, 0, col_width) Chris@441: pdf.SetY(base_y + max_height); Chris@1115: Chris@1115: if query.has_column?(:description) && issue.description? Chris@1115: pdf.SetX(10) Chris@1115: pdf.SetAutoPageBreak(true, 20) Chris@1115: pdf.RDMwriteHTMLCell(0, 5, 10, 0, issue.description.to_s, issue.attachments, "LRBT") Chris@1115: pdf.SetAutoPageBreak(false) Chris@1115: end Chris@0: end Chris@441: Chris@0: if issues.size == Setting.issues_export_limit.to_i Chris@0: pdf.SetFontStyle('B',10) Chris@441: pdf.RDMCell(0, row_height, '...') Chris@0: end Chris@0: pdf.Output Chris@0: end chris@22: Chris@441: # Renders MultiCells and returns the maximum height used Chris@1464: def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height, head=false) Chris@441: base_y = pdf.GetY Chris@441: max_height = row_height Chris@441: col_values.each_with_index do |column, i| Chris@441: col_x = pdf.GetX Chris@441: if head == true Chris@441: pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1) Chris@441: else Chris@441: pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1) Chris@441: end Chris@441: max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height Chris@441: pdf.SetXY(col_x + col_widths[i], base_y); Chris@441: end Chris@441: return max_height Chris@441: end Chris@441: Chris@441: # Draw lines to close the row (MultiCell border drawing in not uniform) Chris@1464: # Chris@1464: # parameter "col_id_width" is not used. it is kept for compatibility. Chris@441: def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y, Chris@1464: col_id_width, col_widths) Chris@1464: col_x = top_x Chris@441: pdf.Line(col_x, top_y, col_x, lower_y) # id right border Chris@441: col_widths.each do |width| Chris@441: col_x += width Chris@441: pdf.Line(col_x, top_y, col_x, lower_y) # columns right border Chris@441: end Chris@441: pdf.Line(top_x, top_y, top_x, lower_y) # left border Chris@441: pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border Chris@441: end Chris@441: Chris@0: # Returns a PDF string of a single issue Chris@1115: def issue_to_pdf(issue, assoc={}) Chris@441: pdf = ITCPDF.new(current_language) Chris@1115: pdf.SetTitle("#{issue.project} - #{issue.tracker} ##{issue.id}") Chris@441: pdf.alias_nb_pages Chris@0: pdf.footer_date = format_date(Date.today) Chris@0: pdf.AddPage Chris@441: pdf.SetFontStyle('B',11) Chris@1115: buf = "#{issue.project} - #{issue.tracker} ##{issue.id}" Chris@909: pdf.RDMMultiCell(190, 5, buf) Chris@909: pdf.SetFontStyle('',8) Chris@909: base_x = pdf.GetX Chris@909: i = 1 Chris@1115: issue.ancestors.visible.each do |ancestor| Chris@909: pdf.SetX(base_x + i) Chris@909: buf = "#{ancestor.tracker} # #{ancestor.id} (#{ancestor.status.to_s}): #{ancestor.subject}" Chris@909: pdf.RDMMultiCell(190 - i, 5, buf) Chris@909: i += 1 if i < 35 Chris@909: end Chris@1115: pdf.SetFontStyle('B',11) Chris@1115: pdf.RDMMultiCell(190 - i, 5, issue.subject.to_s) Chris@1115: pdf.SetFontStyle('',8) Chris@1115: pdf.RDMMultiCell(190, 5, "#{format_time(issue.created_on)} - #{issue.author}") Chris@0: pdf.Ln Chris@441: Chris@1115: left = [] Chris@1115: left << [l(:field_status), issue.status] Chris@1115: left << [l(:field_priority), issue.priority] Chris@1115: left << [l(:field_assigned_to), issue.assigned_to] unless issue.disabled_core_fields.include?('assigned_to_id') Chris@1115: left << [l(:field_category), issue.category] unless issue.disabled_core_fields.include?('category_id') Chris@1115: left << [l(:field_fixed_version), issue.fixed_version] unless issue.disabled_core_fields.include?('fixed_version_id') Chris@441: Chris@1115: right = [] Chris@1115: right << [l(:field_start_date), format_date(issue.start_date)] unless issue.disabled_core_fields.include?('start_date') Chris@1115: right << [l(:field_due_date), format_date(issue.due_date)] unless issue.disabled_core_fields.include?('due_date') Chris@1115: right << [l(:field_done_ratio), "#{issue.done_ratio}%"] unless issue.disabled_core_fields.include?('done_ratio') Chris@1115: right << [l(:field_estimated_hours), l_hours(issue.estimated_hours)] unless issue.disabled_core_fields.include?('estimated_hours') Chris@1115: right << [l(:label_spent_time), l_hours(issue.total_spent_hours)] if User.current.allowed_to?(:view_time_entries, issue.project) Chris@441: Chris@1115: rows = left.size > right.size ? left.size : right.size Chris@1115: while left.size < rows Chris@1115: left << nil Chris@1115: end Chris@1115: while right.size < rows Chris@1115: right << nil Chris@0: end Chris@441: Chris@1464: half = (issue.visible_custom_field_values.size / 2.0).ceil Chris@1464: issue.visible_custom_field_values.each_with_index do |custom_value, i| Chris@1517: (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value, false)] Chris@1115: end Chris@507: Chris@1115: rows = left.size > right.size ? left.size : right.size Chris@1115: rows.times do |i| Chris@1115: item = left[i] Chris@1115: pdf.SetFontStyle('B',9) Chris@1115: pdf.RDMCell(35,5, item ? "#{item.first}:" : "", i == 0 ? "LT" : "L") Chris@1115: pdf.SetFontStyle('',9) Chris@1115: pdf.RDMCell(60,5, item ? item.last.to_s : "", i == 0 ? "RT" : "R") Chris@1115: Chris@1115: item = right[i] Chris@1115: pdf.SetFontStyle('B',9) Chris@1115: pdf.RDMCell(35,5, item ? "#{item.first}:" : "", i == 0 ? "LT" : "L") Chris@1115: pdf.SetFontStyle('',9) Chris@1115: pdf.RDMCell(60,5, item ? item.last.to_s : "", i == 0 ? "RT" : "R") Chris@1115: pdf.Ln Chris@1115: end Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@507: pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1) Chris@0: pdf.SetFontStyle('',9) Chris@909: Chris@909: # Set resize image scale Chris@909: pdf.SetImageScale(1.6) Chris@909: pdf.RDMwriteHTMLCell(35+155, 5, 0, 0, Chris@909: issue.description.to_s, issue.attachments, "LRB") Chris@909: Chris@909: unless issue.leaf? Chris@909: # for CJK Chris@909: truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 90 : 65 ) Chris@909: pdf.SetFontStyle('B',9) Chris@909: pdf.RDMCell(35+155,5, l(:label_subtask_plural) + ":", "LTR") Chris@909: pdf.Ln Chris@1115: issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level| Chris@1517: buf = "#{child.tracker} # #{child.id}: #{child.subject}". Chris@1517: truncate(truncate_length) Chris@909: level = 10 if level >= 10 Chris@909: pdf.SetFontStyle('',8) Chris@909: pdf.RDMCell(35+135,5, (level >=1 ? " " * level : "") + buf, "L") Chris@909: pdf.SetFontStyle('B',8) Chris@909: pdf.RDMCell(20,5, child.status.to_s, "R") Chris@909: pdf.Ln Chris@909: end Chris@909: end Chris@909: Chris@909: relations = issue.relations.select { |r| r.other_issue(issue).visible? } Chris@909: unless relations.empty? Chris@909: # for CJK Chris@909: truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 80 : 60 ) Chris@909: pdf.SetFontStyle('B',9) Chris@909: pdf.RDMCell(35+155,5, l(:label_related_issues) + ":", "LTR") Chris@909: pdf.Ln Chris@909: relations.each do |relation| Chris@909: buf = "" Chris@909: buf += "#{l(relation.label_for(issue))} " Chris@909: if relation.delay && relation.delay != 0 Chris@909: buf += "(#{l('datetime.distance_in_words.x_days', :count => relation.delay)}) " Chris@909: end Chris@909: if Setting.cross_project_issue_relations? Chris@909: buf += "#{relation.other_issue(issue).project} - " Chris@909: end Chris@909: buf += "#{relation.other_issue(issue).tracker}" + Chris@909: " # #{relation.other_issue(issue).id}: #{relation.other_issue(issue).subject}" Chris@1517: buf = buf.truncate(truncate_length) Chris@909: pdf.SetFontStyle('', 8) Chris@909: pdf.RDMCell(35+155-60, 5, buf, "L") Chris@909: pdf.SetFontStyle('B',8) Chris@909: pdf.RDMCell(20,5, relation.other_issue(issue).status.to_s, "") Chris@909: pdf.RDMCell(20,5, format_date(relation.other_issue(issue).start_date), "") Chris@909: pdf.RDMCell(20,5, format_date(relation.other_issue(issue).due_date), "R") Chris@909: pdf.Ln Chris@909: end Chris@909: end Chris@909: pdf.RDMCell(190,5, "", "T") Chris@0: pdf.Ln Chris@441: Chris@441: if issue.changesets.any? && Chris@441: User.current.allowed_to?(:view_changesets, issue.project) Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(190,5, l(:label_associated_revisions), "B") Chris@0: pdf.Ln Chris@0: for changeset in issue.changesets Chris@0: pdf.SetFontStyle('B',8) Chris@507: csstr = "#{l(:label_revision)} #{changeset.format_identifier} - " Chris@507: csstr += format_time(changeset.committed_on) + " - " + changeset.author.to_s Chris@507: pdf.RDMCell(190, 5, csstr) Chris@0: pdf.Ln Chris@0: unless changeset.comments.blank? Chris@0: pdf.SetFontStyle('',8) Chris@909: pdf.RDMwriteHTMLCell(190,5,0,0, Chris@909: changeset.comments.to_s, issue.attachments, "") Chris@441: end Chris@0: pdf.Ln Chris@0: end Chris@0: end Chris@441: Chris@1115: if assoc[:journals].present? Chris@1115: pdf.SetFontStyle('B',9) Chris@1115: pdf.RDMCell(190,5, l(:label_history), "B") Chris@0: pdf.Ln Chris@1115: assoc[:journals].each do |journal| Chris@1115: pdf.SetFontStyle('B',8) Chris@1115: title = "##{journal.indice} - #{format_time(journal.created_on)} - #{journal.user}" Chris@1115: title << " (#{l(:field_private_notes)})" if journal.private_notes? Chris@1115: pdf.RDMCell(190,5, title) Chris@1115: pdf.Ln Chris@1115: pdf.SetFontStyle('I',8) Chris@1464: details_to_strings(journal.visible_details, true).each do |string| Chris@1115: pdf.RDMMultiCell(190,5, "- " + string) Chris@1115: end Chris@1115: if journal.notes? Chris@1115: pdf.Ln unless journal.details.empty? Chris@1115: pdf.SetFontStyle('',8) Chris@1115: pdf.RDMwriteHTMLCell(190,5,0,0, Chris@1115: journal.notes.to_s, issue.attachments, "") Chris@1115: end Chris@1115: pdf.Ln Chris@0: end Chris@0: end Chris@441: Chris@0: if issue.attachments.any? Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(190,5, l(:label_attachment_plural), "B") Chris@0: pdf.Ln Chris@0: for attachment in issue.attachments Chris@0: pdf.SetFontStyle('',8) Chris@441: pdf.RDMCell(80,5, attachment.filename) Chris@441: pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R") Chris@441: pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R") Chris@441: pdf.RDMCell(65,5, attachment.author.name,0,0,"R") Chris@0: pdf.Ln Chris@0: end Chris@0: end Chris@0: pdf.Output Chris@0: end chris@22: Chris@1115: # Returns a PDF string of a set of wiki pages Chris@1115: def wiki_pages_to_pdf(pages, project) Chris@1115: pdf = ITCPDF.new(current_language) Chris@1115: pdf.SetTitle(project.name) Chris@1115: pdf.alias_nb_pages Chris@1115: pdf.footer_date = format_date(Date.today) Chris@1115: pdf.AddPage Chris@1115: pdf.SetFontStyle('B',11) Chris@1115: pdf.RDMMultiCell(190,5, project.name) Chris@1115: pdf.Ln Chris@1115: # Set resize image scale Chris@1115: pdf.SetImageScale(1.6) Chris@1115: pdf.SetFontStyle('',9) Chris@1115: write_page_hierarchy(pdf, pages.group_by(&:parent_id)) Chris@1115: pdf.Output Chris@1115: end Chris@1115: Chris@909: # Returns a PDF string of a single wiki page Chris@1115: def wiki_page_to_pdf(page, project) Chris@909: pdf = ITCPDF.new(current_language) Chris@909: pdf.SetTitle("#{project} - #{page.title}") Chris@909: pdf.alias_nb_pages Chris@909: pdf.footer_date = format_date(Date.today) Chris@909: pdf.AddPage Chris@909: pdf.SetFontStyle('B',11) Chris@909: pdf.RDMMultiCell(190,5, Chris@909: "#{project} - #{page.title} - # #{page.content.version}") Chris@909: pdf.Ln Chris@909: # Set resize image scale Chris@909: pdf.SetImageScale(1.6) Chris@909: pdf.SetFontStyle('',9) Chris@1115: write_wiki_page(pdf, page) Chris@1115: pdf.Output Chris@1115: end Chris@1115: Chris@1115: def write_page_hierarchy(pdf, pages, node=nil, level=0) Chris@1115: if pages[node] Chris@1115: pages[node].each do |page| Chris@1115: if @new_page Chris@1115: pdf.AddPage Chris@1115: else Chris@1115: @new_page = true Chris@1115: end Chris@1115: pdf.Bookmark page.title, level Chris@1115: write_wiki_page(pdf, page) Chris@1115: write_page_hierarchy(pdf, pages, page.id, level + 1) if pages[page.id] Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: def write_wiki_page(pdf, page) Chris@909: pdf.RDMwriteHTMLCell(190,5,0,0, Chris@1115: page.content.text.to_s, page.attachments, 0) Chris@909: if page.attachments.any? Chris@909: pdf.Ln Chris@909: pdf.SetFontStyle('B',9) Chris@909: pdf.RDMCell(190,5, l(:label_attachment_plural), "B") Chris@909: pdf.Ln Chris@909: for attachment in page.attachments Chris@909: pdf.SetFontStyle('',8) Chris@909: pdf.RDMCell(80,5, attachment.filename) Chris@909: pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R") Chris@909: pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R") Chris@909: pdf.RDMCell(65,5, attachment.author.name,0,0,"R") Chris@909: pdf.Ln Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@441: class RDMPdfEncoding Chris@909: def self.rdm_from_utf8(txt, encoding) Chris@441: txt ||= '' Chris@909: txt = Redmine::CodesetUtil.from_utf8(txt, encoding) Chris@441: if txt.respond_to?(:force_encoding) Chris@441: txt.force_encoding('ASCII-8BIT') Chris@441: end Chris@441: txt Chris@441: end Chris@909: Chris@909: def self.attach(attachments, filename, encoding) Chris@909: filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding) Chris@909: atta = nil Chris@909: if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i Chris@909: atta = Attachment.latest_attach(attachments, filename_utf8) Chris@909: end Chris@909: if atta && atta.readable? && atta.visible? Chris@909: return atta Chris@909: else Chris@909: return nil Chris@909: end Chris@909: end Chris@441: end Chris@0: end Chris@0: end Chris@0: end