From bc9cc5c8c20c1786c07b12578ec2cc352e668dd9 Mon Sep 17 00:00:00 2001 From: Vitor George Date: Tue, 9 Jan 2024 08:06:46 +0000 Subject: [PATCH] Handle API response when content is a blob on pdf download (#572) --- .../documents/document-download-menu.js | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/app/assets/scripts/components/documents/document-download-menu.js b/app/assets/scripts/components/documents/document-download-menu.js index 79890857..a3effeb9 100644 --- a/app/assets/scripts/components/documents/document-download-menu.js +++ b/app/assets/scripts/components/documents/document-download-menu.js @@ -159,15 +159,20 @@ export default function DocumentDownloadMenu(props) { return; } - // If we get a 200, it means the PDF is ready for download. - // We get the s3 url and use file saver to download and save the pdf. - if ( - response.status === 200 && - response.headers.get('content-type') === 'application/json' - ) { - const result = await response.json(); + if (response.status === 200) { + const contentType = response.headers.get('content-type'); + let downloadData; + + if (contentType === 'application/json') { + const result = await response.json(); + downloadData = result.pdf_url; + } else if (contentType === 'application/pdf') { + downloadData = await response.blob(); + } else { + throw new Error('Unexpected content type received.'); + } - saveAs(result.pdf_url, fileName); + saveAs(downloadData, fileName); processToast.success( 'PDF downloaded successfully! If the PDF did not open automatically, your browser may have blocked the download. Please make sure that popups are allowed on this site.' );