[#70333] Support WebP images in PDF exports

https://community.openproject.org/work_packages/70333
This commit is contained in:
as-op
2026-02-16 17:12:45 +01:00
parent 188cb5ea08
commit b48d65e5d2
3 changed files with 33 additions and 1 deletions
+11 -1
View File
@@ -42,7 +42,7 @@ module Exports::PDF::Common::Attachments
end
def pdf_embeddable?(content_type)
%w[image/jpeg image/png image/gif].include?(content_type)
%w[image/jpeg image/png image/gif image/webp].include?(content_type)
end
def delete_all_resized_images
@@ -67,6 +67,7 @@ module Exports::PDF::Common::Attachments
filename = local_file.path
filename = convert_gif_to_png(filename) if attachment.content_type == "image/gif"
filename = convert_webp_to_png(filename) if attachment.content_type == "image/webp"
resize_image(filename)
end
@@ -86,6 +87,15 @@ module Exports::PDF::Common::Attachments
tmp_file
end
def convert_webp_to_png(filename)
tmp_file = temp_image_file(".png")
image = MiniMagick::Image.open(filename)
image.format("png")
image.write(tmp_file)
tmp_file
end
def attachment_by_api_content_src(src)
return nil if src.empty?
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 68 B

@@ -376,6 +376,28 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
expect(exporter.send(:pdf_embeddable?, "image/png")).to be true
expect(exporter.send(:pdf_embeddable?, "image/jpeg")).to be true
expect(exporter.send(:pdf_embeddable?, "image/gif")).to be true
expect(exporter.send(:pdf_embeddable?, "image/webp")).to be true
end
end
describe "with WebP image attachment" do
let(:webp_path) { Rails.root.join("spec/fixtures/files/image.webp") }
let(:webp_attachment) { Attachment.new author: user, file: File.open(webp_path) }
let(:attachments) { [webp_attachment] }
let(:description) do
<<~DESCRIPTION
This work package contains a WebP image.
![](/api/v3/attachments/#{webp_attachment.id}/content)
DESCRIPTION
end
before do
webp_attachment.save
end
it "converts WebP images and includes them in the PDF export" do
expect(webp_attachment.content_type).to eq "image/webp"
expect(pdf[:images].length).to eq(1)
end
end