I have a frontend/backend working, but struggling to get the backend to grab a PDF, turn it into an array of bytes, send it to the front end, and have the front end transform it into a blob, and display the working PDF on the front end display. Currently, the error I am getting is that its displaying on the front end as “Error , failed to load PDF document” and not giving much other error info. So its sending the info, but maybe not formatting correctly?
Image of error
I took out some info that I cannot show (what Item is, or how the Item is used on the backend to grab the correct pdf, sorry)
Backend :
[Route("grab-pdf")]
[ApiController]
public async Task <ActionResult> Create(request) {
byte[] pdf = system.IO.File.ReadAllByters("fileexample.pdf");
return File()pdf, "application/pdf","pdfexampletitle");
}
frontend :
var items = [{ id: "1", amount : 100}]
grabPDF();
async function grabPDF() {
const response = await fetch("/grab-pdf", {
method : "POST",
headers : {"Content-Type": "application/json"},
body : JSON.stringify ({items}),
});
const {invAmt} = await response.blob();
//const blob = new blob([(response.blob()], { type: 'application/pdf' }); //I tried using this, but gave same error
const url = URL.createObjectURL(invAmt);
const iframe = document.createElement("iframe");
iframe.src = url
iframe.style.width = '100%';
iframe.style.height = '900px';
document.body.appendChild(iframe);
}