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

[#66384] Custom font for PDF Exports

as-op109a886c356c
diff --git a/app/controllers/custom_styles_controller.rb b/app/controllers/custom_styles_controller.rb
index 8ae0dbcb94f..c269eb3ef4c 100644
--- a/app/controllers/custom_styles_controller.rb
+++ b/app/controllers/custom_styles_controller.rb
@@ -35,8 +35,6 @@ class CustomStylesController < ApplicationController
menu_item :custom_style
UNGUARDED_ACTIONS = %i[logo_download
- export_logo_download
- export_cover_download
favicon_download
touch_icon_download].freeze
@@ -132,6 +130,22 @@ class CustomStylesController < ApplicationController
file_delete(:remove_favicon)
end
+ def export_font_regular_delete
+ file_delete(:remove_export_font_regular)
+ end
+
+ def export_font_bold_delete
+ file_delete(:remove_export_font_bold)
+ end
+
+ def export_font_italic_delete
+ file_delete(:remove_export_font_italic)
+ end
+
+ def export_font_bold_italic_delete
+ file_delete(:remove_export_font_bold_italic)
+ end
+
def touch_icon_delete
file_delete(:remove_touch_icon)
end
@@ -148,8 +162,8 @@ class CustomStylesController < ApplicationController
def update_themes
call = ::Design::UpdateDesignService
- .new(theme_from_params)
- .call
+ .new(theme_from_params)
+ .call
call.on_success do
flash[:notice] = I18n.t(:notice_successful_update)
@@ -183,12 +197,18 @@ class CustomStylesController < ApplicationController
end
def custom_style_params
- params.require(:custom_style).permit(:logo, :remove_logo,
- :export_logo, :remove_export_logo,
- :export_cover, :remove_export_cover,
- :export_cover_text_color,
- :favicon, :remove_favicon,
- :touch_icon, :remove_touch_icon)
+ params.expect(custom_style: %i[
+ logo remove_logo
+ export_logo remove_export_logo
+ export_cover remove_export_cover
+ favicon remove_favicon
+ touch_icon remove_touch_icon
+ export_font_regular remove_export_font_regular
+ export_font_bold remove_export_font_bold
+ export_font_italic remove_export_font_italic
+ export_font_bold_italic remove_export_font_bold_italic
+ export_cover_text_color
+ ])
end
def file_download(path_method)
diff --git a/app/helpers/custom_styles_helper.rb b/app/helpers/custom_styles_helper.rb
index 5a47f61e1d8..c63647fbd67 100644
--- a/app/helpers/custom_styles_helper.rb
+++ b/app/helpers/custom_styles_helper.rb
@@ -48,6 +48,12 @@ module CustomStylesHelper
partial: "custom_styles/pdf_export_styles",
path: custom_style_path(tab: :pdf_export_styles),
label: t(:"admin.custom_styles.tab_pdf_export_styles")
+ },
+ {
+ name: "pdf_export_font",
+ partial: "custom_styles/pdf_export_font",
+ path: custom_style_path(tab: :pdf_export_font),
+ label: t(:"admin.custom_styles.tab_pdf_export_font")
}
]
end
@@ -72,4 +78,23 @@ module CustomStylesHelper
def apply_custom_touch_icon?
apply_custom_styles?(skip_ee_check: false) && CustomStyle.current.touch_icon.present?
end
+
+ def export_font_file(custom_style, font)
+ custom_style.id && font.present? ? File.basename(font.file.path) : nil
+ end
+
+ def export_fonts(custom_style)
+ %i[regular bold italic bold_italic].map do |variant|
+ field = :"export_font_#{variant}"
+ font = custom_style.public_send(field)
+ {
+ field: field,
+ label: I18n.t(:"label_custom_export_font_#{variant}"),
+ present: font.present?,
+ filename: export_font_file(custom_style, font),
+ delete_path: public_send(:"custom_style_export_font_#{variant}_delete_path"),
+ instructions: I18n.t(:"text_custom_export_font_#{variant}_instructions")
+ }
+ end
+ end
end
diff --git a/app/models/custom_style.rb b/app/models/custom_style.rb
index e8b76035a4d..18da96906d7 100644
--- a/app/models/custom_style.rb
+++ b/app/models/custom_style.rb
@@ -34,6 +34,10 @@ class CustomStyle < ApplicationRecord
mount_uploader :export_cover, OpenProject::Configuration.file_uploader
mount_uploader :favicon, OpenProject::Configuration.file_uploader
mount_uploader :touch_icon, OpenProject::Configuration.file_uploader
+ mount_uploader :export_font_regular, OpenProject::Configuration.file_uploader
+ mount_uploader :export_font_bold, OpenProject::Configuration.file_uploader
+ mount_uploader :export_font_italic, OpenProject::Configuration.file_uploader
+ mount_uploader :export_font_bold_italic, OpenProject::Configuration.file_uploader
class << self
def current
@@ -52,18 +56,19 @@ class CustomStyle < ApplicationRecord
updated_at.to_i
end
- %i(favicon touch_icon export_logo export_cover logo).each do |name|
+ %i(favicon touch_icon export_logo export_cover logo
+ export_font_regular export_font_bold export_font_italic export_font_bold_italic).each do |name|
define_method :"#{name}_path" do
- image = send(name)
+ attachment = send(name)
- if image.readable?
- image.local_file.path
+ if attachment.readable?
+ attachment.local_file.path
end
end
define_method :"remove_#{name}" do
- image = send(name)
- image&.remove!
+ attachment = send(name)
+ attachment&.remove!
if new_record?
send(:"#{name}=", nil)
diff --git a/app/views/custom_styles/_pdf_export_font.html.erb b/app/views/custom_styles/_pdf_export_font.html.erb
new file mode 100644
index 00000000000..98d0bd010e2
--- /dev/null
+++ b/app/views/custom_styles/_pdf_export_font.html.erb
@@ -0,0 +1,58 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 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.
+
+++#%>
+
+<p><%= I18n.t(:label_custom_export_font_instructions) %></p>
+
+<% export_fonts(@custom_style).each do |font| %>
+ <%= form_for @custom_style, url: custom_style_path, html: { multipart: true, class: "form -vertical" } do |f| %>
+ <section class="form--section">
+ <fieldset class="form--fieldset">
+ <legend class="form--fieldset-legend"><%= font[:label] %></legend>
+ <% if font[:present] %>
+ <div>
+ <span><%= font[:filename] %></span>
+ <%= link_to t(:button_delete), font[:delete_path], method: :delete, class: "icon icon-delete confirm-form-submit" %>
+ </div>
+ <% end %>
+ <div class="grid-block">
+ <div class="form--field -required">
+ <div class="attachment_field form--field-container -vertical -shrink">
+ <div class="form--file-container">
+ <%= f.file_field font[:field], required: true, class: "attachment_choose_file", accept: "font/ttf", size: "15" %>
+ </div>
+ </div>
+ <div class="form--field-instructions"><%= font[:instructions] %></div>
+ </div>
+ </div>
+ <%= styled_button_tag t(font[:present] ? :button_replace : :button_upload),
+ class: "button #{font[:present] ? '' : '-with-icon icon-add'}" %>
+ </fieldset>
+ </section>
+ <% end %>
+<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 00a186a12a9..3a43b98d339 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -68,6 +68,7 @@ en:
tab_interface: "Interface"
tab_branding: "Branding"
tab_pdf_export_styles: "PDF export styles"
+ tab_pdf_export_font: "PDF export font"
colors:
primary-button-color: "Primary button"
accent-color: "Accent"
@@ -3039,6 +3040,10 @@ en:
label_custom_logo: "Custom logo"
label_custom_export_logo: "Custom export logo"
label_custom_export_cover: "Custom export cover background"
+ label_custom_export_font_regular: "Regular"
+ label_custom_export_font_bold: "Bold"
+ label_custom_export_font_italic: "Italic"
+ label_custom_export_font_bold_italic: "Bold Italic"
label_custom_export_cover_overlay: "Custom export cover background overlay"
label_custom_export_cover_text_color: "Text color"
label_custom_pdf_export_settings: "Custom PDF export settings"
@@ -4394,6 +4399,18 @@ en:
text_custom_export_cover_instructions: >
This is the image that appears in the background of a cover page in your PDF exports.
It needs to be an about 800px width by 500px height sized PNG or JPEG image file.
+ label_custom_export_font_instructions: >
+ The following font files are used in your PDF exports.
+ Upload and manage custom TrueType (.ttf) fonts for PDF exports. Ensure your license permits PDF embedding.
+ For best results, use matching files from the same font family. If no font is provided, the default NotoSans font will be used.
+ text_custom_export_font_regular_instructions: >
+ This is the font file for regular text. It needs to be in TTF format and is required.
+ text_custom_export_font_bold_instructions: >
+ This is the font file for bold text. It needs to be in TTF format.
+ text_custom_export_font_italic_instructions: >
+ This is the font file for italic text. It needs to be in TTF format.
+ text_custom_export_font_bold_italic_instructions: >
+ This is the font file for bold and italic text. It needs to be in TTF format.
text_custom_favicon_instructions: >
This is the tiny icon that appears in your browser window/tab next to the
page's title.
diff --git a/config/routes.rb b/config/routes.rb
index b6fc9e405a2..fda3f967be9 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -503,6 +503,12 @@ Rails.application.routes.draw do
delete "design/logo" => "custom_styles#logo_delete", as: "custom_style_logo_delete"
delete "design/export_logo" => "custom_styles#export_logo_delete", as: "custom_style_export_logo_delete"
delete "design/export_cover" => "custom_styles#export_cover_delete", as: "custom_style_export_cover_delete"
+ delete "design/export_font_regular" => "custom_styles#export_font_regular_delete",
+ as: "custom_style_export_font_regular_delete"
+ delete "design/export_font_bold" => "custom_styles#export_font_bold_delete", as: "custom_style_export_font_bold_delete"
+ delete "design/export_font_italic" => "custom_styles#export_font_italic_delete", as: "custom_style_export_font_italic_delete"
+ delete "design/export_font_bold_italic" => "custom_styles#export_font_bold_italic_delete",
+ as: "custom_style_export_font_bold_italic_delete"
delete "design/favicon" => "custom_styles#favicon_delete", as: "custom_style_favicon_delete"
delete "design/touch_icon" => "custom_styles#touch_icon_delete", as: "custom_style_touch_icon_delete"
post "design/colors" => "custom_styles#update_colors", as: "update_design_colors"
diff --git a/db/migrate/20250811102200_add_pdf_fonts_to_custom_styles.rb b/db/migrate/20250811102200_add_pdf_fonts_to_custom_styles.rb
new file mode 100644
index 00000000000..5f2a48da01c
--- /dev/null
+++ b/db/migrate/20250811102200_add_pdf_fonts_to_custom_styles.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+#++
+
+class AddPdfFontsToCustomStyles < ActiveRecord::Migration[8.0]
+ def change
+ change_table :custom_styles, bulk: true do |t|
+ t.string :export_font_regular, null: true
+ t.string :export_font_bold, null: true
+ t.string :export_font_italic, null: true
+ t.string :export_font_bold_italic, null: true
+ end
+ end
+end
diff --git a/spec/controllers/custom_styles_controller_spec.rb b/spec/controllers/custom_styles_controller_spec.rb
index 26720bcef50..eb3cc6e8529 100644
--- a/spec/controllers/custom_styles_controller_spec.rb
+++ b/spec/controllers/custom_styles_controller_spec.rb
@@ -233,7 +233,7 @@ RSpec.describe CustomStylesController do
end
end
- describe "#export_logo_download" do
+ describe "#export_logo_download", with_ee: %i[define_custom_style] do
before do
allow(CustomStyle).to receive(:current).and_return(custom_style)
allow(controller).to receive(:send_file) { controller.head 200 }
@@ -294,7 +294,7 @@ RSpec.describe CustomStylesController do
end
end
- describe "#export_cover_download" do
+ describe "#export_cover_download", with_ee: %i[define_custom_style] do
before do
allow(CustomStyle).to receive(:current).and_return(custom_style)
allow(controller).to receive(:send_file) { controller.head 200 }