diff --git a/app/template/rest.py b/app/template/rest.py index 9db0bed57..8ad07d423 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -200,7 +200,6 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file template = dao_get_template_by_id(notification.template_id) if template.is_precompiled_letter: - try: pdf_file = get_letter_pdf(notification) @@ -215,9 +214,9 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file content = base64.b64encode(pdf_file).decode('utf-8') if file_type == 'png': - try: - page_number = page if page else "0" + page_number = page if page else "1" + pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1) content = base64.b64encode(pdf_page).decode('utf-8') except PdfReadError as e: @@ -227,12 +226,12 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file status_code=500 ) - url = '{}/precompiled-preview.png'.format( - current_app.config['TEMPLATE_PREVIEW_API_HOST'] + url = '{}/precompiled-preview.png{}'.format( + current_app.config['TEMPLATE_PREVIEW_API_HOST'], + '?hide_notify=true' if page_number == '1' else '' ) content = _get_png_preview(url, content, notification.id, json=False) - else: template_for_letter_print = { @@ -256,7 +255,6 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file file_type, '?page={}'.format(page) if page else '' ) - content = _get_png_preview(url, data, notification.id, json=True) return jsonify({"content": content}) diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 14145014e..9d043a19c 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -1008,6 +1008,54 @@ def test_preview_letter_template_precompiled_png_file_type( assert base64.b64decode(resp['content']) == png_content +@pytest.mark.parametrize('page_number,expect_preview_url', [ + ('', 'http://localhost/notifications-template-preview/precompiled-preview.png?hide_notify=true'), + ('1', 'http://localhost/notifications-template-preview/precompiled-preview.png?hide_notify=true'), + ('2', 'http://localhost/notifications-template-preview/precompiled-preview.png') +]) +def test_preview_letter_template_precompiled_png_file_type_hide_notify_tag_only_on_first_page( + notify_api, + client, + admin_request, + sample_service, + mocker, + page_number, + expect_preview_url +): + + template = create_template(sample_service, + template_type='letter', + template_name='Pre-compiled PDF', + subject='Pre-compiled PDF', + hidden=True) + + notification = create_notification(template) + + with set_config_values(notify_api, { + 'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview', + 'TEMPLATE_PREVIEW_API_KEY': 'test-key' + }): + pdf_content = b'\x00\x01' + png_content = b'\x00\x02' + encoded = base64.b64encode(png_content).decode('utf-8') + + mocker.patch('app.template.rest.get_letter_pdf', return_value=pdf_content) + mocker.patch('app.template.rest.extract_page_from_pdf', return_value=png_content) + mock_get_png_preview = mocker.patch('app.template.rest._get_png_preview', return_value=encoded) + + admin_request.get( + 'template.preview_letter_template_by_notification_id', + service_id=notification.service_id, + notification_id=notification.id, + file_type='png', + page=page_number + ) + + mock_get_png_preview.assert_called_once_with( + expect_preview_url, encoded, notification.id, json=False + ) + + def test_preview_letter_template_precompiled_png_template_preview_500_error( notify_api, client,