Convert image to base64 in PDF reports [SCI-3557] (#1835)

* Convert image to base64 in PDF reports

* Fix typo error
This commit is contained in:
aignatov-bio 2019-06-19 13:57:04 +02:00 committed by GitHub
parent 81d10c8ce4
commit 9653f16196
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 21 deletions

View file

@ -97,21 +97,8 @@ module ReportsHelper
end
# "Hack" to omit file preview URL because of WKHTML issues
def report_image_asset_url(asset, type = :asset, klass = nil)
prefix = ''
if ENV['PAPERCLIP_STORAGE'].present? &&
ENV['MAIL_SERVER_URL'].present? &&
ENV['PAPERCLIP_STORAGE'] == 'filesystem'
prefix = ENV['MAIL_SERVER_URL']
end
if !prefix.empty? &&
!prefix.include?('http://') &&
!prefix.include?('https://')
prefix = "http://#{prefix}"
end
size = type == :tiny_mce_asset ? :large : :medium
url = prefix + asset.url(size, timeout: Constants::URL_LONG_EXPIRE_TIME)
image_tag(url, class: klass)
def report_image_asset_url(asset, _type = :asset, klass = nil)
image_tag(asset.generate_base64(:medium), class: klass)
end
# "Hack" to load Glyphicons css directly from the CDN

View file

@ -493,6 +493,16 @@ class Asset < ApplicationRecord
!locked? && %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES_EDITABLE)}} =~ file.content_type
end
def generate_base64(style)
image = if file.options[:storage].to_sym == :s3
URI.parse(url(style)).open.to_a.join
else
File.open(file.path(style)).to_a.join
end
encoded_data = Base64.strict_encode64(image)
"data:#{file_content_type};base64,#{encoded_data}"
end
protected
# Checks if attachments is an image (in post processing imagemagick will

View file

@ -56,7 +56,7 @@ ActiveRecord::Schema.define(version: 20190520135317) do
t.datetime "updated_at", null: false
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.bigint "file_file_size"
t.datetime "file_updated_at"
t.bigint "created_by_id"
t.bigint "last_modified_by_id"
@ -169,7 +169,7 @@ ActiveRecord::Schema.define(version: 20190520135317) do
t.datetime "updated_at", null: false
t.string "workflowimg_file_name"
t.string "workflowimg_content_type"
t.integer "workflowimg_file_size"
t.bigint "workflowimg_file_size"
t.datetime "workflowimg_updated_at"
t.uuid "uuid"
t.index ["archived_by_id"], name: "index_experiments_on_archived_by_id"
@ -737,14 +737,14 @@ ActiveRecord::Schema.define(version: 20190520135317) do
t.datetime "updated_at", null: false
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.bigint "file_file_size"
t.datetime "file_updated_at"
end
create_table "tiny_mce_assets", force: :cascade do |t|
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.bigint "image_file_size"
t.datetime "image_updated_at"
t.integer "estimated_size", default: 0, null: false
t.integer "step_id"
@ -857,7 +857,7 @@ ActiveRecord::Schema.define(version: 20190520135317) do
t.datetime "updated_at", null: false
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.bigint "avatar_file_size"
t.datetime "avatar_updated_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
@ -924,7 +924,7 @@ ActiveRecord::Schema.define(version: 20190520135317) do
t.datetime "updated_at", null: false
t.string "zip_file_file_name"
t.string "zip_file_content_type"
t.integer "zip_file_file_size"
t.bigint "zip_file_file_size"
t.datetime "zip_file_updated_at"
t.string "type"
t.index ["user_id"], name: "index_zip_exports_on_user_id"