I'm receiving a multipart/mixed response over HTTP, which contains some JSON data as well as PDFs in byte format. Since Angular cannot handle such responses naively, my current approach is to convert the response into a string using the responseType: 'text' option.
I then take the response apart, parse the JSON, and put the PDF data into a blob like this:
let pdf: Blob = new Blob([new TextEncoder().encode(bytestring)], { type: 'application/pdf' });
However, when I want to create a download link for the PDF with window.URL.createObjectURL(pdf), the downloaded PDF is damaged and can't be opened.
I have confirmed that when Angular turns the response into a string, it uses UTF-8 encoding. I also implemented a separate route so I can request a single PDF on its own, allowing me to use responseType: 'blob', which works and downloads a functioning PDF. Furthermore, I forced VS Code to open both the original PDF file as well as the damaged one and the representation of the bytes is identical.
Since I'm able to transfer a functioning PDF when not sending it as part of a multipart request, it seems to me that the only possible cause for the broken PDF is the way I parse the multipart request, and I have read elsewhere that converting a PDF into a string and then back into a PDF can be problematic. So, is there any way to do this without converting it into a string?