Converting returned pdf data back into pdf

Hi i’ve been trying to get a document from the filing history using the apis. first i get all the documents from the filing history, then i capture the document id and pass it to the /{document_id}/content end point. after this i’m not sure what the data is thats being returned. and have no idea how to turn it into a pdf.

i’m a junior developer and have been tasked to create a companies house app. and have been doing fine up until retrieving a document. i have written the code for the fetch in node.js, and have a ract front end. and using axios request in node and then fetch in react. ill attach some pics. i hope someone will be able to help many thanks for reading my query.

I could only upload one photo so at a time so heres the others. thanks again

Coincidentally i’ve re written the request as a fetch request on the front end and used the browser extension to turn off cors, this is what i get back in the network. could anyone shed some light on the cors issue?

after this i’m not sure what the data is thats being returned. and have no idea how to turn it into a pdf.

You seem to have 3 questions here. Two I understand (first / last) but for the middle one I can’t see what the problem is. Hopefully I can answer 1 and 3 and you can provide more explanation on 2?

(1) First you said:

after this i’m not sure what the data is thats being returned

That one’s simple (ish) - see e.g. my posts in the following threads:

(2) The bit I don’t understand. You say:

have no idea how to turn it into a pdf.

…but (your first post / second posts) you appear to be requesting a PDF, getting PDF data and your client side script seems to be sending that to some “openPDF” function. What’s the problem?

I’m not sure why you’re putting the (binary) PDF data through JSON.stringify() and then converting back again (can you not just use e.g. Response.blob()?) but maybe you’ve another reason for that (e.g. are there requirements on openPDF?). However it looks like your client script is parsing the json string (response.json() ) you’re sending. So far, so good? What have I missed? Did you try using the browser’s network monitor in the client to check the data you get back?

(3) Your last screenshot is explained as follows:

  • You make a request to the Companies House document content endpoint.
  • You do a successful preflight.
  • You get back a 302 redirect. This is to Amazon (where the file is hosted).
  • The browser then tries to follow that onwards but we’re now on a different host so it throws an error.

I think you need to catch that and pull out the redirect URL. Note - I’ve not done this in javascript and am not entirely sure if it’s possible there.

You need to manually follow the redirect url because according to MDN you won’t be able to continue the CORS request as the host (server) has changed. (However I see the spec changed to allow this though a while back - maybe browsers haven’t caught up though?). Looks like you need some combination of “simple request” and setting redirect to manual / “disallow redirects”.

After that error you get a 403 - presumably because (as mentioned in my posts at the top) you’re passing the Companies House authentication (http Basic) to Amazon. That is an error because (a) it’s not valid and (b) Amazon has its own authentication in the url so on receiving two types of auth it rejects the request.

Good luck.

Wow thank you for your detailed reply. let me try and read though it and understand it all.
basically all i am trying to achieve is to make a request to companies house, get the selected document and then render it to the page using js.

as i’m a junior dev i’ve only been learning for 6 months. so alot of the code wont make sense as i dont fully understand it myself. the JSON.stringyfi is just me trying different ways to get it to work unsuccessfully.

the reason i turned it into a string is because i was sending it from a node app to the frint end react app and was told when you send info you need to turn it into a JSON string first and then parse that string the other end

The open pdf was some script i found online to open a pdf, but this wasn’t working either.

was sending it from a node app to the frint end react app and was told when you send info you need to turn it into a JSON string first and then parse that string the other end

Commonly, but not always e.g.

If you’ve got the PDF data back to the client then you’ve covered all that you’ll want to ask on this forum. You’re just on to “handling data using js” at that point.

For rendering there’s plenty you could use e.g. PDF.js