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): unify styles specification; extract styles to yml; include & validate schema for yml

as-opb40dcfa9c866
diff --git a/Gemfile b/Gemfile
index 1cac1879520..73338a34c06 100644
--- a/Gemfile
+++ b/Gemfile
@@ -156,7 +156,7 @@ gem 'structured_warnings', '~> 0.4.0'
gem 'airbrake', '~> 13.0.0', require: false
gem 'prawn', '~> 2.2'
-gem 'md_to_pdf', git: 'https://github.com/opf/md-to-pdf', tag: 'v0.0.15'
+gem 'md_to_pdf', git: 'https://github.com/opf/md-to-pdf', ref: '0d6702b9d4706bba3c5ff1c50b65fdf3ba5bc95c'
# prawn implicitly depends on matrix gem no longer in ruby core with 3.1
gem 'matrix', '~> 0.4.2'
diff --git a/Gemfile.lock b/Gemfile.lock
index 4f2175acf80..d256d831fac 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,13 +1,14 @@
GIT
remote: https://github.com/opf/md-to-pdf
- revision: fc2724113cecd758a77b347042276ece8d119a03
- tag: v0.0.15
+ revision: 0d6702b9d4706bba3c5ff1c50b65fdf3ba5bc95c
+ ref: 0d6702b9d4706bba3c5ff1c50b65fdf3ba5bc95c
specs:
- md_to_pdf (0.0.15)
+ md_to_pdf (0.0.16)
commonmarker (~> 0.23)
front_matter_parser (~> 1.0)
+ json-schema (~> 4.0)
matrix (~> 0.4)
- nokogiri (~> 1.14)
+ nokogiri (~> 1.15)
prawn (~> 2.4)
prawn-table (~> 0.2)
text-hyphen (~> 1.5)
@@ -540,6 +541,8 @@ GEM
bindata
faraday (~> 2.0)
faraday-follow_redirects
+ json-schema (4.0.0)
+ addressable (>= 2.8)
json_schemer (0.2.24)
ecma-re-validator (~> 0.3)
hana (~> 1.3)
diff --git a/app/models/work_package/pdf_export/common.rb b/app/models/work_package/pdf_export/common.rb
index dc631945ed9..3ef3876eaf8 100644
--- a/app/models/work_package/pdf_export/common.rb
+++ b/app/models/work_package/pdf_export/common.rb
@@ -66,19 +66,19 @@ module WorkPackage::PDFExport::Common
def with_margin(opts, &)
with_vertical_margin(opts) do
- pdf.indent(opts[:margin_left] || 0, opts[:margin_right] || 0, &)
+ pdf.indent(opts[:left_margin] || 0, opts[:right_margin] || 0, &)
end
end
def with_vertical_margin(opts)
- pdf.move_down(opts[:margin_top]) if opts.key?(:margin_top)
+ pdf.move_down(opts[:top_margin]) if opts.key?(:top_margin)
yield
- pdf.move_down(opts[:margin_bottom]) if opts.key?(:margin_bottom)
+ pdf.move_down(opts[:bottom_margin]) if opts.key?(:bottom_margin)
end
- def write_optional_page_break(threshold)
+ def write_optional_page_break
space_from_bottom = pdf.y - pdf.bounds.bottom
- if space_from_bottom < threshold
+ if space_from_bottom < styles.page_break_threshold
pdf.start_new_page
end
end
@@ -111,15 +111,15 @@ module WorkPackage::PDFExport::Common
end
def make_link_href_cell(href, caption)
- "<color rgb='#{overview_table_link_color}'><link href='#{href}'>#{caption}</link></color>"
+ "<color rgb='#{styles.link_color}'><link href='#{href}'>#{caption}</link></color>"
end
def make_link_anchor(anchor, caption)
"<link anchor=\"#{anchor}\">#{caption}</link>"
end
- def align_to_left_position(text, align, style)
- text_width = pdf.width_of(text, style)
+ def align_to_left_position(text, align, text_style)
+ text_width = pdf.width_of(text, text_style)
if align == :right
pdf.bounds.right - text_width
elsif align == :center
@@ -134,19 +134,19 @@ module WorkPackage::PDFExport::Common
pdf.add_dest(id.to_s, pdf_dest)
end
- def draw_repeating_text(text:, align:, top:, style:)
- left = align_to_left_position(text, align, style)
- opts = style.merge({ at: [left, top] })
+ def draw_repeating_text(text:, align:, top:, text_style:)
+ left = align_to_left_position(text, align, text_style)
+ opts = text_style.merge({ at: [left, top] })
pdf.repeat :all do
pdf.draw_text text, opts
end
end
- def draw_repeating_dynamic_text(align, top, style)
+ def draw_repeating_dynamic_text(align, top, text_style)
pdf.repeat :all, dynamic: true do
text = yield
- left = align_to_left_position(text, align, style)
- opts = style.merge({ at: [left, top] })
+ left = align_to_left_position(text, align, text_style)
+ opts = text_style.merge({ at: [left, top] })
pdf.draw_text text, opts
end
end
diff --git a/app/models/work_package/pdf_export/markdown.rb b/app/models/work_package/pdf_export/markdown.rb
index 25c99347160..83149b4d33a 100644
--- a/app/models/work_package/pdf_export/markdown.rb
+++ b/app/models/work_package/pdf_export/markdown.rb
@@ -97,7 +97,7 @@ module WorkPackage::PDFExport::Markdown
end
def write_markdown!(work_package, markdown)
- md2pdf = MD2PDF.new(markdown_styling_yml)
+ md2pdf = MD2PDF.new(styles.wp_markdown_styling_yml)
md2pdf.draw_markdown(markdown, pdf, ->(src) {
with_attachments? ? attachment_image_filepath(work_package, src) : nil
})
diff --git a/app/models/work_package/pdf_export/markdown_field.rb b/app/models/work_package/pdf_export/markdown_field.rb
index 89f9ceb4128..460792897ca 100644
--- a/app/models/work_package/pdf_export/markdown_field.rb
+++ b/app/models/work_package/pdf_export/markdown_field.rb
@@ -32,8 +32,8 @@ module WorkPackage::PDFExport::MarkdownField
def write_markdown_field!(work_package, markdown, label)
return if markdown.blank?
- with_margin(wp_markdown_field_margins_style) do
- write_markdown! work_package, "# <font size=\"#{wp_markdown_field_label_size}\">#{label}</font>\n\n#{markdown}"
+ with_margin(styles.wp_markdown_margins) do
+ write_markdown! work_package, "# <font size=\"#{styles.wp_markdown_label_size}\">#{label}</font>\n\n#{markdown}"
end
end
end
diff --git a/app/models/work_package/pdf_export/overview_table.rb b/app/models/work_package/pdf_export/overview_table.rb
index f0cd16fa7ed..ca2b67b3b76 100644
--- a/app/models/work_package/pdf_export/overview_table.rb
+++ b/app/models/work_package/pdf_export/overview_table.rb
@@ -31,7 +31,7 @@ module WorkPackage::PDFExport::OverviewTable
if query.grouped?
write_grouped!(work_packages, id_wp_meta_map)
else
- with_margin(overview_table_margins_style) do
+ with_margin(styles.overview_table_margins) do
write_table!(work_packages, id_wp_meta_map, get_total_sums)
end
end
@@ -68,11 +68,11 @@ module WorkPackage::PDFExport::OverviewTable
end
def write_group!(group, work_packages, id_wp_meta_map, sums)
- write_optional_page_break(page_break_space_left_threshold)
- with_margin(overview_table_margins_style) do
+ write_optional_page_break
+ with_margin(styles.overview_table_margins) do
label = make_group_label(group)
- with_margin(overview_group_header_margins_style) do
- pdf.formatted_text([overview_group_header_style.merge({ text: label })])
+ with_margin(styles.overview_group_header_margins) do
+ pdf.formatted_text([styles.overview_group_header.merge({ text: label })])
end
write_table!(work_packages, id_wp_meta_map, sums)
end
@@ -86,21 +86,21 @@ module WorkPackage::PDFExport::OverviewTable
end
def table_options
- { header: true, cell_style: overview_table_cell_style.merge({ inline_format: true }) }
+ { header: true, cell_style: styles.overview_table_cell.merge({ inline_format: true }) }
end
def format_sum_cells(sums_cells)
- sums_style = overview_table_sums_cell_style
+ sums_style = styles.overview_table_sums_cell
sums_cells.each do |cell|
apply_cell_style cell, sums_style
end
end
- def apply_cell_style(cell, style)
- cell.background_color = style[:background_color]
- cell.font_style = style[:font_style]
- cell.text_color = style[:text_color]
- cell.size = style[:size]
+ def apply_cell_style(cell, cell_style)
+ cell.background_color = cell_style[:background_color]
+ cell.font_style = cell_style[:font]
+ cell.text_color = cell_style[:color]
+ cell.size = cell_style[:size]
end
def get_total_sums
@@ -133,7 +133,7 @@ module WorkPackage::PDFExport::OverviewTable
end
def build_column_cell(content)
- pdf.make_cell(content, overview_table_cell_padding_style)
+ pdf.make_cell(content, styles.overview_table_cell)
end
def build_nr_cell(work_package, id_wp_meta_map)
@@ -143,17 +143,17 @@ module WorkPackage::PDFExport::OverviewTable
end
def build_subject_cell(content, work_package, id_wp_meta_map)
- opts = overview_table_cell_padding_style
- padding_left = opts[:padding_left]
+ opts = styles.overview_table_cell
+ padding_left = opts[:padding_left] || 0
if query.show_hierarchies
level = id_wp_meta_map[work_package.id][:level_path].length
- padding_left = (overview_table_subject_indent_style * level) if level > 1
+ padding_left = (styles.overview_table_subject_indent * level) if level > 1
end
pdf.make_cell(content, opts.merge({ padding_left: }))
end
def build_header_row
- header_style = overview_table_header_cell_style
+ header_style = styles.overview_table_header_cell
row = table_columns_objects.map do |col|
content = (col.caption || '').upcase
build_header_cell(content, header_style)
diff --git a/app/models/work_package/pdf_export/page.rb b/app/models/work_package/pdf_export/page.rb
index 94f064abd60..9d0909463c3 100644
--- a/app/models/work_package/pdf_export/page.rb
+++ b/app/models/work_package/pdf_export/page.rb
@@ -28,10 +28,12 @@
module WorkPackage::PDFExport::Page
def configure_page_size!(layout)
- pdf.options[:page_size] = page_size
pdf.options[:page_layout] = layout
- pdf.options[:top_margin] = page_top_margin
- pdf.options[:bottom_margin] = page_bottom_margin
+ pdf.options[:page_size] = styles.page_size
+ pdf.options[:top_margin] = styles.page_margin_top
+ pdf.options[:left_margin] = styles.page_margin_left
+ pdf.options[:bottom_margin] = styles.page_margin_bottom
+ pdf.options[:right_margin] = styles.page_margin_right
end
def write_logo!
@@ -44,8 +46,8 @@ module WorkPackage::PDFExport::Page
end
def logo_pdf_left(logo_width)
- case page_logo_align
- when :middle
+ case styles.page_logo_align.to_sym
+ when :center
(pdf.bounds.right - pdf.bounds.left - logo_width) / 2
when :right
pdf.bounds.right - logo_width
@@ -55,14 +57,14 @@ module WorkPackage::PDFExport::Page
end
def logo_pdf_top
- pdf.bounds.top + page_header_top + (page_logo_height / 2)
+ pdf.bounds.top + styles.page_header_offset + (styles.page_logo_height / 2)
end
def logo_pdf_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 = [page_logo_height / image_info.height.to_f, 1].min
+ scale = [styles.page_logo_height / image_info.height.to_f, 1].min
[image_obj, image_info, scale]
end
@@ -79,8 +81,8 @@ module WorkPackage::PDFExport::Page
def write_title!
pdf.title = heading
- with_margin(page_heading_margins_style) do
- pdf.formatted_text([page_heading_style.merge({ text: heading })])
+ with_margin(styles.page_heading_margins) do
+ pdf.formatted_text([styles.page_heading.merge({ text: heading })])
end
end
@@ -95,7 +97,7 @@ module WorkPackage::PDFExport::Page
end
def write_footer_page_nr!
- draw_repeating_dynamic_text(:center, -page_footer_top, page_footer_style) do
+ draw_repeating_dynamic_text(:center, styles.page_footer_offset, styles.page_footer) do
current_page_nr.to_s + total_page_nr_text
end
end
@@ -105,15 +107,20 @@ module WorkPackage::PDFExport::Page
end
def write_footer_title!
- draw_repeating_text(text: heading, align: :right, top: -page_footer_top, style: page_footer_style)
+ draw_repeating_text(
+ text: heading,
+ align: :right,
+ top: styles.page_footer_offset,
+ text_style: styles.page_footer
+ )
end
def write_footer_date!
draw_repeating_text(
text: format_date(Time.zone.today),
align: :left,
- top: -page_footer_top,
- style: page_footer_style
+ top: styles.page_footer_offset,
+ text_style: styles.page_footer
)
end
end
diff --git a/app/models/work_package/pdf_export/schema.json b/app/models/work_package/pdf_export/schema.json
new file mode 100644
index 00000000000..e8f3beaa30e
--- /dev/null
+++ b/app/models/work_package/pdf_export/schema.json
@@ -0,0 +1,940 @@
+{
+ "type" : "object",
+ "properties" : {
+ "page" : {
+ "type" : "object",
+ "properties" : {
+ "link_color" : {
+ "$ref" : "#/$defs/color"
+ },
+ "page_break_threshold" : {
+ "$ref" : "#/$defs/measurement"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/page"
+ }
+ ]
+ },
+ "page_logo" : {
+ "$ref" : "#/$defs/page_logo"
+ },
+ "page_header" : {
+ "$ref" : "#/$defs/page_header_footer"
+ },
+ "page_footer" : {
+ "$ref" : "#/$defs/page_header_footer"
+ },
+ "page_heading" : {
+ "$ref" : "#/$defs/font_and_margin"
+ },
+ "work_package" : {
+ "type" : "object",
+ "properties" : {
+ "subject" : {
+ "$ref" : "#/$defs/font_and_margin"
+ },
+ "label" : {
+ "$ref" : "#/$defs/font_and_margin"
+ },
+ "attributes_table" : {
+ "type" : "object",
+ "properties" : {
+ "cell" : {
+ "$ref" : "#/$defs/table_cell"
+ },
+ "cell_label" : {
+ "$ref" : "#/$defs/table_cell"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "section" : {
+ "type" : "object",
+ "properties" : {
+ "heading" : {
+ "$ref" : "#/$defs/font_and_margin"
+ },
+ "item" : {
+ "$ref" : "#/$defs/font_and_margin"
+ }
+ }
+ },
+ "markdown_label" : {
+ "$ref" : "#/$defs/font"
+ },
+ "markdown_margin" : {
+ "$ref" : "#/$defs/margin"
+ },
+ "markdown" : {
+ "$ref" : "#/$defs/md_to_pdf_markdown"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "toc" : {
+ "type" : "object",
+ "properties" : {
+ "subject_indent" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "item" : {
+ "$ref" : "#/$defs/font_and_margin"
+ }
+ },
+ "patternProperties" : {
+ "^item_level_\\d+" : {
+ "$ref" : "#/$defs/font_and_margin"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "overview" : {
+ "type" : "object",
+ "properties" : {
+ "group_heading" : {
+ "$ref" : "#/$defs/font_and_margin"
+ },
+ "table" : {
+ "type" : "object",
+ "properties" : {
+ "subject_indent" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "cell" : {
+ "$ref" : "#/$defs/table_cell"
+ },
+ "cell_header" : {
+ "$ref" : "#/$defs/table_cell"
+ },
+ "cell_sums" : {
+ "$ref" : "#/$defs/table_cell"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ }
+ }
+ }
+ },
+ "patternProperties" : {
+ "^page_header_\\d+" : {
+ "$ref" : "#/$defs/page_header_footer"
+ },
+ "^page_footer_\\d+" : {
+ "$ref" : "#/$defs/page_header_footer"
+ },
+ "^ordered_list_point_\\d+" : {
+ "$ref" : "#/$defs/ordered_list_point"
+ },
+ "^ordered_list_\\d+" : {
+ "$ref" : "#/$defs/ordered_list"
+ },
+ "^unordered_list_point_\\d+" : {
+ "$ref" : "#/$defs/unordered_list_point"
+ },
+ "^unordered_list_\\d+" : {
+ "$ref" : "#/$defs/unordered_list"
+ },
+ "^header_\\d+" : {
+ "$ref" : "#/$defs/header"
+ }
+ },
+ "required" : [],
+ "additionalProperties" : false,
+ "$defs" : {
+ "md_to_pdf_markdown" : {
+ "type" : "object",
+ "properties" : {
+ "font" : {
+ "$ref" : "#/$defs/font"
+ },
+ "paragraph" : {
+ "$ref" : "#/$defs/paragraph"
+ },
+ "table" : {
+ "$ref" : "#/$defs/table"
+ },
+ "headless_table" : {
+ "$ref" : "#/$defs/headless_table"
+ },
+ "code" : {
+ "$ref" : "#/$defs/code"
+ },
+ "codeblock" : {
+ "$ref" : "#/$defs/codeblock"
+ },
+ "link" : {
+ "$ref" : "#/$defs/link"
+ },
+ "image" : {
+ "$ref" : "#/$defs/image"
+ },
+ "image_classes" : {
+ "$ref" : "#/$defs/image_classes"
+ },
+ "hrule" : {
+ "$ref" : "#/$defs/hrule"
+ },
+ "footnote_reference" : {
+ "$ref" : "#/$defs/footnote_reference"
+ },
+ "footnote_definition" : {
+ "$ref" : "#/$defs/footnote_definition"
+ },
+ "header" : {
+ "$ref" : "#/$defs/header"
+ },
+ "blockquote" : {
+ "$ref" : "#/$defs/blockquote"
+ },
+ "unordered_list" : {
+ "$ref" : "#/$defs/unordered_list"
+ },
+ "unordered_list_point" : {
+ "$ref" : "#/$defs/unordered_list_point"
+ },
+ "task_list" : {
+ "$ref" : "#/$defs/unordered_list"
+ },
+ "task_list_point" : {
+ "$ref" : "#/$defs/task_list_point"
+ },
+ "ordered_list" : {
+ "$ref" : "#/$defs/ordered_list"
+ },
+ "ordered_list_point" : {
+ "$ref" : "#/$defs/ordered_list_point"
+ }
+ },
+ "patternProperties" : {
+ "^ordered_list_point_\\d+" : {
+ "$ref" : "#/$defs/ordered_list_point"
+ },
+ "^ordered_list_\\d+" : {
+ "$ref" : "#/$defs/ordered_list"
+ },
+ "^unordered_list_point_\\d+" : {
+ "$ref" : "#/$defs/unordered_list_point"
+ },
+ "^unordered_list_\\d+" : {
+ "$ref" : "#/$defs/unordered_list"
+ },
+ "^header_\\d+" : {
+ "$ref" : "#/$defs/header"
+ }
+ }
+ },
+ "font_and_margin" : {
+ "type" : "object",
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "font_specs" : {
+ "type" : "array",
+ "items" : {
+ "$ref" : "#/$defs/font_spec"
+ }
+ },
+ "font_spec" : {
+ "type" : "object",
+ "properties" : {
+ "name" : {
+ "type" : "string"
+ },
+ "pathname" : {
+ "type" : "string"
+ },
+ "regular" : {
+ "type" : "string"
+ },
+ "bold" : {
+ "type" : "string"
+ },
+ "italic" : {
+ "type" : "string"
+ },
+ "bold_italic" : {
+ "type" : "string"
+ }
+ },
+ "required" : [
+ "name",
+ "pathname",
+ "regular",
+ "bold",
+ "italic",
+ "bold_italic"
+ ]
+ },
+ "fallback_font_spec" : {
+ "type" : "object",
+ "properties" : {
+ "name" : {
+ "type" : "string"
+ },
+ "pathname" : {
+ "type" : "string"
+ },
+ "filename" : {
+ "type" : "string"
+ }
+ },
+ "required" : [
+ "name",
+ "pathname",
+ "filename"
+ ]
+ },
+ "color" : {
+ "type" : "string",
+ "pattern" : "^(?:[0-9a-fA-F]{3}){1,2}$"
+ },
+ "font" : {
+ "type" : "object",
+ "properties" : {
+ "font" : {
+ "type" : "string"
+ },
+ "size" : {
+ "type" : "number"
+ },
+ "character_spacing" : {
+ "type" : "number"
+ },
+ "leading" : {
+ "type" : "number"
+ },
+ "color" : {
+ "$ref" : "#/$defs/color"
+ },
+ "styles" : {
+ "type" : "array",
+ "items" : {
+ "$ref" : "#/$defs/font_style"
+ }
+ }
+ }
+ },
+ "font_style" : {
+ "type" : "string",
+ "enum" : [
+ "bold",
+ "italic",
+ "underline",
+ "strikethrough",
+ "superscript",
+ "subscript"
+ ]
+ },
+ "page" : {
+ "type" : "object",
+ "properties" : {
+ "page_layout" : {
+ "type" : "string",
+ "enum" : [
+ "portrait",
+ "landscape"
+ ]
+ },
+ "page_size" : {
+ "type" : "string",
+ "enum" : [
+ "EXECUTIVE",
+ "TABLOID",
+ "LETTER",
+ "LEGAL",
+ "FOLIO",
+ "A0",
+ "A1",
+ "A2",
+ "A3",
+ "A4",
+ "A5",
+ "A6",
+ "A7",
+ "A8",
+ "A9",
+ "A10",
+ "B0",
+ "B1",
+ "B2",
+ "B3",
+ "B4",
+ "B5",
+ "B6",
+ "B7",
+ "B8",
+ "B9",
+ "B10",
+ "C0",
+ "C1",
+ "C2",
+ "C3",
+ "C4",
+ "C5",
+ "C6",
+ "C7",
+ "C8",
+ "C9",
+ "C10",
+ "RA0",
+ "RA1",
+ "RA2",
+ "RA3",
+ "RA4",
+ "SRA0",
+ "SRA1",
+ "SRA2",
+ "SRA3",
+ "SRA4",
+ "4A0",
+ "2A0"
+ ]
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "page_header_footer" : {
+ "type" : "object",
+ "properties" : {
+ "filter_pages" : {
+ "type" : "array",
+ "items" : {
+ "type" : "number"
+ }
+ },
+ "align" : {
+ "$ref" : "#/$defs/alignment"
+ },
+ "offset" : {
+ "type" : "number"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "page_logo" : {
+ "type" : "object",
+ "properties" : {
+ "filter_pages" : {
+ "type" : "array",
+ "items" : {
+ "type" : "number"
+ }
+ },
+ "align" : {
+ "$ref" : "#/$defs/alignment"
+ },
+ "height" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "max_width" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "offset" : {
+ "type" : "number"
+ },
+ "disabled" : {
+ "type" : "boolean"
+ }
+ }
+ },
+ "hrule" : {
+ "type" : "object",
+ "properties" : {
+ "line_width" : {
+ "$ref" : "#/$defs/measurement"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "margin" : {
+ "type" : "object",
+ "properties" : {
+ "margin" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "margin_left" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "margin_right" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "margin_top" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "margin_bottom" : {
+ "$ref" : "#/$defs/measurement"
+ }
+ }
+ },
+ "padding" : {
+ "type" : "object",
+ "properties" : {
+ "padding" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "padding_left" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "padding_right" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "padding_top" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "padding_bottom" : {
+ "$ref" : "#/$defs/measurement"
+ }
+ }
+ },
+ "measurement" : {
+ "type" : [
+ "number",
+ "string"
+ ],
+ "pattern" : "^([0-9\\.]+)(mm|cm|dm|m|in|ft|yr|pt)$"
+ },
+ "alignment" : {
+ "type" : "string",
+ "enum" : [
+ "left",
+ "center",
+ "right"
+ ]
+ },
+ "unordered_list" : {
+ "type" : "object",
+ "properties" : {
+ "spacing" : {
+ "$ref" : "#/$defs/measurement"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/padding"
+ }
+ ]
+ },
+ "unordered_list_point" : {
+ "type" : "object",
+ "properties" : {
+ "sign" : {
+ "type" : "string"
+ },
+ "spacing" : {
+ "$ref" : "#/$defs/measurement"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "task_list_point" : {
+ "type" : "object",
+ "properties" : {
+ "checked" : {
+ "type" : "string"
+ },
+ "unchecked" : {
+ "type" : "string"
+ },
+ "spacing" : {
+ "$ref" : "#/$defs/measurement"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "ordered_list" : {
+ "type" : "object",
+ "properties" : {
+ "spacing" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "point_inline" : {
+ "type" : "boolean"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/padding"
+ }
+ ]
+ },
+ "ordered_list_point" : {
+ "type" : "object",
+ "properties" : {
+ "spacing" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "alphabetical" : {
+ "type" : "boolean"
+ },
+ "spanning" : {
+ "type" : "boolean"
+ },
+ "template" : {
+ "type" : "string"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "footnote_reference" : {
+ "type" : "object",
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "footnote_definition" : {
+ "type" : "object",
+ "properties" : {
+ "point" : {
+ "type" : "object",
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ }
+ }
+ },
+ "link" : {
+ "type" : "object",
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "code" : {
+ "type" : "object",
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "header" : {
+ "type" : "object",
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/padding"
+ }
+ ]
+ },
+ "blockquote" : {
+ "type" : "object",
+ "properties" : {
+ "background_color" : {
+ "$ref" : "#/$defs/color"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/border"
+ },
+ {
+ "$ref" : "#/$defs/padding"
+ },
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "codeblock" : {
+ "type" : "object",
+ "properties" : {
+ "background_color" : {
+ "$ref" : "#/$defs/color"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/padding"
+ },
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "table" : {
+ "type" : "object",
+ "properties" : {
+ "auto_width" : {
+ "type" : "boolean"
+ },
+ "header" : {
+ "$ref" : "#/$defs/table_header"
+ },
+ "cell" : {
+ "$ref" : "#/$defs/table_cell"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ },
+ {
+ "$ref" : "#/$defs/border"
+ }
+ ]
+ },
+ "headless_table" : {
+ "type" : "object",
+ "properties" : {
+ "auto_width" : {
+ "type" : "boolean"
+ },
+ "cell" : {
+ "$ref" : "#/$defs/table_cell"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ },
+ {
+ "$ref" : "#/$defs/border"
+ }
+ ]
+ },
+ "table_cell" : {
+ "type" : "object",
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/padding"
+ },
+ {
+ "$ref" : "#/$defs/border"
+ }
+ ]
+ },
+ "table_header" : {
+ "type" : "object",
+ "properties" : {
+ "background_color" : {
+ "$ref" : "#/$defs/color"
+ },
+ "no_repeating" : {
+ "type" : "boolean"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ }
+ ]
+ },
+ "paragraph" : {
+ "type" : "object",
+ "properties" : {
+ "align" : {
+ "type" : "string",
+ "enum" : [
+ "left",
+ "center",
+ "right",
+ "justify"
+ ]
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/font"
+ },
+ {
+ "$ref" : "#/$defs/padding"
+ }
+ ]
+ },
+ "image" : {
+ "type" : "object",
+ "properties" : {
+ "max_width" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "align" : {
+ "$ref" : "#/$defs/alignment"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "border" : {
+ "type" : "object",
+ "properties" : {
+ "border_width" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "border_width_left" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "border_width_top" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "border_width_right" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "border_width_bottom" : {
+ "$ref" : "#/$defs/measurement"
+ },
+ "border_color" : {
+ "$ref" : "#/$defs/color"
+ },
+ "border_color_left" : {
+ "$ref" : "#/$defs/color"
+ },
+ "border_color_top" : {
+ "$ref" : "#/$defs/color"
+ },
+ "border_color_right" : {
+ "$ref" : "#/$defs/color"
+ },
+ "border_color_bottom" : {
+ "$ref" : "#/$defs/color"
+ },
+ "no_border" : {
+ "type" : "boolean"
+ },
+ "no_border_left" : {
+ "type" : "boolean"
+ },
+ "no_border_top" : {
+ "type" : "boolean"
+ },
+ "no_border_right" : {
+ "type" : "boolean"
+ },
+ "no_border_bottom" : {
+ "type" : "boolean"
+ }
+ },
+ "allOf" : [
+ {
+ "$ref" : "#/$defs/margin"
+ }
+ ]
+ },
+ "image_classes" : {
+ "type" : "object",
+ "patternProperties" : {
+ ".*" : {
+ "$ref" : "#/$defs/image"
+ }
+ }
+ },
+ "fields_default" : {
+ "type" : "object",
+ "properties" : {
+ "pdf_language" : {
+ "type" : "string"
+ },
+ "pdf_header" : {
+ "type" : "string"
+ },
+ "pdf_header_2" : {
+ "type" : "string"
+ },
+ "pdf_header_3" : {
+ "type" : "string"
+ },
+ "pdf_footer" : {
+ "type" : "string"
+ },
+ "pdf_footer_2" : {
+ "type" : "string"
+ },
+ "pdf_footer_3" : {
+ "type" : "string"
+ },
+ "pdf_header_logo" : {
+ "type" : "string"
+ },
+ "pdf_hyphenation" : {
+ "type" : "boolean"
+ },
+ "pdf_fields" : {
+ "type" : "object",
+ "patternProperties" : {
+ ".*" : {
+ "type" : [
+ "string",
+ "number"
+ ]
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/app/models/work_package/pdf_export/standard.yml b/app/models/work_package/pdf_export/standard.yml
new file mode 100644
index 00000000000..77c745832cf
--- /dev/null
+++ b/app/models/work_package/pdf_export/standard.yml
@@ -0,0 +1,176 @@
+page:
+ page_size: "EXECUTIVE"
+ margin_top: 60
+ margin_bottom: 60
+ margin_left: 36
+ margin_right: 36
+ page_break_threshold: 200
+ link_color: "175A8E"
+
+page_header:
+ offset: 20
+ size: 8
+
+page_footer:
+ offset: -30
+ size: 8
+
+page_logo:
+ height: 20
+ align: "right"
+
+page_heading:
+ size: 14
+ styles: [ "bold" ]
+ margin_bottom: 10
+
+overview:
+ group_heading:
+ size: 11
+ styles: [ "bold" ]
+ margin_bottom: 10
+ table:
+ subject_indent: 8
+ margin_bottom: 20
+ cell:
+ size: 9
+ color: "000000"
+ padding_left: 5
+ padding_right: 5
+ padding_top: 0
+ padding_bottom: 5
+ border_width: 0.25
+ cell_header:
+ size: 9
+ styles: [ "bold" ]
+ cell_sums:
+ size: 8
+ styles: [ "bold" ]
+
+toc:
+ subject_indent: 4
+ margin_top: 10
+ margin_bottom: 20
+ item:
+ size: 9
+ color: "000000"
+ margin_bottom: 4
+ item_level_1:
+ size: 10
+ styles: [ "bold" ]
+ margin_top: 4
+ margin_bottom: 4
+ item_level_2:
+ size: 10
+
+work_package:
+ margin_bottom: 20
+ subject:
+ size: 14
+ styles: [ "bold" ]
+ margin_bottom: 10
+ label:
+ size: 11
+ styles: [ "bold" ]
+ attributes_table:
+ margin_bottom: 10
+ cell:
+ size: 9
+ color: "000000"
+ padding_left: 5
+ padding_right: 5
+ padding_top: 0
+ padding_bottom: 5
+ border_color: "4B4B4B"
+ border_width: 0.25
+ cell_label:
+ styles: [ "bold" ]
+ section:
+ heading:
+ size: 9
+ styles: [ "bold" ]
+ margin_top: 4
+ margin_bottom: 4
+ item:
+ size: 9
+ styles: [ "italic" ]
+ margin_left: 10
+ markdown_label:
+ size: 12
+ markdown_margin:
+ margin_top: 12
+ margin_bottom: 8
+ markdown:
+ font:
+ size: 10
+ leading: 3
+ header:
+ size: 8
+ styles: [ "bold" ]
+ header_1:
+ size: 10
+ padding_bottom: 4
+ header_2:
+ size: 10
+ header_3:
+ size: 9
+ paragraph:
+ align: "left"
+ unordered_list:
+ spacing: 1
+ unordered_list_point:
+ spacing: 4
+ ordered_list:
+ spacing: 1
+ ordered_list_point:
+ spacing: 4
+ task_list:
+ spacing: 1
+ task_list_point:
+ spacing: 4
+ checked: "☑"
+ unchecked: "☐"
+ link:
+ color: "175A8E"
+ styles: [ ]
+ code:
+ color: "880000"
+ size: 9
+ font: "SpaceMono"
+ blockquote:
+ background_color: "f4f9ff"
+ size: 10
+ styles: [ "italic" ]
+ color: "0f3b66"
+ border_color: "b8d6f4"
+ border_width: 1
+ padding: 4
+ padding_left: 6
+ margin_top: 4
+ margin_bottom: 4
+ no_border_left: false
+ no_border_right: true
+ no_border_bottom: true
+ no_border_top: true
+ image:
+ align: "left"
+ codeblock:
+ background_color: "F5F5F5"
+ color: "880000"
+ padding: "3mm"
+ size: 8
+ margin_top: "2mm"
+ margin_bottom: "2mm"
+ font: "SpaceMono"
+ table:
+ auto_width: true
+ margin_top: 4
+ margin_bottom: 4
+ header:
+ size: 9
+ styles: [ "bold" ]
+ background_color: "F0F0F0"
+ cell:
+ size: 9
+ border_width: "0.25mm"
+ padding: 5
diff --git a/app/models/work_package/pdf_export/style.rb b/app/models/work_package/pdf_export/style.rb
index 6df9c76551e..eb4d1b788a1 100644
--- a/app/models/work_package/pdf_export/style.rb
+++ b/app/models/work_package/pdf_export/style.rb
@@ -27,282 +27,232 @@
#++
module WorkPackage::PDFExport::Style
- def page_size
- 'EXECUTIVE'
- end
+ include MarkdownToPDF::StyleValidation
- def page_header_top
- 20
- end
+ class PDFStyles
+ include MarkdownToPDF::Common
+ include MarkdownToPDF::StyleHelper
- def page_bottom_margin
- 60
- end
+ def initialize(yml)
+ @styles = yml.deep_symbolize_keys
+ end
- def page_footer_top
- 30
- end
+ def page_size
+ @styles[:page_size] || 'EXECUTIVE'
+ end
- def page_logo_height
- 20
- end
+ def page_header_offset
+ @styles.dig(:page_header, :offset) || 20
+ end
- def page_logo_align
- :right
- end
+ def page_footer_offset
+ @styles.dig(:page_footer, :offset) || -30
+ end
- def page_top_margin
- 60
- end
+ def page_logo_height
+ @styles.dig(:page_logo, :height) || 20
+ end
- def page_heading_style
- { size: 14, styles: [:bold] }
- end
+ def page_logo_align
+ @styles.dig(:page_logo, :align) || :right
+ end
- def page_heading_margins_style
- { margin_bottom: 10 }
- end
+ def page_margin_top
+ @styles.dig(:page, :margin_top) || 60
+ end
- def page_header_style
- { size: 8, style: :normal }
- end
+ def page_margin_left
+ @styles.dig(:page, :margin_left) || 50
+ end
- def page_footer_style
- { size: 8, style: :normal }
- end
+ def page_margin_right
+ @styles.dig(:page, :margin_right) || 50
+ end
- def page_break_space_left_threshold
- 200
- end
+ def page_margin_bottom
+ @styles.dig(:page, :margin_bottom) || 60
+ end
- def overview_table_link_color
- '175A8E'
- end
+ def page_heading
+ resolve_font(@styles[:page_heading])
+ end
- def overview_group_header_style
- { size: 11, styles: [:bold] }
- end
+ def page_heading_margins
+ resolve_margin(@styles[:page_heading])
+ end
- def overview_group_header_margins_style
- { margin_bottom: 10 }
- end
+ def page_header
+ resolve_font(@styles[:page_header])
+ end
- def overview_table_margins_style
- { margin_bottom: 20, margin_left: 0, margin_right: 0 }
- end
+ def page_footer
+ resolve_font(@styles[:page_footer])
+ end
- def overview_table_header_cell_style
- { size: 9, text_color: "000000", font_style: :bold,
- padding_left: 5, padding_right: 5, padding_top: 0, padding_bottom: 5 }
- end
+ def page_break_threshold
+ @styles.dig(:page, :page_break_threshold) || 200
+ end
- def overview_table_sums_cell_style
- { size: 8, text_color: "000000", font_style: :bold }
- end
+ def link_color
+ @styles.dig(:page, :link_color) || '000000'
+ end
- def overview_table_subject_indent_style
- 8
- end
+ def overview_group_header
+ resolve_font(@styles.dig(:overview, :group_heading))
+ end
- def overview_table_cell_padding_style
- { padding_left: 5,
- padding_right: 5,
- padding_top: 0,
- padding_bottom: 5 }
- end
+ def overview_group_header_margins
+ resolve_margin(@styles.dig(:overview, :group_heading))
+ end
- def overview_table_cell_style
- { size: 9,
- text_color: "000000",
- border_widths: [0.25, 0.25, 0.25, 0.25] }
- end
+ def overview_table_margins
+ resolve_margin(@styles.dig(:overview, :table))
+ end
- def toc_margins_style
- { margin_top: 10, margin_bottom: 20 }
- end
+ def overview_table_cell
+ resolve_table_cell(@styles.dig(:overview, :table, :cell))
+ end
- def toc_item_style(level)
- if level <= 1
- { size: 10, style: :bold }
- elsif level <= 2
- { size: 10 }
- else
- { size: 9 }
+ def overview_table_header_cell
+ overview_table_cell.merge(
+ resolve_table_cell(@styles.dig(:overview, :table, :cell_header))
+ )
end
- end
- def toc_item_subject_indent_style
- 4
- end
+ def overview_table_sums_cell
+ overview_table_cell.merge(
+ resolve_table_cell(@styles.dig(:overview, :table, :cell_sums))
+ )
+ end
- def toc_item_margins_style(level)
- if level === 1
- { margin_top: 4, margin_bottom: 4 }
- else
- { margin_bottom: 4 }
+ def overview_table_subject_indent
+ @styles.dig(:overview, :table, :subject_indent) || 8
end
- end
- def wp_headline_margins_style
- { margin_top: 4, margin_bottom: 4 }
- end
+ def toc_margins
+ resolve_margin(@styles[:toc])
+ end
- def wp_headline_style
- { size: 9, styles: [:bold] }
- end
+ def toc_item(level)
+ resolve_font(@styles.dig(:toc, :item)).merge(
+ resolve_font(@styles.dig(:toc, "item_level_#{level}".to_sym))
+ )
+ end
- def wp_item_margins_style
- { margin_left: 10 }
- end
+ def toc_item_subject_indent
+ @styles.dig(:toc, :subject_indent) || 4
+ end
- def wp_item_style
- { size: 9, styles: [:italic] }
- end
+ def toc_item_margins(level)
+ resolve_margin(@styles.dig(:toc, :item)).merge(
+ resolve_margin(@styles.dig(:toc, "item_level_#{level}".to_sym))
+ )
+ end
- def wp_label_margins_style
- { margin_top: 12, margin_bottom: 8 }
- end
+ def wp_margins
+ resolve_margin(@styles[:work_package])
+ end
- def wp_label_style
- { size: 11, styles: [:bold] }
- end
+ def wp_section_heading
+ resolve_font(@styles.dig(:work_package, :section, :heading))
+ end
- def wp_detail_margins_style
- { margin_bottom: 20 }
- end
+ def wp_section_heading_margins
+ resolve_margin(@styles.dig(:work_package, :section, :heading))
+ end
- def wp_detail_subject_margins_style
- { margin_bottom: 4 }
- end
+ def wp_section_item
+ resolve_font(@styles.dig(:work_package, :section, :item))
+ end
- def wp_detail_subject_font_style
- { size: 14, styles: [:bold] }
- end
+ def wp_section_item_margins
+ resolve_margin(@styles.dig(:work_package, :section, :item))
+ end
- def wp_attributes_table_margins_style
- { margin_top: 4, margin_bottom: 2 }
- end
+ def wp_label
+ resolve_font(@styles.dig(:work_package, :label))
+ end
- def wp_attributes_table_label_font_style
- { font_style: :bold }
- end
+ def wp_label_margins
+ resolve_margin(@styles.dig(:work_package, :label))
+ end
+
+ def wp_subject
+ resolve_font(@styles.dig(:work_package, :subject))
+ end
+
+ def wp_detail_subject_margins
+ resolve_margin(@styles.dig(:work_package, :subject))
+ end
+
+ def wp_attributes_table_margins
+ resolve_margin(@styles.dig(:work_package, :attributes_table))
+ end
+
+ def wp_attributes_table_cell
+ resolve_table_cell(@styles.dig(:work_package, :attributes_table, :cell))
+ end
+
+ def wp_attributes_table_label_cell
+ wp_attributes_table_cell.merge(
+ resolve_table_cell(@styles.dig(:work_package, :attributes_table, :cell_label))
+ )
+ end
- def wp_attributes_table_cell_style
- { size: 9,
- text_color: "000000",
- border_color: '4B4B4B',
- border_widths: [0.25, 0.25, 0.25, 0.25],
- padding_left: 5,
- padding_right: 5,
- padding_top: 0,
- padding_bottom: 4 }
+ def wp_markdown_label_size
+ resolve_font(@styles.dig(:work_package, :markdown_label))[:size] || 12
+ end
+
+ def wp_markdown_margins
+ resolve_margin(@styles.dig(:work_package, :markdown))
+ end
+
+ def wp_markdown_styling_yml
+ resolve_markdown_styling(@styles.dig(:work_package, :markdown) || {})
+ end
+
+ private
+
+ def resolve_table_cell(style)
+ # prawn.table.make_cell does use differently named options
+ # so to have them specified consistently, we map here
+ opts = opts_table_cell(style || {})
+ font_styles = opts.delete(:styles) || []
+ opts[:font_style] = font_styles[0] unless font_styles.empty?
+ color = opts.delete(:color)
+ opts[:text_color] = color unless color.nil?
+ opts
+ end
+
+ def resolve_markdown_styling(style)
+ page = style.delete(:font)
+ style[:page] = page unless page.nil?
+ style
+ end
+
+ def resolve_font(style)
+ opts_font(style || {})
+ end
+
+ def resolve_margin(style)
+ opts_margin(style || {})
+ end
end
- def wp_markdown_field_label_size
- 12
+ def styles
+ @style ||= PDFStyles.new(load_style)
end
- def wp_markdown_field_margins_style
- { margin_top: 12, margin_bottom: 8 }
+ private
+
+ def load_style
+ yml = YAML::load_file(File.join(styles_asset_path, 'standard.yml'))
+ schema = JSON::load_file(File.join(styles_asset_path, 'schema.json'))
+ validate_schema!(yml, schema)
end
- def markdown_styling_yml
- # rubocop:disable Naming/VariableNumber
- {
- header: {
- size: 8,
- styles: [:bold]
- },
- header_1: {
- size: 10,
- padding_bottom: 4
- },
- header_2: {
- size: 10
- },
- header_3: {
- size: 9
- },
- page: {
- size: 10,
- leading: 3
- },
- paragraph: {
- align: 'left'
- },
- unordered_list: {
- spacing: 1
- },
- unordered_list_point: {
- spacing: 4
- },
- ordered_list: {
- spacing: 1
- },
- ordered_list_point: {
- spacing: 4
- },
- task_list: {
- spacing: 1
- },
- task_list_point: {
- spacing: 4,
- checked: '☑', # fallback font is needed
- unchecked: '☐' # fallback font is needed
- },
- link: {
- color: '175A8E',
- styles: []
- },
- code: {
- color: '880000',
- size: 9,
- font: 'SpaceMono'
- },
- blockquote: {
- background_color: 'f4f9ff',
- size: 10,
- styles: ['italic'],
- color: '0f3b66',
- border_color: 'b8d6f4',
- border_width: 1,
- padding: 4,
- padding_left: 6,
- margin_top: 4,
- margin_bottom: 4,
- no_border_left: false,
- no_border_right: true,
- no_border_bottom: true,
- no_border_top: true
- },
- image: {
- align: 'left'
- },
- codeblock: {
- background_color: 'F5F5F5',
- color: '880000',
- padding: '3mm',
- size: 8,
- margin_top: '2mm',
- margin_bottom: '2mm',
- font: 'SpaceMono'
- },
- table: {
- auto_width: true,
- margin_top: 4,
- margin_bottom: 4,
- header: {
- size: 9,
- styles: ['bold'],
- background_color: 'F0F0F0'
- },
- cell: {
- size: 9,
- border_width: '0.25mm',
- padding: 5
- }
- }
- }
- # rubocop:enable Naming/VariableNumber
+ def styles_asset_path
+ # TODO: where to put & load yml & json file
+ File.dirname(File.expand_path(__FILE__))
end
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
index 0d2b6a88598..d310eb4172c 100644
--- a/app/models/work_package/pdf_export/table_of_contents.rb
+++ b/app/models/work_package/pdf_export/table_of_contents.rb
@@ -29,7 +29,7 @@
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
+ with_margin(styles.toc_margins) do
write_toc! toc_list
end
pdf.start_new_page
@@ -46,46 +46,48 @@ module WorkPackage::PDFExport::TableOfContents
def build_toc_data_list_entry(work_package, id_wp_meta_map)
level_path = id_wp_meta_map[work_package.id][:level_path]
level = level_path.length
- style = toc_item_style(level)
+ level_style = styles.toc_item(level)
level_string = "#{level_path.join('.')}. "
- level_string_width = measure_part_width(level_string, style)
+ level_string_width = measure_part_width(level_string, level_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_part_width(page_nr_string, style)
+ page_nr_string_width = measure_part_width(page_nr_string, level_style)
{ id: work_package.id, level_string:, level_string_width:, title:, page_nr_string:, page_nr_string_width:, level: }
end
- def measure_part_width(part, style)
- measure_text_width(part, style) + toc_item_subject_indent_style
+ def measure_part_width(part, part_style)
+ measure_text_width(part, part_style) + styles.toc_item_subject_indent
end
- def write_part_float(id, part, style)
+ def write_part_float(id, part, part_style)
text = make_link_anchor(id, part)
pdf.float do
- pdf.text(text, style.merge({ inline_format: true }))
+ pdf.text(text, part_style.merge({ inline_format: true }))
end
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(toc_item[:level])) do
+ with_margin(styles.toc_item_margins(toc_item[:level])) do
write_toc_item! toc_item, max_level_string_width
end
end
end
- def write_toc_item_subject!(toc_item, max_level_width, style)
- style[:styles] = [style[:style]] if style[:style]
+ def write_toc_item_subject!(toc_item, max_level_width, subject_style)
pdf.indent(max_level_width, toc_item[:page_nr_string_width]) do
- pdf.formatted_text([style.merge({ text: toc_item[:title] })])
+ pdf.formatted_text([subject_style.merge({ text: toc_item[:title] })])
end
end
def write_toc_item!(toc_item, max_level_width)
- style = toc_item_style(toc_item[:level])
- write_part_float(toc_item[:id], toc_item[:level_string], style)
- write_part_float(toc_item[:id], toc_item[:page_nr_string], style.merge({ align: :right }))
- write_toc_item_subject!(toc_item, max_level_width, style)
+ toc_item_style = styles.toc_item(toc_item[:level])
+ part_style = toc_item_style.clone
+ font_styles = part_style.delete(:styles) || []
+ part_style[:style] = font_styles[0] unless font_styles.empty?
+ write_part_float(toc_item[:id], toc_item[:level_string], part_style)
+ write_part_float(toc_item[:id], toc_item[:page_nr_string], part_style.merge({ align: :right }))
+ write_toc_item_subject!(toc_item, max_level_width, toc_item_style)
end
end
diff --git a/app/models/work_package/pdf_export/view.rb b/app/models/work_package/pdf_export/view.rb
index dba00ba2801..5b92fe3ad93 100644
--- a/app/models/work_package/pdf_export/view.rb
+++ b/app/models/work_package/pdf_export/view.rb
@@ -105,10 +105,10 @@ class WorkPackage::PDFExport::View
info[:Title]
end
- def apply_font(name: nil, style: nil, size: nil)
+ def apply_font(name: nil, font_style: nil, size: nil)
name ||= document.font.basename.split('-').first # e.g. NotoSans-Bold => NotoSans
font_opts = {}
- font_opts[:style] = style if style
+ font_opts[:style] = font_style if font_style
document.font name, font_opts
document.font_size size if size
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 2abf769e412..a04e000ccdb 100644
--- a/app/models/work_package/pdf_export/work_package_detail.rb
+++ b/app/models/work_package/pdf_export/work_package_detail.rb
@@ -36,9 +36,9 @@ module WorkPackage::PDFExport::WorkPackageDetail
end
def write_work_package_detail!(work_package, id_wp_meta_map_entry)
- write_optional_page_break(page_break_space_left_threshold)
+ write_optional_page_break
id_wp_meta_map_entry[:page_number] = current_page_nr
- with_margin(wp_detail_margins_style) do
+ with_margin(styles.wp_margins) do
write_work_package_subject! work_package, id_wp_meta_map_entry[:level_path]
write_work_package_detail_content! work_package
end
@@ -53,11 +53,13 @@ module WorkPackage::PDFExport::WorkPackageDetail
private
def write_work_package_subject!(work_package, level_path)
- with_margin(wp_detail_subject_margins_style) do
+ with_margin(styles.wp_detail_subject_margins) do
link_target_at_current_y(work_package.id)
level_string_width = write_work_package_level!(level_path)
title = get_column_value work_package, :subject
- @pdf.indent(level_string_width) { pdf.formatted_text([wp_detail_subject_font_style.merge({ text: title })]) }
+ @pdf.indent(level_string_width) do
+ pdf.formatted_text([styles.wp_subject.merge({ text: title })])
+ end
end
end
@@ -65,8 +67,8 @@ module WorkPackage::PDFExport::WorkPackageDetail
return 0 if level_path.empty?
level_string = "#{level_path.join('.')}. "
- level_string_width = measure_text_width(level_string, wp_detail_subject_font_style)
- @pdf.float { @pdf.formatted_text([wp_detail_subject_font_style.merge({ text: level_string })]) }
+ level_string_width = measure_text_width(level_string, styles.wp_subject)
+ @pdf.float { @pdf.formatted_text([styles.wp_subject.merge({ text: level_string })]) }
level_string_width
end
@@ -76,11 +78,11 @@ module WorkPackage::PDFExport::WorkPackageDetail
else
build_attributes_table_rows(work_package)
end
- with_margin(wp_attributes_table_margins_style) do
+ with_margin(styles.wp_attributes_table_margins) do
pdf.table(
rows,
column_widths: attributes_table_column_widths,
- cell_style: wp_attributes_table_cell_style.merge({ inline_format: true })
+ cell_style: styles.wp_attributes_table_cell.merge({ inline_format: true })
)
end
end
@@ -138,7 +140,7 @@ module WorkPackage::PDFExport::WorkPackageDetail
def build_attributes_row(label, col_name, work_package)
# get work package attribute table cell data: [label, value]
[
- pdf.make_cell(label.upcase, wp_attributes_table_label_font_style),
+ pdf.make_cell(label.upcase, styles.wp_attributes_table_label_cell),
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 3db88ce8352..041780e0efd 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
@@ -37,7 +37,6 @@
# 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
diff --git a/app/models/work_package/pdf_export/work_package_to_pdf.rb b/app/models/work_package/pdf_export/work_package_to_pdf.rb
index 69bd011dbf6..bf8886d08e0 100644
--- a/app/models/work_package/pdf_export/work_package_to_pdf.rb
+++ b/app/models/work_package/pdf_export/work_package_to_pdf.rb
@@ -100,13 +100,13 @@ class WorkPackage::PDFExport::WorkPackageToPdf < Exports::Exporter
def write_history_item_headline!(journal)
headline = "#{format_time(journal.created_at)} - #{journal.user.name}"
- with_margin(wp_headline_margins_style) do
- pdf.formatted_text([wp_headline_style.merge({ text: headline })])
+ with_margin(styles.wp_section_heading_margins) do
+ pdf.formatted_text([styles.wp_section_heading.merge({ text: headline })])
end
end
def write_history_item_content!(journal)
- with_margin(wp_item_margins_style) do
+ with_margin(styles.wp_section_item_margins) do
journal.details.each do |detail|
text = journal.render_detail(detail, html: true, only_path: false)
write_markdown! work_package, "* #{text}"
@@ -127,8 +127,8 @@ class WorkPackage::PDFExport::WorkPackageToPdf < Exports::Exporter
end
def write_label!(label)
- with_margin(wp_label_margins_style) do
- pdf.formatted_text([wp_label_style.merge({ text: label })])
+ with_margin(styles.wp_label_margins) do
+ pdf.formatted_text([styles.wp_label.merge({ text: label })])
end
end
@@ -138,16 +138,16 @@ class WorkPackage::PDFExport::WorkPackageToPdf < Exports::Exporter
end
def write_changeset_item_headline!(changeset)
- headline = "#{format_time(changeset.committed_on)} - #{changeset.author}"
- with_margin(wp_headline_margins_style) do
- pdf.formatted_text([wp_headline_style.merge({ text: headline })])
+ text = "#{format_time(changeset.committed_on)} - #{changeset.author}"
+ with_margin(styles.wp_section_heading_margins) do
+ pdf.formatted_text([styles.wp_section_heading.merge({ text: })])
end
end
def write_changeset_item_content!(changeset)
if changeset.comments.present?
- with_margin(wp_item_margins_style) do
- pdf.formatted_text([wp_headline_style.merge({ text: changeset.comments.to_s })])
+ with_margin(styles.wp_section_item_margins) do
+ pdf.formatted_text([styles.wp_section_heading.merge({ text: changeset.comments.to_s })])
end
end
end