Chris@0: # encoding: utf-8 Chris@0: # Chris@0: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 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@0: require 'iconv' Chris@0: require 'rfpdf/fpdf' Chris@441: require 'fpdf/chinese' Chris@441: require 'fpdf/japanese' Chris@441: require 'fpdf/korean' Chris@0: 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@441: Chris@441: class ITCPDF < TCPDF Chris@0: include Redmine::I18n Chris@0: attr_accessor :footer_date Chris@441: Chris@0: def initialize(lang) Chris@0: set_language_if_valid lang Chris@441: pdf_encoding = l(:general_pdf_encoding).upcase Chris@441: if RUBY_VERSION < '1.9' Chris@441: @ic = Iconv.new(pdf_encoding, 'UTF-8') Chris@441: end Chris@441: super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding) Chris@441: case pdf_encoding Chris@441: when 'UTF-8' Chris@441: @font_for_content = 'FreeSans' Chris@441: @font_for_footer = 'FreeSans' Chris@441: when 'CP949' Chris@0: extend(PDF_Korean) Chris@0: AddUHCFont() Chris@0: @font_for_content = 'UHC' Chris@441: @font_for_footer = 'UHC' Chris@441: when 'CP932' Chris@0: extend(PDF_Japanese) Chris@0: AddSJISFont() Chris@0: @font_for_content = 'SJIS' Chris@441: @font_for_footer = 'SJIS' Chris@441: when 'GB18030' Chris@0: extend(PDF_Chinese) Chris@0: AddGBFont() Chris@0: @font_for_content = 'GB' Chris@441: @font_for_footer = 'GB' Chris@441: when 'BIG5' Chris@0: extend(PDF_Chinese) Chris@0: AddBig5Font() Chris@0: @font_for_content = 'Big5' Chris@441: @font_for_footer = 'Big5' Chris@0: else Chris@0: @font_for_content = 'Arial' Chris@441: @font_for_footer = 'Helvetica' Chris@0: end Chris@0: SetCreator(Redmine::Info.app_name) Chris@0: SetFont(@font_for_content) 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@0: utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', 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 =~ /^ space_left Chris@441: pdf.AddPage("L") 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: pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1) Chris@441: issues_to_pdf_write_cells(pdf, col_values, col_width, row_height) Chris@441: issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width) Chris@441: pdf.SetY(base_y + max_height); 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@441: def issues_to_pdf_write_cells(pdf, col_values, col_widths, Chris@441: 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@441: def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y, Chris@441: id_width, col_widths) Chris@441: col_x = top_x + id_width 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@0: def issue_to_pdf(issue) Chris@441: pdf = ITCPDF.new(current_language) Chris@0: 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@441: pdf.RDMMultiCell(190,5, Chris@441: "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}") Chris@0: pdf.Ln Chris@441: Chris@0: y0 = pdf.GetY Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_status) + ":","LT") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, issue.status.to_s,"RT") Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_priority) + ":","LT") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, issue.priority.to_s,"RT") Chris@0: pdf.Ln Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_author) + ":","L") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, issue.author.to_s,"R") Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_category) + ":","L") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, issue.category.to_s,"R") Chris@441: pdf.Ln Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_created_on) + ":","L") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, format_date(issue.created_on),"R") Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, issue.assigned_to.to_s,"R") Chris@0: pdf.Ln Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, format_date(issue.updated_on),"RB") Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_due_date) + ":","LB") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMCell(60,5, format_date(issue.due_date),"RB") Chris@0: pdf.Ln Chris@441: Chris@0: for custom_value in issue.custom_field_values Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMMultiCell(155,5, (show_value custom_value),"R") Chris@0: end Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_subject) + ":","LT") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMMultiCell(155,5, issue.subject,"RT") Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(35,5, l(:field_description) + ":","LT") Chris@0: pdf.SetFontStyle('',9) Chris@441: pdf.RDMMultiCell(155,5, issue.description.to_s,"RT") Chris@441: Chris@0: pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY) Chris@441: pdf.Line(pdf.GetX, pdf.GetY, pdf.GetX + 190, pdf.GetY) 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@441: pdf.RDMCell(190,5, Chris@441: format_time(changeset.committed_on) + " - " + changeset.author.to_s) Chris@0: pdf.Ln Chris@0: unless changeset.comments.blank? Chris@0: pdf.SetFontStyle('',8) Chris@441: pdf.RDMMultiCell(190,5, changeset.comments.to_s) Chris@441: end Chris@0: pdf.Ln Chris@0: end Chris@0: end Chris@441: Chris@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(190,5, l(:label_history), "B") Chris@441: pdf.Ln Chris@441: for journal in issue.journals.find( Chris@441: :all, :include => [:user, :details], Chris@441: :order => "#{Journal.table_name}.created_on ASC") Chris@0: pdf.SetFontStyle('B',8) Chris@441: pdf.RDMCell(190,5, Chris@441: format_time(journal.created_on) + " - " + journal.user.name) Chris@0: pdf.Ln Chris@0: pdf.SetFontStyle('I',8) Chris@0: for detail in journal.details Chris@441: pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true)) Chris@0: end Chris@0: if journal.notes? Chris@441: pdf.Ln unless journal.details.empty? Chris@0: pdf.SetFontStyle('',8) Chris@441: pdf.RDMMultiCell(190,5, journal.notes.to_s) Chris@441: end Chris@0: pdf.Ln 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@441: class RDMPdfEncoding Chris@441: include Redmine::I18n Chris@441: def self.rdm_pdf_iconv(ic, txt) Chris@441: txt ||= '' Chris@441: if txt.respond_to?(:force_encoding) Chris@441: txt.force_encoding('UTF-8') Chris@441: if l(:general_pdf_encoding).upcase != 'UTF-8' Chris@441: txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace, Chris@441: :undef => :replace, :replace => '?') Chris@441: else Chris@441: txt = Redmine::CodesetUtil.replace_invalid_utf8(txt) Chris@441: end Chris@441: txt.force_encoding('ASCII-8BIT') Chris@441: else Chris@441: ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8') Chris@441: txtar = "" Chris@441: begin Chris@441: txtar += ic.iconv(txt) Chris@441: rescue Iconv::IllegalSequence Chris@441: txtar += $!.success Chris@441: txt = '?' + $!.failed[1,$!.failed.length] Chris@441: retry Chris@441: rescue Chris@441: txtar += $!.success Chris@441: end Chris@441: txt = txtar Chris@441: end Chris@441: txt Chris@441: end Chris@441: end Chris@0: end Chris@0: end Chris@0: end