[#48857] Cover page for pdf reports

https://community.openproject.org/work_packages/48857
This commit is contained in:
as-op
2023-08-07 17:22:12 +02:00
parent c9ef83afa0
commit 9a78fed7b1
6 changed files with 266 additions and 19 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

+34 -6
View File
@@ -91,7 +91,7 @@ module WorkPackage::PDFExport::Common
def get_column_value_cell(work_package, column_name)
value = get_column_value(work_package, column_name)
return get_id_column_cell(work_package, value) if column_name == :id
return get_subject_column_cell(work_package, value) if with_descriptions? && column_name == :subject
return get_subject_column_cell(work_package, value) if is_report? && column_name == :subject
escape_tags(value)
end
@@ -154,15 +154,39 @@ module WorkPackage::PDFExport::Common
end
end
def formatted_text_box_measured(formatted_text_array, options)
features_box = ::Prawn::Text::Formatted::Box.new(formatted_text_array, options.merge({ document: pdf }))
features_box.render
features_box.height
end
def draw_horizontal_line(top, left, right, height, color)
pdf.stroke do
pdf.stroke_color = color
pdf.line_width = height
pdf.horizontal_line left, right, at: top
end
end
def draw_styled_text(text, opts)
color_before = pdf.fill_color
@pdf.save_font do
@pdf.font(opts[:font], opts) if opts[:font]
@pdf.fill_color = opts[:color] if opts[:color]
@pdf.draw_text(text, opts)
end
pdf.fill_color = color_before
end
def draw_text_centered(text, text_style, top)
text_width = measure_text_width(text, text_style)
text_x = (pdf.bounds.width - text_width) / 2
pdf.draw_text text, text_style.merge({ at: [text_x, top] })
draw_styled_text text, text_style.merge({ at: [text_x, top] })
[text_x, text_width]
end
def draw_text_multiline_part(line, text_style, x_position, y_position)
pdf.draw_text line, text_style.merge({ at: [x_position, y_position] })
draw_styled_text line, text_style.merge({ at: [x_position, y_position] })
measure_text_height(line, text_style)
end
@@ -197,7 +221,7 @@ module WorkPackage::PDFExport::Common
lines = split_wrapped_lines(text, available_width, text_style)
if lines.length > max_lines
lines[max_lines - 1] = truncate_ellipsis(lines[max_lines - 1], available_width, text_style)
lines = lines.first(3)
lines = lines.first(max_lines)
end
lines
end
@@ -249,10 +273,14 @@ module WorkPackage::PDFExport::Common
@group_sums[group] || {}
end
def with_descriptions?
def is_report?
options[:show_report]
end
def with_cover?
false
end
def with_sums_table?
query.display_sums?
end
@@ -271,6 +299,6 @@ module WorkPackage::PDFExport::Common
end
def current_page_nr
pdf.page_number + @page_count
pdf.page_number + @page_count - (with_cover? ? 1 : 0)
end
end
+136
View File
@@ -0,0 +1,136 @@
#-- 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.
#++
## TODO move constants into style
module WorkPackage::PDFExport::Cover
def write_cover_page!
top = pdf.bounds.top
logo_width = write_cover_logo(top)
write_cover_header(top, logo_width + styles.cover_logo_header_spacing)
write_cover_hr
write_cover_artwork
write_cover_footer
pdf.start_new_page
end
def write_cover_artwork
max_width = pdf.bounds.width - styles.cover_art_padding_right
float_top = write_background_image
float_top -= write_artwork_headline(float_top, max_width) if project
float_top -= write_artwork_title(float_top, max_width)
write_artwork_subheading(float_top, max_width) unless User.current.nil?
end
def available_title_height(current_y)
current_y -
styles.cover_art_headline_max_height -
styles.cover_art_subheading_max_height -
styles.cover_art_headline_spacing -
styles.cover_art_title_spacing
end
def write_cover_hr
hr_style = styles.cover_header_border
draw_horizontal_line(
pdf.bounds.height - hr_style[:offset],
pdf.bounds.left, pdf.bounds.right,
hr_style[:height], hr_style[:color]
)
end
def write_cover_header(top, max_left)
draw_text_multiline_right(
text: heading.upcase, max_left:,
text_style: styles.cover_header, top: top + styles.cover_header_offset, max_lines: 1
)
end
def write_artwork_headline(top, width)
text_style = styles.cover_art_headline
formatted_text_box_measured(
[text_style.merge({ text: project.name, size: nil, leading: nil })],
size: text_style[:size], leading: text_style[:leading],
at: [0, top], width:, height: styles.cover_art_headline_max_height, overflow: :shrink_to_fit
) + styles.cover_art_headline_spacing
end
def write_artwork_title(top, width)
max_title_height = available_title_height(top)
text_style = styles.cover_art_title
formatted_text_box_measured(
[text_style.merge({ text: heading, size: nil, leading: nil })],
size: text_style[:size], leading: text_style[:leading],
at: [0, top], width:, height: max_title_height, overflow: :shrink_to_fit
) + styles.cover_art_title_spacing
end
def write_artwork_subheading(top, width)
text_style = styles.cover_art_author
pdf.formatted_text_box(
[text_style.merge({ text: User.current.name, size: nil, leading: nil })],
size: text_style[:size], leading: text_style[:leading],
at: [0, top], width:, height: styles.cover_art_subheading_max_height, overflow: :shrink_to_fit)
end
def write_cover_footer
draw_text_multiline_left(
text: footer_date,
max_left: pdf.bounds.width / 2,
max_lines: 1,
top: pdf.bounds.bottom - styles.cover_footer_offset,
text_style: styles.cover_footer
)
end
def write_cover_logo(top)
image_obj, image_info = logo_image
height = styles.cover_header_logo_height
scale = [height / image_info.height.to_f, 1].min
pdf.embed_image image_obj, image_info, { at: [0, top + height], scale: }
image_info.width.to_f * scale
end
def cover_background_image
image_file = Rails.root.join("app/assets/images/pdf/cover.png")
image_obj, image_info = pdf.build_image_object(image_file)
scale = pdf.bounds.width / image_info.width.to_f
height = image_info.height.to_f * scale
image_opts = { at: [0, height], scale: }
[image_obj, image_info, image_opts, height]
end
def write_background_image
height = pdf.bounds.height / 2
pdf.canvas do
image_obj, image_info, image_opts, height = cover_background_image
pdf.embed_image image_obj, image_info, image_opts
end
height - styles.cover_art_padding_top
end
end
+18 -5
View File
@@ -42,7 +42,7 @@ module WorkPackage::PDFExport::Page
image_obj, image_info, scale = logo_pdf_image
top = logo_pdf_top
left = logo_pdf_left(image_info.width.to_f * scale)
pdf.repeat :all do
pdf.repeat lambda { |pg| header_footer_filter_pages.exclude?(pg) } do
pdf.embed_image image_obj, image_info, { at: [left, top], scale: }
end
end
@@ -63,11 +63,16 @@ module WorkPackage::PDFExport::Page
end
def logo_pdf_image
image_obj, image_info = logo_image
scale = [styles.page_logo_height / image_info.height.to_f, 1].min
[image_obj, image_info, scale]
end
def logo_image
image_file = custom_logo_image
image_file = Rails.root.join("app/assets/images/logo_openproject.png") if image_file.nil?
image_obj, image_info = pdf.build_image_object(image_file)
scale = [styles.page_logo_height / image_info.height.to_f, 1].min
[image_obj, image_info, scale]
[image_obj, image_info]
end
def custom_logo_image
@@ -92,8 +97,12 @@ module WorkPackage::PDFExport::Page
write_logo!
end
def header_footer_filter_pages
with_cover? ? [1] : []
end
def write_footers!
pdf.repeat :all, dynamic: true do
pdf.repeat lambda { |pg| header_footer_filter_pages.exclude?(pg) }, dynamic: true do
draw_footer_on_page
end
end
@@ -122,6 +131,10 @@ module WorkPackage::PDFExport::Page
end
def total_page_nr_text
@total_page_nr ? "/#{@total_page_nr}" : ''
if @total_page_nr
"/#{@total_page_nr - (with_cover? ? 1 : 0)}"
else
''
end
end
end
@@ -201,6 +201,70 @@ module WorkPackage::PDFExport::Style
resolve_markdown_styling(@styles.dig(:work_package, :markdown) || {})
end
def cover_logo_header_spacing
20
end
def cover_header
{ font: 'SpaceMono', size: 10, color: '064e80' }
end
def cover_header_offset
6.5
end
def cover_header_logo_height
25
end
def cover_footer
{ size: 10, color: '414d5f' }
end
def cover_footer_offset
30
end
def cover_header_border
{ color: 'd3dee3', height: 1, offset: 6 }
end
def cover_art_padding_top
120
end
def cover_art_padding_right
150
end
def cover_art_headline_max_height
30
end
def cover_art_subheading_max_height
30
end
def cover_art_headline_spacing
10
end
def cover_art_title_spacing
14
end
def cover_art_headline
{ font: 'SpaceMono', color: '414d5f', size: 10 }
end
def cover_art_title
{ color: '414d5f', styles: [:bold], size: 32, leading: -8 }
end
def cover_art_author
{ color: '414d5f', styles: [:italic], size: 10 }
end
private
def resolve_pt(value, default)
@@ -48,6 +48,7 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
include WorkPackage::PDFExport::TableOfContents
include WorkPackage::PDFExport::Page
include WorkPackage::PDFExport::Style
include WorkPackage::PDFExport::Cover
attr_accessor :pdf,
:options
@@ -71,8 +72,8 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
rescue Prawn::Errors::CannotFit
error(I18n.t(:error_pdf_export_too_many_columns))
rescue StandardError => e
Rails.logger.error { "Failed to generated PDF export: #{e} #{e.message}}." }
error(I18n.t(:error_pdf_failed_to_export, error: e.message))
Rails.logger.error { "Failed to generated PDF export: #{e}." }
error(I18n.t(:error_pdf_failed_to_export, error: e.message[0..300]))
end
private
@@ -80,7 +81,7 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
def setup_page!
self.pdf = get_pdf(current_language)
configure_page_size!(with_descriptions? ? :portrait : :landscape)
configure_page_size!(is_report? ? :portrait : :landscape)
end
def render_work_packages(work_packages, filename: "pdf_export")
@@ -99,11 +100,16 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
true
end
def with_cover?
is_report?
end
def render_work_packages_pdfs(work_packages, filename)
write_cover_page! if with_cover?
write_title!
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?
write_work_packages_sums! work_packages if with_sums_table? && with_descriptions?
write_work_packages_toc! work_packages, @id_wp_meta_map if is_report?
write_work_packages_overview! work_packages, @id_wp_meta_map unless is_report?
write_work_packages_sums! work_packages if with_sums_table? && is_report?
if should_be_batched?(work_packages)
render_batched(work_packages, filename)
else
@@ -151,7 +157,7 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
end
def render_pdf(work_packages, filename)
write_work_packages_details!(work_packages, @id_wp_meta_map) if with_descriptions?
write_work_packages_details!(work_packages, @id_wp_meta_map) if is_report?
write_after_pages!
file = Tempfile.new(filename)
pdf.render_file(file.path)
@@ -216,7 +222,7 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
end
def should_be_batched?(work_packages)
batch_supported? && with_descriptions? && with_images? && (work_packages.length > @work_packages_per_batch)
batch_supported? && is_report? && with_images? && (work_packages.length > @work_packages_per_batch)
end
def project