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@441: require 'fpdf/chinese' Chris@441: require 'fpdf/japanese' Chris@441: require 'fpdf/korean' Chris@909: require 'core/rmagick' 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@909: include IssuesHelper 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@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@441: super('P', '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@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@909: buf = "#{issue.project} - #{issue.tracker} # #{issue.id}" Chris@909: pdf.RDMMultiCell(190, 5, buf) Chris@909: pdf.Ln Chris@909: pdf.SetFontStyle('',8) Chris@909: base_x = pdf.GetX Chris@909: i = 1 Chris@909: issue.ancestors.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@0: pdf.Ln 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@507: y0 = pdf.GetY Chris@507: 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@507: pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY) 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: Chris@909: pdf.SetFontStyle('B',9) Chris@909: pdf.RDMCell(35+155,5, l(:label_subtask_plural) + ":", "LTR") Chris@909: pdf.Ln Chris@909: issue_list(issue.descendants.sort_by(&:lft)) do |child, level| Chris@909: buf = truncate("#{child.tracker} # #{child.id}: #{child.subject}", Chris@909: :length => 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: 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@909: buf = truncate(buf, :length => 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@0: pdf.SetFontStyle('B',9) Chris@441: pdf.RDMCell(190,5, l(:label_history), "B") Chris@441: pdf.Ln Chris@909: indice = 0 Chris@441: for journal in issue.journals.find( Chris@441: :all, :include => [:user, :details], Chris@441: :order => "#{Journal.table_name}.created_on ASC") Chris@909: indice = indice + 1 Chris@0: pdf.SetFontStyle('B',8) Chris@441: pdf.RDMCell(190,5, Chris@909: "#" + indice.to_s + Chris@909: " - " + format_time(journal.created_on) + Chris@909: " - " + 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@909: pdf.RDMwriteHTMLCell(190,5,0,0, Chris@909: journal.notes.to_s, issue.attachments, "") 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@909: # Returns a PDF string of a single wiki page Chris@909: def wiki_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@909: pdf.RDMwriteHTMLCell(190,5,0,0, Chris@909: page.content.text.to_s, page.attachments, "TLRB") 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: pdf.Output 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