@ -50,13 +50,36 @@ module WorkPackage::PDFExport::Attachments
# Access the local file. For Carrierwave attachments, this will be blocking.
file_path = attachment . file . local_file . path
# Let's not include the raw images as the sum of all images can hit the memory limit of the worker process.
# As we do not need the full image size when printing small images into the PDF let's reduce it on the fly.
# It uses CPU and time. However, we don't expect that feature to get used often.
resized_file_path = resize_image ( file_path )
# Fit the image roughly in the center of each cell
pdf . make_cell ( image : file_path , fit : [ available_width , 125 ] , position : :center )
pdf . make_cell ( image : resized_ file_path, fit : [ available_width , 125 ] , position : :center )
rescue = > e
Rails . logger . error { " Failed to attach work package image to PDF: #{ e } #{ e . message } " }
nil
end
def resize_image ( file_path )
resized_file_path = extend_file_name_in_path ( file_path , '__x325' )
image = MiniMagick :: Image . open ( file_path )
image . resize ( " x325 " )
image . write ( resized_file_path )
resized_file_path
end
def extend_file_name_in_path ( file_path , name_suffix )
dir_path = File . dirname ( file_path )
file_extension = File . extname ( file_path )
file_name = File . basename ( file_path , '.*' )
File . join ( dir_path , " #{ file_name } #{ name_suffix } #{ file_extension } " )
end
def pdf_embeddable? ( attachment )
%w[ image/jpeg image/png ] . include? ( attachment . content_type )
end