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): batching details pages for export with images

as-op0faaccb5ee48
diff --git a/Gemfile b/Gemfile
index 3b3dd43857b..d538946060a 100644
--- a/Gemfile
+++ b/Gemfile
@@ -151,6 +151,7 @@ gem 'structured_warnings', '~> 0.4.0'
# don't require by default, instead load on-demand when actually configured
gem 'airbrake', '~> 13.0.0', require: false
+gem "hexapdf", "~> 0.32.0"
gem 'prawn', '~> 2.2'
gem 'prawn-markup', '~> 0.3.0'
# prawn implicitly depends on matrix gem no longer in ruby core with 3.1
diff --git a/Gemfile.lock b/Gemfile.lock
index b0bf5015a97..9b276c1ed21 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -351,6 +351,7 @@ GEM
closure_tree (7.4.0)
activerecord (>= 4.2.10)
with_advisory_lock (>= 4.0.0)
+ cmdparse (3.0.7)
coderay (1.1.3)
colored2 (3.1.2)
commonmarker (0.23.8)
@@ -472,6 +473,7 @@ GEM
fuubar (2.5.1)
rspec-core (~> 3.0)
ruby-progressbar (~> 1.4)
+ geom2d (0.3.1)
globalid (1.1.0)
activesupport (>= 5.0)
gon (6.4.0)
@@ -512,6 +514,10 @@ GEM
hashdiff (1.0.1)
hashery (2.1.2)
hashie (3.6.0)
+ hexapdf (0.32.0)
+ cmdparse (~> 3.0, >= 3.0.3)
+ geom2d (~> 0.3)
+ openssl (>= 2.2.1)
html-pipeline (2.14.3)
activesupport (>= 2)
nokogiri (>= 1.4)
@@ -637,6 +643,7 @@ GEM
webfinger (>= 1.0.1)
openproject-token (2.2.0)
activemodel
+ openssl (3.1.0)
os (1.1.4)
paper_trail (12.3.0)
activerecord (>= 5.2)
@@ -1018,6 +1025,7 @@ DEPENDENCIES
grape (~> 1.7.0)
grape_logging (~> 1.8.4)
grids!
+ hexapdf (~> 0.32.0)
html-pipeline (~> 2.14.0)
htmldiff
i18n-js (~> 3.9.0)
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 76d4a788ca4..ba4ed721c5a 100644
--- a/app/models/work_package/pdf_export/work_package_detail.rb
+++ b/app/models/work_package/pdf_export/work_package_detail.rb
@@ -27,9 +27,9 @@
#++
module WorkPackage::PDFExport::WorkPackageDetail
- def write_work_packages_details!(work_packages)
- work_packages.each_with_index do |work_package, index|
- write_detail!(work_package, [index + 1])
+ def write_work_packages_details!(work_packages, id_wp_meta_map)
+ work_packages.each do |work_package|
+ write_detail!(work_package, id_wp_meta_map[work_package.id][:level_path])
end
end
@@ -60,7 +60,7 @@ module WorkPackage::PDFExport::WorkPackageDetail
with_margin(attributes_table_margins_style) do
pdf.table(
rows, column_widths: attributes_table_column_widths,
- cell_style: attributes_table_cell_style.merge({ inline_format: true })
+ cell_style: attributes_table_cell_style.merge({ inline_format: true })
)
end
end
@@ -108,6 +108,8 @@ module WorkPackage::PDFExport::WorkPackageDetail
def write_description!(work_package)
return if work_package.description.blank?
+ # TODO: move page break threshold const to style settings and implement conditional break with height measuring
+ write_optional_page_break(100)
write_description_label!
write_description_content! work_package
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 27c9dbb504b..de87c92a830 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
@@ -26,6 +26,20 @@
# See COPYRIGHT and LICENSE files for more details.
#++
+# Exporter for work package table.
+#
+# It can optionally export a work package details list with
+# - title
+# - attribute table
+# - description with optional embedded images
+#
+# When exporting with embedded images then the memory consumption can quickly
+# grow beyond limits. Therefore we create multiple smaller PDFs that we finally
+# merge do one file.
+
+require 'hexapdf'
+require 'open3'
+
class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::QueryExporter
include WorkPackage::PDFExport::Common
include WorkPackage::PDFExport::Attachments
@@ -42,8 +56,8 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
def initialize(object, options = {})
super
- @cell_padding = options.delete(:cell_padding)
- @start_page_count = 0
+ @page_count = 0
+ @work_packages_per_batch = 100
setup_page!
end
@@ -62,10 +76,10 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
def setup_page!
self.pdf = get_pdf(current_language)
- configure_page_size
+ configure_page_size!
end
- def configure_page_size
+ def configure_page_size!
pdf.options[:page_size] = 'EXECUTIVE' # TODO: 'A4'?
pdf.options[:page_layout] = with_descriptions? ? :portrait : :landscape
pdf.options[:top_margin] = page_top_margin
@@ -73,26 +87,77 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
end
def render_work_packages(work_packages, filename: "pdf_export")
- @resized_image_paths = []
-
+ @id_wp_meta_map = build_meta_infos_map(work_packages)
write_title!
write_work_packages_overview! work_packages
- write_work_packages_details! work_packages if with_descriptions?
+ if should_be_batched?(work_packages)
+ render_batched(work_packages, filename)
+ else
+ render_pdf(work_packages, filename)
+ end
+ end
- yield if block_given?
+ def render_batched(work_packages, filename)
+ @batches_count = work_packages.length.fdiv(@work_packages_per_batch).ceil
+ batch_files = []
+ (1..@batches_count).each do |batch_index|
+ batch_work_packages = work_packages.paginate(page: batch_index, per_page: @work_packages_per_batch)
+ batch_files.push render_pdf(batch_work_packages, "pdf_batch_#{batch_index}.pdf")
+ setup_page!
+ end
+ merge_batched_pdfs(batch_files, filename)
+ end
- write_logo!
- write_headers!
- write_footers!
+ def merge_batched_pdfs(batch_files, filename)
+ return batch_files[0] if batch_files.length == 1
+
+ merged_pdf = Tempfile.new(filename)
+
+ # TODO: Also possible, use the hexapdf cli that comes with the gem
+ # Open3.capture2e("hexapdf", 'merge', '--force', *batch_files.map(&:path), merged_pdf.path)
+
+ # TODO: All internal link annotions are not copied over on merging, is there a way to preserve them?
+ target = HexaPDF::Document.new
+ batch_files.each do |batch_file|
+ pdf = HexaPDF::Document.open(batch_file.path)
+ pdf.pages.each { |page| target.pages << target.import(page) }
+ end
+ target.write(merged_pdf.path, optimize: true)
+
+ merged_pdf
+ end
+ def render_pdf(work_packages, filename)
+ @resized_image_paths = []
+ write_work_packages_details!(work_packages, @id_wp_meta_map) if with_descriptions?
+ write_after_pages!
file = Tempfile.new(filename)
pdf.render_file(file.path)
-
+ @page_count += pdf.page_count
delete_all_resized_images
-
+ file.close
file
end
+ def write_after_pages!
+ write_logo!
+ write_headers!
+ write_footers!
+ end
+
+ def build_meta_infos_map(work_packages)
+ result = {}
+ # TODO: Auto-numbering and hierarchy level informations
+ work_packages.each_with_index do |work_package, index|
+ result[work_package.id] = { level_path: [index + 1], level: 0 }
+ end
+ result
+ end
+
+ def should_be_batched?(work_packages)
+ with_descriptions? && with_attachments? && (work_packages.length > @work_packages_per_batch)
+ end
+
def project
query.project
end
@@ -146,7 +211,7 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
title_string_width = pdf.width_of(title_string, page_footer_style)
pdf.repeat :all, dynamic: true do
- page_string = pdf.page_number.to_s
+ page_string = (pdf.page_number + @page_count).to_s
page_string_width = pdf.width_of(page_string, page_footer_style)
pdf.draw_text date_string, page_footer_style.merge({ at: [pdf.bounds.left, -page_footer_top] })
@@ -186,5 +251,4 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Query
def page_footer_style
{ size: 8, style: :normal }
end
-
end