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

[#49684] Upload custom picture for cover page of pdf export

as-op76e1c964617d
diff --git a/app/controllers/custom_styles_controller.rb b/app/controllers/custom_styles_controller.rb
index 3473bb4c780..ecbb2d23f0f 100644
--- a/app/controllers/custom_styles_controller.rb
+++ b/app/controllers/custom_styles_controller.rb
@@ -30,9 +30,12 @@ class CustomStylesController < ApplicationController
layout 'admin'
menu_item :custom_style
- before_action :require_admin, except: %i[logo_download export_logo_download favicon_download touch_icon_download]
- before_action :require_ee_token, except: %i[upsale logo_download export_logo_download favicon_download touch_icon_download]
- skip_before_action :check_if_login_required, only: %i[logo_download export_logo_download favicon_download touch_icon_download]
+ before_action :require_admin,
+ except: %i[logo_download export_logo_download export_cover_download favicon_download touch_icon_download]
+ before_action :require_ee_token,
+ except: %i[upsale logo_download export_logo_download export_cover_download favicon_download touch_icon_download]
+ skip_before_action :check_if_login_required,
+ only: %i[logo_download export_logo_download export_cover_download favicon_download touch_icon_download]
def show
@custom_style = CustomStyle.current || CustomStyle.new
@@ -70,6 +73,10 @@ class CustomStylesController < ApplicationController
file_download(:export_logo_path)
end
+ def export_cover_download
+ file_download(:export_cover_path)
+ end
+
def favicon_download
file_download(:favicon_path)
end
@@ -86,6 +93,10 @@ class CustomStylesController < ApplicationController
file_delete(:remove_export_logo)
end
+ def export_cover_delete
+ file_delete(:remove_export_cover)
+ end
+
def favicon_delete
file_delete(:remove_favicon)
end
@@ -151,6 +162,7 @@ class CustomStylesController < ApplicationController
def custom_style_params
params.require(:custom_style).permit(:logo, :remove_logo,
:export_logo, :remove_export_logo,
+ :export_cover, :remove_export_cover,
:favicon, :remove_favicon,
:touch_icon, :remove_touch_icon)
end
diff --git a/app/models/custom_style.rb b/app/models/custom_style.rb
index 0cf8efc221c..e12ce883900 100644
--- a/app/models/custom_style.rb
+++ b/app/models/custom_style.rb
@@ -1,6 +1,7 @@
class CustomStyle < ApplicationRecord
mount_uploader :logo, OpenProject::Configuration.file_uploader
mount_uploader :export_logo, OpenProject::Configuration.file_uploader
+ mount_uploader :export_cover, OpenProject::Configuration.file_uploader
mount_uploader :favicon, OpenProject::Configuration.file_uploader
mount_uploader :touch_icon, OpenProject::Configuration.file_uploader
@@ -21,7 +22,7 @@ class CustomStyle < ApplicationRecord
updated_at.to_i
end
- %i(favicon touch_icon export_logo logo).each do |name|
+ %i(favicon touch_icon export_logo export_cover logo).each do |name|
define_method "#{name}_path" do
image = send(name)
diff --git a/app/models/work_package/pdf_export/cover.rb b/app/models/work_package/pdf_export/cover.rb
index 71596abb468..09818c1078f 100644
--- a/app/models/work_package/pdf_export/cover.rb
+++ b/app/models/work_package/pdf_export/cover.rb
@@ -107,7 +107,8 @@ module WorkPackage::PDFExport::Cover
end
def cover_background_image
- image_file = Rails.root.join("app/assets/images/pdf/cover.png")
+ image_file = custom_cover_image
+ image_file = Rails.root.join("app/assets/images/pdf/cover.png") if image_file.nil?
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
@@ -115,12 +116,24 @@ module WorkPackage::PDFExport::Cover
[image_obj, image_info, image_opts, height]
end
+ def custom_cover_image
+ return unless CustomStyle.current.present? &&
+ CustomStyle.current.export_cover.present? && CustomStyle.current.export_cover.local_file.present?
+
+ image_file = CustomStyle.current.export_cover.local_file.path
+ content_type = OpenProject::ContentTypeDetector.new(image_file).detect
+ return unless pdf_embeddable?(content_type)
+
+ image_file
+ end
+
def write_background_image
- height = pdf.bounds.height / 2
+ half = pdf.bounds.height / 2
+ height = half
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_hero_padding[:top_padding]
+ height.clamp(half, pdf.bounds.height) - styles.cover_hero_padding[:top_padding]
end
end
diff --git a/app/views/custom_styles/show.html.erb b/app/views/custom_styles/show.html.erb
index af9c771a856..890dbc7541c 100644
--- a/app/views/custom_styles/show.html.erb
+++ b/app/views/custom_styles/show.html.erb
@@ -189,6 +189,40 @@ See COPYRIGHT and LICENSE files for more details.
</section>
<% end %>
+<%= 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"><%= I18n.t(:label_custom_export_cover) %></legend>
+
+ <% if @custom_style.id && @custom_style.export_cover.present? %>
+ <div>
+ <%= tag('img', src: custom_style_export_cover_path(digest: @custom_style.digest, filename: @custom_style.export_cover_identifier), class: 'custom-export-cover-preview') %>
+ <%= link_to t(:button_delete),
+ custom_style_export_cover_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 :export_cover, required: true, class: "attachment_choose_file", size: "15" %>
+ </div>
+ </div>
+ <div class="form--field-instructions">
+ <%= t('text_custom_export_cover_instructions') %>
+ </div>
+ </div>
+ </div>
+
+ <%= styled_button_tag t(@custom_style.export_cover.present? ? :button_replace : :button_upload), class: "button #{@custom_style.export_cover.blank? ? '-with-icon icon-add' : ''}" %>
+
+ </fieldset>
+ </section>
+<% end %>
+
<%= form_tag update_design_colors_path, method: :post, class: "form" do %>
<section class="form--section">
<fieldset class="form--fieldset -collapsible <%= 'collapsed' if @current_theme.present? %> ">
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 32235e0c07a..a5679d3e201 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1879,6 +1879,7 @@ en:
label_login: "Sign in"
label_custom_logo: "Custom logo"
label_custom_export_logo: "Custom export logo"
+ label_custom_export_cover: "Custom export cover background"
label_custom_favicon: "Custom favicon"
label_custom_touch_icon: "Custom touch icon"
label_logout: "Sign out"
@@ -2905,6 +2906,10 @@ en:
This is the logo that appears in your PDF exports.
It needs to be a PNG or JPEG image file.
A black or colored logo on transparent or white background is recommended.
+ 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 a PNG or JPEG image file with the image's dimensions about 800px width by 550px height.
+ A light image must be used to make the overlaid black text legible.
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 65ccdb1f7a4..ba484fd490a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -135,6 +135,10 @@ OpenProject::Application.routes.draw do
as: 'custom_style_export_logo',
constraints: { filename: /[^\/]*/ }
+ get 'custom_style/:digest/export_cover/:filename' => 'custom_styles#export_cover_download',
+ as: 'custom_style_export_cover',
+ constraints: { filename: /[^\/]*/ }
+
get 'custom_style/:digest/favicon/:filename' => 'custom_styles#favicon_download',
as: 'custom_style_favicon',
constraints: { filename: /[^\/]*/ }
@@ -352,6 +356,7 @@ OpenProject::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/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'
get 'design/upsale' => 'custom_styles#upsale', as: 'custom_style_upsale'
diff --git a/db/migrate/20230905126002_add_export_cover_to_custom_style.rb b/db/migrate/20230905126002_add_export_cover_to_custom_style.rb
new file mode 100644
index 00000000000..7a4cd85ad05
--- /dev/null
+++ b/db/migrate/20230905126002_add_export_cover_to_custom_style.rb
@@ -0,0 +1,8 @@
+class AddExportCoverToCustomStyle < ActiveRecord::Migration[7.0]
+ def change
+ add_column :custom_styles,
+ :export_cover,
+ :string,
+ default: nil
+ end
+end
diff --git a/frontend/src/global_styles/content/_custom_logo.sass b/frontend/src/global_styles/content/_custom_logo.sass
index 4fc0cc3d252..02830b6f187 100644
--- a/frontend/src/global_styles/content/_custom_logo.sass
+++ b/frontend/src/global_styles/content/_custom_logo.sass
@@ -4,7 +4,8 @@ img.custom-touch-icon-preview
box-shadow: 0 0 3px lightgrey
background-color: var(--header-bg-color)
-img.custom-export-logo-preview
+img.custom-export-logo-preview,
+img.custom-export-cover-preview
box-shadow: 0 0 3px lightgrey
background-color: #ffffff // Do not theme (PDF is white, so is the preview)
@@ -12,6 +13,9 @@ img.custom-logo-preview,
img.custom-export-logo-preview
height: 42px
+img.custom-export-cover-preview
+ height: 100px
+
img.custom-favicon-preview
height: 16px
background-color: whiteSmoke
diff --git a/spec/controllers/custom_styles_controller_spec.rb b/spec/controllers/custom_styles_controller_spec.rb
index 07960f0aceb..3b0b86d07fe 100644
--- a/spec/controllers/custom_styles_controller_spec.rb
+++ b/spec/controllers/custom_styles_controller_spec.rb
@@ -228,7 +228,7 @@ RSpec.describe CustomStylesController do
get :export_logo_download, params: { digest: "1234", filename: "export_logo_image.png" }
end
- context "when logo is present" do
+ context "when export logo is present" do
let(:custom_style) { build(:custom_style_with_export_logo) }
it 'will send a file' do
@@ -261,11 +261,11 @@ RSpec.describe CustomStylesController do
context 'if it exists' do
before do
allow(CustomStyle).to receive(:current).and_return(custom_style)
- allow(custom_style).to receive(:remove_logo).and_call_original
- delete :logo_delete
+ allow(custom_style).to receive(:remove_export_logo).and_call_original
+ delete :export_logo_delete
end
- it 'removes the logo from custom_style' do
+ it 'removes the export logo from custom_style' do
expect(response).to redirect_to action: :show
end
end
@@ -273,7 +273,68 @@ RSpec.describe CustomStylesController do
context 'if it does not exist' do
before do
allow(CustomStyle).to receive(:current).and_return(nil)
- delete :logo_delete
+ delete :export_logo_delete
+ end
+
+ it 'renders 404' do
+ expect(response).to have_http_status :not_found
+ end
+ end
+ end
+
+ describe "#export_cover_download" do
+ before do
+ allow(CustomStyle).to receive(:current).and_return(custom_style)
+ allow(controller).to receive(:send_file) { controller.head 200 }
+ get :export_cover_download, params: { digest: "1234", filename: "export_cover_image.png" }
+ end
+
+ context "when export cover is present" do
+ let(:custom_style) { build(:custom_style_with_export_cover) }
+
+ it 'will send a file' do
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ context "when no custom style is present" do
+ let(:custom_style) { nil }
+
+ it 'renders with error' do
+ expect(controller).not_to have_received(:send_file)
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ context "when no export cover is present" do
+ let(:custom_style) { build_stubbed(:custom_style) }
+
+ it 'renders with error' do
+ expect(controller).not_to have_received(:send_file)
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+ end
+
+ describe "#export_cover_delete", with_ee: %i[define_custom_style] do
+ let(:custom_style) { create(:custom_style_with_export_cover) }
+
+ context 'if it exists' do
+ before do
+ allow(CustomStyle).to receive(:current).and_return(custom_style)
+ allow(custom_style).to receive(:remove_cover).and_call_original
+ delete :export_cover_delete
+ end
+
+ it 'removes the export cover from custom_style' do
+ expect(response).to redirect_to action: :show
+ end
+ end
+
+ context 'if it does not exist' do
+ before do
+ allow(CustomStyle).to receive(:current).and_return(nil)
+ delete :export_cover_delete
end
it 'renders 404' do
diff --git a/spec/factories/custom_style_factory.rb b/spec/factories/custom_style_factory.rb
index d131850e30d..63dd9d981c7 100644
--- a/spec/factories/custom_style_factory.rb
+++ b/spec/factories/custom_style_factory.rb
@@ -45,6 +45,14 @@ FactoryBot.define do
end
end
+ factory :custom_style_with_export_cover, class: 'CustomStyle' do
+ export_cover do
+ Rack::Test::UploadedFile.new(
+ Rails.root.join('spec/support/custom_styles/export_logos/export_logo_image.png')
+ )
+ end
+ end
+
factory :custom_style_with_favicon, class: 'CustomStyle' do
favicon do
Rack::Test::UploadedFile.new(
diff --git a/spec/models/custom_style_spec.rb b/spec/models/custom_style_spec.rb
index f72bf35beff..ae6c82dca94 100644
--- a/spec/models/custom_style_spec.rb
+++ b/spec/models/custom_style_spec.rb
@@ -69,5 +69,11 @@ RSpec.describe CustomStyle do
let(:image) { "export_logo" }
end
end
+
+ describe "#remove_export_cover" do
+ it_behaves_like "removing an image from a custom style" do
+ let(:image) { "export_cover" }
+ end
+ end
end
end