Merge pull request #10078 from demarches-simplifiees/add_metadata_to_pdf_generation

Tech: ajoute des informations de debug lors de la generation de pdf utilisant weasyprint
This commit is contained in:
LeSim 2024-03-21 20:48:08 +00:00 committed by GitHub
commit b471cbbcb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 44 additions and 6 deletions

View file

@ -20,11 +20,27 @@ module Administrateurs
format.pdf do
html = render_to_string('/administrateurs/attestation_template_v2s/show', layout: 'attestation', formats: [:html])
result = Typhoeus.post(WEASYPRINT_URL,
headers: { 'content-type': 'application/json' },
body: { html: html }.to_json)
headers = {
'Content-Type' => 'application/json',
'X-Request-Id' => Current.request_id
}
send_data(result.body, filename: 'attestation.pdf', type: 'application/pdf', disposition: 'inline')
body = {
html: html,
upstream_context: {
procedure_id: @procedure.id,
path: request.path,
user_id: current_user.id
}
}.to_json
response = Typhoeus.post(WEASYPRINT_URL, headers:, body:)
if response.success?
send_data(response.body, filename: 'attestation.pdf', type: 'application/pdf', disposition: 'inline')
else
raise StandardError.new("PDF Generation failed: #{response.return_code} #{response.status_message}")
end
end
end
end

View file

@ -1,3 +1,14 @@
- content_for :title, @title
- content_for :authors do
- @authors&.map do |author|
%meta{ name: 'author', content: author }
- content_for :description do
- if @description.present?
%meta{ name: 'description', content: @description }
- content_for :keywords do
- @keywords&.map do |keyword|
%meta{ name: 'keywords', content: keyword }
.a4-container{ class: class_names("official-layout": @attestation_template.official_layout?) }
.content
%header.first-header

View file

@ -1,6 +1,12 @@
%html
%html{ lang: 'fr' }
%head
= stylesheet_link_tag 'attestation', media: 'all'
%title= content_for?(:title) ? yield(:title) : 'Attestation'
= yield(:authors)
= yield(:keywords)
= yield(:description)
%meta{ name: 'generator', content: ENV.fetch('APP_HOST') }
%meta{ name: 'dcterms.created', content: Time.zone.now.iso8601 }
%body#attestation
= yield

View file

@ -65,7 +65,12 @@ describe 'As an administrateur, I want to manage the procedures attestation',
end
context 'Update attestation v2' do
before { Flipper.enable(:attestation_v2) }
before do
Flipper.enable(:attestation_v2)
response = Typhoeus::Response.new(code: 200, body: 'Hello world')
Typhoeus.stub(WEASYPRINT_URL).and_return(response)
end
scenario do
visit admin_procedure_path(procedure)