"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/lib/redmine/export/pdf/issues_pdf_helper.rb" (6 Apr 2020, 22577 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "issues_pdf_helper.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    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.
   10 #
   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.
   15 #
   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.
   19 
   20 module Redmine
   21   module Export
   22     module PDF
   23       module IssuesPdfHelper
   24         # Returns a PDF string of a single issue
   25         def issue_to_pdf(issue, assoc={})
   26           pdf = ITCPDF.new(current_language)
   27           pdf.set_title("#{issue.project} - #{issue.tracker} ##{issue.id}")
   28           pdf.alias_nb_pages
   29           pdf.footer_date = format_date(User.current.today)
   30           pdf.add_page
   31           pdf.SetFontStyle('B',11)
   32           buf = "#{issue.project} - #{issue.tracker} ##{issue.id}"
   33           pdf.RDMMultiCell(190, 5, buf)
   34           pdf.SetFontStyle('',8)
   35           base_x = pdf.get_x
   36           i = 1
   37           issue.ancestors.visible.each do |ancestor|
   38             pdf.set_x(base_x + i)
   39             buf = "#{ancestor.tracker} # #{ancestor.id} (#{ancestor.status.to_s}): #{ancestor.subject}"
   40             pdf.RDMMultiCell(190 - i, 5, buf)
   41             i += 1 if i < 35
   42           end
   43           pdf.SetFontStyle('B',11)
   44           pdf.RDMMultiCell(190 - i, 5, issue.subject.to_s)
   45           pdf.SetFontStyle('',8)
   46           pdf.RDMMultiCell(190, 5, "#{format_time(issue.created_on)} - #{issue.author}")
   47           pdf.ln
   48 
   49           left = []
   50           left << [l(:field_status), issue.status]
   51           left << [l(:field_priority), issue.priority]
   52           left << [l(:field_assigned_to), issue.assigned_to] unless issue.disabled_core_fields.include?('assigned_to_id')
   53           left << [l(:field_category), issue.category] unless issue.disabled_core_fields.include?('category_id')
   54           left << [l(:field_fixed_version), issue.fixed_version] unless issue.disabled_core_fields.include?('fixed_version_id')
   55 
   56           right = []
   57           right << [l(:field_start_date), format_date(issue.start_date)] unless issue.disabled_core_fields.include?('start_date')
   58           right << [l(:field_due_date), format_date(issue.due_date)] unless issue.disabled_core_fields.include?('due_date')
   59           right << [l(:field_done_ratio), "#{issue.done_ratio}%"] unless issue.disabled_core_fields.include?('done_ratio')
   60           right << [l(:field_estimated_hours), l_hours(issue.estimated_hours)] unless issue.disabled_core_fields.include?('estimated_hours')
   61           right << [l(:label_spent_time), l_hours(issue.total_spent_hours)] if User.current.allowed_to?(:view_time_entries, issue.project)
   62 
   63           rows = left.size > right.size ? left.size : right.size
   64           left  << nil while left.size  < rows
   65           right << nil while right.size < rows
   66 
   67           custom_field_values = issue.visible_custom_field_values.reject {|value| value.custom_field.full_width_layout?}
   68           half = (custom_field_values.size / 2.0).ceil
   69           custom_field_values.each_with_index do |custom_value, i|
   70             (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value, false)]
   71           end
   72 
   73           if pdf.get_rtl
   74             border_first_top = 'RT'
   75             border_last_top  = 'LT'
   76             border_first = 'R'
   77             border_last  = 'L'
   78           else
   79             border_first_top = 'LT'
   80             border_last_top  = 'RT'
   81             border_first = 'L'
   82             border_last  = 'R'
   83           end
   84 
   85           rows = left.size > right.size ? left.size : right.size
   86           rows.times do |i|
   87             heights = []
   88             pdf.SetFontStyle('B',9)
   89             item = left[i]
   90             heights << pdf.get_string_height(35, item ? "#{item.first}:" : "")
   91             item = right[i]
   92             heights << pdf.get_string_height(35, item ? "#{item.first}:" : "")
   93             pdf.SetFontStyle('',9)
   94             item = left[i]
   95             heights << pdf.get_string_height(60, item ? item.last.to_s  : "")
   96             item = right[i]
   97             heights << pdf.get_string_height(60, item ? item.last.to_s  : "")
   98             height = heights.max
   99 
  100             item = left[i]
  101             pdf.SetFontStyle('B',9)
  102             pdf.RDMMultiCell(35, height, item ? "#{item.first}:" : "", (i == 0 ? border_first_top : border_first), '', 0, 0)
  103             pdf.SetFontStyle('',9)
  104             pdf.RDMMultiCell(60, height, item ? item.last.to_s : "", (i == 0 ? border_last_top : border_last), '', 0, 0)
  105 
  106             item = right[i]
  107             pdf.SetFontStyle('B',9)
  108             pdf.RDMMultiCell(35, height, item ? "#{item.first}:" : "",  (i == 0 ? border_first_top : border_first), '', 0, 0)
  109             pdf.SetFontStyle('',9)
  110             pdf.RDMMultiCell(60, height, item ? item.last.to_s : "", (i == 0 ? border_last_top : border_last), '', 0, 2)
  111 
  112             pdf.set_x(base_x)
  113           end
  114 
  115           pdf.SetFontStyle('B',9)
  116           pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
  117           pdf.SetFontStyle('',9)
  118 
  119           # Set resize image scale
  120           pdf.set_image_scale(1.6)
  121           text =
  122             textilizable(
  123               issue, :description,
  124               :only_path => false,
  125               :edit_section_links => false,
  126               :headings => false,
  127               :inline_attachments => false
  128             )
  129           pdf.RDMwriteFormattedCell(35+155, 5, '', '', text, issue.attachments, "LRB")
  130 
  131           custom_field_values = issue.visible_custom_field_values.select {|value| value.custom_field.full_width_layout?}
  132           custom_field_values.each do |value|
  133             text = show_value(value, false)
  134             next if text.blank?
  135 
  136             pdf.SetFontStyle('B',9)
  137             pdf.RDMCell(35+155, 5, value.custom_field.name, "LRT", 1)
  138             pdf.SetFontStyle('',9)
  139             pdf.RDMwriteHTMLCell(35+155, 5, '', '', text, issue.attachments, "LRB")
  140           end
  141 
  142           unless issue.leaf?
  143             truncate_length = (!is_cjk? ? 90 : 65)
  144             pdf.SetFontStyle('B',9)
  145             pdf.RDMCell(35+155,5, l(:label_subtask_plural) + ":", "LTR")
  146             pdf.ln
  147             issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
  148               buf = "#{child.tracker} # #{child.id}: #{child.subject}".
  149                       truncate(truncate_length)
  150               level = 10 if level >= 10
  151               pdf.SetFontStyle('',8)
  152               pdf.RDMCell(35+135,5, (level >=1 ? "  " * level : "") + buf, border_first)
  153               pdf.SetFontStyle('B',8)
  154               pdf.RDMCell(20,5, child.status.to_s, border_last)
  155               pdf.ln
  156             end
  157           end
  158 
  159           relations = issue.relations.select { |r| r.other_issue(issue).visible? }
  160           unless relations.empty?
  161             truncate_length = (!is_cjk? ? 80 : 60)
  162             pdf.SetFontStyle('B',9)
  163             pdf.RDMCell(35+155,5, l(:label_related_issues) + ":", "LTR")
  164             pdf.ln
  165             relations.each do |relation|
  166               buf = relation.to_s(issue) {|other|
  167                 text = ""
  168                 if Setting.cross_project_issue_relations?
  169                   text += "#{relation.other_issue(issue).project} - "
  170                 end
  171                 text += "#{other.tracker} ##{other.id}: #{other.subject}"
  172                 text
  173               }
  174               buf = buf.truncate(truncate_length)
  175               pdf.SetFontStyle('', 8)
  176               pdf.RDMCell(35+155-60, 5, buf, border_first)
  177               pdf.SetFontStyle('B',8)
  178               pdf.RDMCell(20,5, relation.other_issue(issue).status.to_s, "")
  179               pdf.RDMCell(20,5, format_date(relation.other_issue(issue).start_date), "")
  180               pdf.RDMCell(20,5, format_date(relation.other_issue(issue).due_date), border_last)
  181               pdf.ln
  182             end
  183           end
  184           pdf.RDMCell(190,5, "", "T")
  185           pdf.ln
  186 
  187           if issue.changesets.any? &&
  188                User.current.allowed_to?(:view_changesets, issue.project)
  189             pdf.SetFontStyle('B',9)
  190             pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
  191             pdf.ln
  192             for changeset in issue.changesets
  193               pdf.SetFontStyle('B',8)
  194               csstr  = "#{l(:label_revision)} #{changeset.format_identifier} - "
  195               csstr += format_time(changeset.committed_on) + " - " + changeset.author.to_s
  196               pdf.RDMCell(190, 5, csstr)
  197               pdf.ln
  198               unless changeset.comments.blank?
  199                 pdf.SetFontStyle('',8)
  200                 pdf.RDMwriteHTMLCell(
  201                       190,5,'','',
  202                       changeset.comments.to_s, issue.attachments, "")
  203               end
  204               pdf.ln
  205             end
  206           end
  207 
  208           if assoc[:journals].present?
  209             pdf.SetFontStyle('B',9)
  210             pdf.RDMCell(190,5, l(:label_history), "B")
  211             pdf.ln
  212             assoc[:journals].each do |journal|
  213               pdf.SetFontStyle('B',8)
  214               title = "##{journal.indice} - #{format_time(journal.created_on)} - #{journal.user}"
  215               title += " (#{l(:field_private_notes)})" if journal.private_notes?
  216               pdf.RDMCell(190,5, title)
  217               pdf.ln
  218               pdf.SetFontStyle('I',8)
  219               details_to_strings(journal.visible_details, true).each do |string|
  220                 pdf.RDMMultiCell(190,5, "- " + string)
  221               end
  222               if journal.notes?
  223                 pdf.ln unless journal.details.empty?
  224                 pdf.SetFontStyle('',8)
  225                 text =
  226                   textilizable(
  227                     journal, :notes,
  228                     :only_path => false,
  229                     :edit_section_links => false,
  230                     :headings => false,
  231                     :inline_attachments => false
  232                   )
  233                 pdf.RDMwriteFormattedCell(190,5,'','', text, issue.attachments, "")
  234               end
  235               pdf.ln
  236             end
  237           end
  238 
  239           if issue.attachments.any?
  240             pdf.SetFontStyle('B',9)
  241             pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
  242             pdf.ln
  243             for attachment in issue.attachments
  244               pdf.SetFontStyle('',8)
  245               pdf.RDMCell(80,5, attachment.filename)
  246               pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
  247               pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
  248               pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
  249               pdf.ln
  250             end
  251           end
  252           pdf.output
  253         end
  254 
  255         # Returns a PDF string of a list of issues
  256         def issues_to_pdf(issues, project, query)
  257           pdf = ITCPDF.new(current_language, "L")
  258           title = query.new_record? ? l(:label_issue_plural) : query.name
  259           title = "#{project} - #{title}" if project
  260           pdf.set_title(title)
  261           pdf.alias_nb_pages
  262           pdf.footer_date = format_date(User.current.today)
  263           pdf.set_auto_page_break(false)
  264           pdf.add_page("L")
  265 
  266           # Landscape A4 = 210 x 297 mm
  267           page_height   = pdf.get_page_height # 210
  268           page_width    = pdf.get_page_width  # 297
  269           left_margin   = pdf.get_original_margins['left'] # 10
  270           right_margin  = pdf.get_original_margins['right'] # 10
  271           bottom_margin = pdf.get_footer_margin
  272           row_height    = 4
  273 
  274           # column widths
  275           table_width = page_width - right_margin - left_margin
  276           col_width = []
  277           unless query.inline_columns.empty?
  278             col_width = calc_col_width(issues, query, table_width, pdf)
  279             table_width = col_width.inject(0, :+)
  280           end
  281 
  282           # use full width if the query has block columns (description, last_notes or full width custom fieds)
  283           if table_width > 0 && query.block_columns.any?
  284             col_width = col_width.map {|w| w * (page_width - right_margin - left_margin) / table_width}
  285             table_width = col_width.inject(0, :+)
  286           end
  287 
  288           # title
  289           pdf.SetFontStyle('B',11)
  290           pdf.RDMCell(190, 8, title)
  291           pdf.ln
  292 
  293           # totals
  294           totals = query.totals.map {|column, total| "#{column.caption}: #{total}"}
  295           if totals.present?
  296             pdf.SetFontStyle('B',10)
  297             pdf.RDMCell(table_width, 6, totals.join("  "), 0, 1, 'R')
  298           end
  299 
  300           totals_by_group = query.totals_by_group
  301           render_table_header(pdf, query, col_width, row_height, table_width)
  302           previous_group = false
  303           result_count_by_group = query.result_count_by_group
  304 
  305           issue_list(issues) do |issue, level|
  306             if query.grouped? &&
  307                  (group = query.group_by_column.group_value(issue)) != previous_group
  308               pdf.SetFontStyle('B',10)
  309               group_label = group.blank? ? +'None' : group.to_s.dup
  310               group_label << " (#{result_count_by_group[group]})"
  311               pdf.bookmark group_label, 0, -1
  312               pdf.RDMCell(table_width, row_height * 2, group_label, 'LR', 1, 'L')
  313               pdf.SetFontStyle('',8)
  314 
  315               totals = totals_by_group.map {|column, total| "#{column.caption}: #{total[group]}"}.join("  ")
  316               if totals.present?
  317                 pdf.RDMCell(table_width, row_height, totals, 'LR', 1, 'L')
  318               end
  319               previous_group = group
  320             end
  321 
  322             # fetch row values
  323             col_values = fetch_row_values(issue, query, level)
  324 
  325             # make new page if it doesn't fit on the current one
  326             base_y     = pdf.get_y
  327             max_height = get_issues_to_pdf_write_cells(pdf, col_values, col_width)
  328             space_left = page_height - base_y - bottom_margin
  329             if max_height > space_left
  330               pdf.add_page("L")
  331               render_table_header(pdf, query, col_width, row_height, table_width)
  332               base_y = pdf.get_y
  333             end
  334 
  335             # write the cells on page
  336             issues_to_pdf_write_cells(pdf, col_values, col_width, max_height)
  337             pdf.set_y(base_y + max_height)
  338 
  339             query.block_columns.each do |column|
  340             if column.is_a?(QueryCustomFieldColumn)
  341               cv = issue.visible_custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
  342               text = show_value(cv, false)
  343             else
  344               text = issue.send(column.name)
  345             end
  346             next if text.blank?
  347 
  348             pdf.set_x(10)
  349             pdf.set_auto_page_break(true, bottom_margin)
  350             pdf.SetFontStyle('B',9)
  351             pdf.RDMCell(0, 5, column.caption, "LRT", 1)
  352             pdf.SetFontStyle('',9)
  353             pdf.RDMwriteHTMLCell(0, 5, '', '', text, [], "LRB")
  354             pdf.set_auto_page_break(false)
  355             end
  356           end
  357 
  358           if issues.size == Setting.issues_export_limit.to_i
  359             pdf.SetFontStyle('B',10)
  360             pdf.RDMCell(0, row_height, '...')
  361           end
  362           pdf.output
  363         end
  364 
  365         def is_cjk?
  366           case current_language.to_s.downcase
  367           when 'ja', 'zh-tw', 'zh', 'ko'
  368             true
  369           else
  370             false
  371           end
  372         end
  373 
  374         # fetch row values
  375         def fetch_row_values(issue, query, level)
  376           query.inline_columns.collect do |column|
  377             s =
  378               if column.is_a?(QueryCustomFieldColumn)
  379                 cv = issue.visible_custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
  380                 show_value(cv, false)
  381               else
  382                 value = column.value_object(issue)
  383                 case column.name
  384                 when :subject
  385                   value = "  " * level + value
  386                 when :attachments
  387                   value = value.to_a.map {|a| a.filename}.join("\n")
  388                 end
  389                 if value.is_a?(Date)
  390                   format_date(value)
  391                 elsif value.is_a?(Time)
  392                   format_time(value)
  393                 elsif value.is_a?(Float)
  394                   sprintf "%.2f", value
  395                 else
  396                   value
  397                 end
  398               end
  399             s.to_s
  400           end
  401         end
  402 
  403         # calculate columns width
  404         def calc_col_width(issues, query, table_width, pdf)
  405           # calculate statistics
  406           #  by captions
  407           pdf.SetFontStyle('B',8)
  408           margins = pdf.get_margins
  409           col_padding = margins['cell']
  410           col_width_min = query.inline_columns.map {|v| pdf.get_string_width(v.caption) + col_padding}
  411           col_width_max = Array.new(col_width_min)
  412           col_width_avg = Array.new(col_width_min)
  413           col_min = pdf.get_string_width('OO') + col_padding * 2
  414           if table_width > col_min * col_width_avg.length
  415             table_width -= col_min * col_width_avg.length
  416           else
  417             col_min = pdf.get_string_width('O') + col_padding * 2
  418             if table_width > col_min * col_width_avg.length
  419               table_width -= col_min * col_width_avg.length
  420             else
  421               ratio = table_width / col_width_avg.inject(0, :+)
  422               return col_width = col_width_avg.map {|w| w * ratio}
  423             end
  424           end
  425           word_width_max = query.inline_columns.map {|c|
  426             n = 10
  427             c.caption.split.each {|w|
  428               x = pdf.get_string_width(w) + col_padding
  429               n = x if n < x
  430             }
  431             n
  432           }
  433 
  434           #  by properties of issues
  435           pdf.SetFontStyle('',8)
  436           k = 1
  437           issue_list(issues) {|issue, level|
  438             k += 1
  439             values = fetch_row_values(issue, query, level)
  440             values.each_with_index {|v,i|
  441               n = pdf.get_string_width(v) + col_padding * 2
  442               col_width_max[i] = n if col_width_max[i] < n
  443               col_width_min[i] = n if col_width_min[i] > n
  444               col_width_avg[i] += n
  445               v.split.each {|w|
  446                 x = pdf.get_string_width(w) + col_padding
  447                 word_width_max[i] = x if word_width_max[i] < x
  448               }
  449             }
  450           }
  451           col_width_avg.map! {|x| x / k}
  452 
  453           # calculate columns width
  454           ratio = table_width / col_width_avg.inject(0, :+)
  455           col_width = col_width_avg.map {|w| w * ratio}
  456 
  457           # correct max word width if too many columns
  458           ratio = table_width / word_width_max.inject(0, :+)
  459           word_width_max.map! {|v| v * ratio} if ratio < 1
  460 
  461           # correct and lock width of some columns
  462           done = 1
  463           col_fix = []
  464           col_width.each_with_index do |w,i|
  465             if w > col_width_max[i]
  466               col_width[i] = col_width_max[i]
  467               col_fix[i] = 1
  468               done = 0
  469             elsif w < word_width_max[i]
  470               col_width[i] = word_width_max[i]
  471               col_fix[i] = 1
  472               done = 0
  473             else
  474               col_fix[i] = 0
  475             end
  476           end
  477 
  478           # iterate while need to correct and lock coluns width
  479           while done == 0
  480             # calculate free & locked columns width
  481             done = 1
  482             ratio = table_width / col_width.inject(0, :+)
  483 
  484             # correct columns width
  485             col_width.each_with_index do |w,i|
  486               if col_fix[i] == 0
  487                 col_width[i] = w * ratio
  488 
  489                 # check if column width less then max word width
  490                 if col_width[i] < word_width_max[i]
  491                   col_width[i] = word_width_max[i]
  492                   col_fix[i] = 1
  493                   done = 0
  494                 elsif col_width[i] > col_width_max[i]
  495                   col_width[i] = col_width_max[i]
  496                   col_fix[i] = 1
  497                   done = 0
  498                 end
  499               end
  500             end
  501           end
  502 
  503           ratio = table_width / col_width.inject(0, :+)
  504           col_width.map! {|v| v * ratio + col_min}
  505           col_width
  506         end
  507 
  508         def render_table_header(pdf, query, col_width, row_height, table_width)
  509           # headers
  510           pdf.SetFontStyle('B',8)
  511           pdf.set_fill_color(230, 230, 230)
  512 
  513           base_x     = pdf.get_x
  514           base_y     = pdf.get_y
  515           max_height = get_issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, true)
  516 
  517           # write the cells on page
  518           issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, max_height, true)
  519           pdf.set_xy(base_x, base_y + max_height)
  520 
  521           # rows
  522           pdf.SetFontStyle('',8)
  523           pdf.set_fill_color(255, 255, 255)
  524         end
  525 
  526         # returns the maximum height of MultiCells
  527         def get_issues_to_pdf_write_cells(pdf, col_values, col_widths, head=false)
  528           heights = []
  529           col_values.each_with_index do |column, i|
  530             heights << pdf.get_string_height(col_widths[i], head ? column.caption : column)
  531           end
  532           return heights.max
  533         end
  534 
  535         # Renders MultiCells and returns the maximum height used
  536         def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height, head=false)
  537           col_values.each_with_index do |column, i|
  538             pdf.RDMMultiCell(col_widths[i], row_height, head ? column.caption : column.strip, 1, '', 1, 0)
  539           end
  540         end
  541 
  542         # Draw lines to close the row (MultiCell border drawing in not uniform)
  543         #
  544         #  parameter "col_id_width" is not used. it is kept for compatibility.
  545         def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y,
  546                                        col_id_width, col_widths, rtl=false)
  547           col_x = top_x
  548           pdf.line(col_x, top_y, col_x, lower_y)    # id right border
  549           col_widths.each do |width|
  550             if rtl
  551               col_x -= width
  552             else
  553               col_x += width
  554             end
  555             pdf.line(col_x, top_y, col_x, lower_y)  # columns right border
  556           end
  557           pdf.line(top_x, top_y, top_x, lower_y)    # left border
  558           pdf.line(top_x, lower_y, col_x, lower_y)  # bottom border
  559         end
  560       end
  561     end
  562   end
  563 end