attestation: fix preview of logos

Turns out that assigning the params to the procedure calls `read` on the
images files. Calling `read` moves the seek offset to the end of the
file; which means that subsequent calls to `read` return an empty
string.

Fix the issue by calling `rewind` before attempting to `read` the file,
to ensure the seek offset is at the beginning of the file.

Fix #4958
This commit is contained in:
Pierre de La Morinerie 2020-03-30 14:48:25 +02:00
parent b8c3d7c6bd
commit ed5f2fa35f
2 changed files with 14 additions and 6 deletions

View file

@ -38,12 +38,12 @@ prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], p
pdf.bounding_box([0, pdf.cursor], width: body_width, height: body_height) do
if logo.present?
logo_file = if logo.is_a?(ActiveStorage::Attached::One)
logo_content = if logo.is_a?(ActiveStorage::Attached::One)
logo.download
else
logo.read
logo.rewind && logo.read
end
pdf.image StringIO.new(logo_file), fit: [max_logo_width , max_logo_height], position: :center
pdf.image StringIO.new(logo_content), fit: [max_logo_width , max_logo_height], position: :center
end
pdf.fill_color grey
@ -57,12 +57,12 @@ prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], p
if signature.present?
pdf.pad_top(40) do
signature_file = if signature.is_a?(ActiveStorage::Attached::One)
signature_content = if signature.is_a?(ActiveStorage::Attached::One)
signature.download
else
signature.read
signature.rewind && signature.read
end
pdf.image StringIO.new(signature_file), fit: [max_signature_size , max_signature_size], position: :right
pdf.image StringIO.new(signature_content), fit: [max_signature_size , max_signature_size], position: :right
end
end
end

View file

@ -46,6 +46,14 @@ describe Admin::AttestationTemplatesController, type: :controller do
context 'if an attestation template does not exist on the procedure' do
let(:attestation_template) { nil }
context 'with images' do
let(:upload_params) { { title: 't', body: 'b', footer: 'f', logo: interlaced_logo } }
it { expect(assigns(:attestation)).to include({ title: 't', body: 'b', footer: 'f' }) }
it { expect(assigns(:attestation)[:logo]).to be_present }
it_behaves_like 'rendering a PDF successfully'
end
context 'without images' do
let(:upload_params) { { title: 't', body: 'b', footer: 'f' } }