Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

feat(pdf-export): add doc index; use columns for detail attribute table

as-op69612d599794
diff --git a/app/models/work_package/pdf_export/common.rb b/app/models/work_package/pdf_export/common.rb
index 1789b108a64..585de2a7f3e 100644
--- a/app/models/work_package/pdf_export/common.rb
+++ b/app/models/work_package/pdf_export/common.rb
@@ -107,14 +107,14 @@ module WorkPackage::PDFExport::Common
end
def get_subject_column_cell(work_package, value)
- make_link_anchor_cell(work_package.id, value)
+ make_link_anchor(work_package.id, value)
end
def make_link_href_cell(href, caption)
"<color rgb='#{overview_table_link_color}'><link href='#{href}'>#{caption}</link></color>"
end
- def make_link_anchor_cell(anchor, caption)
+ def make_link_anchor(anchor, caption)
"<link anchor=\"#{anchor}\">#{caption}</link>"
end
diff --git a/app/models/work_package/pdf_export/markdown.rb b/app/models/work_package/pdf_export/markdown.rb
index ee9f2f3ecf1..a6bc1d5c354 100644
--- a/app/models/work_package/pdf_export/markdown.rb
+++ b/app/models/work_package/pdf_export/markdown.rb
@@ -82,7 +82,7 @@ module WorkPackage::PDFExport::Markdown
def handle_unknown_html_tag(tag, node, opts)
case tag.name
- when 'figure', 'div', 'p'
+ when 'figure', 'div', 'p', 'figcaption'
# nop, but scan children [true, ...]
else
draw_formatted_text([text_hash(tag.to_s, opts)], opts, node)
diff --git a/app/models/work_package/pdf_export/overview_table.rb b/app/models/work_package/pdf_export/overview_table.rb
index cc432b60308..0b8a813ef13 100644
--- a/app/models/work_package/pdf_export/overview_table.rb
+++ b/app/models/work_package/pdf_export/overview_table.rb
@@ -142,7 +142,7 @@ module WorkPackage::PDFExport::OverviewTable
def build_nr_cell(work_package, id_wp_meta_map)
content = "#{id_wp_meta_map[work_package.id][:level_path].join('.')}."
- content = make_link_anchor_cell(work_package.id, content)
+ content = make_link_anchor(work_package.id, content)
build_column_cell(content)
end
diff --git a/app/models/work_package/pdf_export/style.rb b/app/models/work_package/pdf_export/style.rb
index bb6d0bfa3f5..5d264c2593e 100644
--- a/app/models/work_package/pdf_export/style.rb
+++ b/app/models/work_package/pdf_export/style.rb
@@ -113,6 +113,30 @@ module WorkPackage::PDFExport::Style
border_widths: [0.25, 0.25, 0.25, 0.25] }
end
+ def toc_margins_style
+ { margin_bottom: 20 }
+ end
+
+ def toc_item_index_style
+ { size: 10, style: :bold }
+ end
+
+ def toc_item_subject_font_style
+ { size: 10 }
+ end
+
+ def toc_item_subject_indent_style
+ 4
+ end
+
+ def toc_item_page_nr_font_style
+ { size: 10 }
+ end
+
+ def toc_item_margins_style
+ { margin_bottom: 4 }
+ end
+
def wp_headline_margins_style
{ margin_top: 4, margin_bottom: 4 }
end
diff --git a/app/models/work_package/pdf_export/table_of_contents.rb b/app/models/work_package/pdf_export/table_of_contents.rb
new file mode 100644
index 00000000000..787e433e539
--- /dev/null
+++ b/app/models/work_package/pdf_export/table_of_contents.rb
@@ -0,0 +1,86 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module WorkPackage::PDFExport::TableOfContents
+ def write_work_packages_toc!(work_packages, id_wp_meta_map)
+ toc_list = build_toc_data_list work_packages, id_wp_meta_map
+ with_margin(toc_margins_style) do
+ write_toc! toc_list
+ end
+ pdf.start_new_page
+ end
+
+ private
+
+ def build_toc_data_list(work_packages, id_wp_meta_map)
+ work_packages.map do |work_package|
+ level_path = id_wp_meta_map[work_package.id][:level_path]
+ level_string = "#{level_path.join('.')}. "
+ level_string_width = measure_text_width(level_string, toc_item_index_style)
+ title = get_column_value work_package, :subject
+ page_nr_string = (id_wp_meta_map[work_package.id][:page_number] || '000').to_s
+ page_nr_string_width = measure_text_width(page_nr_string, toc_item_page_nr_font_style)
+ { id: work_package.id, level_string:, level_string_width:, title:, page_nr_string:, page_nr_string_width: }
+ end
+ end
+
+ def write_part_float(part, indent_left, indent_right, style)
+ height = 0
+ pdf.float do
+ pdf.indent(indent_left, indent_right) do
+ pdf.text(part, style.merge({ inline_format: true }))
+ end
+ end
+ height
+ end
+
+ def write_toc!(toc_list)
+ max_level_string_width = toc_list.pluck(:level_string_width).max
+ toc_list.each do |toc_item|
+ with_margin(toc_item_margins_style) do
+ write_toc_item! toc_item, max_level_string_width
+ end
+ end
+ end
+
+ def write_toc_item!(toc_item, max_level_string_width)
+ write_part_float(
+ make_link_anchor(toc_item[:id], toc_item[:level_string]),
+ 0, 0, toc_item_index_style)
+ write_part_float(
+ make_link_anchor(toc_item[:id], toc_item[:page_nr_string]),
+ 0, 0, toc_item_page_nr_font_style.merge({ align: :right }))
+ pdf.indent(
+ max_level_string_width + toc_item_subject_indent_style,
+ toc_item[:page_nr_string_width] + toc_item_subject_indent_style
+ ) do
+ pdf.formatted_text([toc_item_subject_font_style.merge({ text: toc_item[:title] })])
+ end
+ end
+
+end
diff --git a/app/models/work_package/pdf_export/work_package_detail.rb b/app/models/work_package/pdf_export/work_package_detail.rb
index e89c076dead..dc242436dd1 100644
--- a/app/models/work_package/pdf_export/work_package_detail.rb
+++ b/app/models/work_package/pdf_export/work_package_detail.rb
@@ -31,16 +31,16 @@ module WorkPackage::PDFExport::WorkPackageDetail
def write_work_packages_details!(work_packages, id_wp_meta_map)
work_packages.each do |work_package|
- id_wp_meta_map[work_package.id][:page_number] = current_page_nr
- write_work_package_detail!(work_package, id_wp_meta_map[work_package.id][:level_path])
+ write_work_package_detail!(work_package, id_wp_meta_map[work_package.id])
end
end
- def write_work_package_detail!(work_package, level_path)
+ def write_work_package_detail!(work_package, id_wp_meta_map_entry)
# TODO: move page break threshold const to style settings and implement conditional break with height measuring
write_optional_page_break(200)
+ id_wp_meta_map_entry[:page_number] = current_page_nr
with_margin(wp_detail_margins_style) do
- write_work_package_subject! work_package, level_path
+ write_work_package_subject! work_package, id_wp_meta_map_entry[:level_path]
write_work_package_detail_content! work_package
end
end
@@ -72,7 +72,11 @@ module WorkPackage::PDFExport::WorkPackageDetail
end
def write_attributes_table!(work_package)
- rows = build_attributes_table_rows work_package
+ rows = if respond_to?(:column_objects)
+ build_columns_table_rows(work_package)
+ else
+ build_attributes_table_rows(work_package)
+ end
with_margin(wp_attributes_table_margins_style) do
pdf.table(
rows,
@@ -89,6 +93,14 @@ module WorkPackage::PDFExport::WorkPackageDetail
widths.map { |w| w * ratio }
end
+ def build_columns_table_rows(work_package)
+ list = column_objects
+ 0.step(list.length - 1, 2).map do |i|
+ build_columns_table_cells(list[i], work_package) +
+ build_columns_table_cells(list[i + 1], work_package)
+ end
+ end
+
def build_attributes_table_rows(work_package)
# get work package attribute table rows data [[label, value, label, value]]
attrs = %i[
@@ -115,10 +127,20 @@ module WorkPackage::PDFExport::WorkPackageDetail
# get work package attribute table cell data: [label, value]
return ['', ''] if attribute.nil?
- label = (WorkPackage.human_attribute_name(attribute) || '').upcase
+ build_attributes_row(WorkPackage.human_attribute_name(attribute) || '', attribute.to_sym, work_package)
+ end
+
+ def build_columns_table_cells(column, work_package)
+ return ['', ''] if column.nil?
+
+ build_attributes_row(column.caption || '', column.name, work_package)
+ end
+
+ def build_attributes_row(label, col_name, work_package)
+ # get work package attribute table cell data: [label, value]
[
- pdf.make_cell(label, wp_attributes_table_label_font_style),
- get_column_value_cell(work_package, attribute.to_sym)
+ pdf.make_cell(label.upcase, wp_attributes_table_label_font_style),
+ get_column_value_cell(work_package, col_name)
]
end
diff --git a/app/models/work_package/pdf_export/work_package_list_to_pdf.rb b/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
index 9b27b6055b0..95efb048af6 100644
--- a/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
+++ b/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
@@ -45,6 +45,7 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
include WorkPackage::PDFExport::Attachments
include WorkPackage::PDFExport::OverviewTable
include WorkPackage::PDFExport::WorkPackageDetail
+ include WorkPackage::PDFExport::TableOfContents
include WorkPackage::PDFExport::Page
include WorkPackage::PDFExport::Style
@@ -100,7 +101,8 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
def render_work_packages_pdfs(work_packages, filename)
write_title!
- write_work_packages_overview! work_packages, @id_wp_meta_map
+ write_work_packages_toc! work_packages, @id_wp_meta_map if with_descriptions?
+ write_work_packages_overview! work_packages, @id_wp_meta_map unless with_descriptions?
if should_be_batched?(work_packages)
render_batched(work_packages, filename)
else