# PDF Downloaded through CHS API not getting opened

**URL:** <http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919>\
**Category:** API Issues\
**Created:** [February 16, 2023, 11:26am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919 "2023-02-16T11:26:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhijeet.kate](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@abhijeet.kate](http://forum.companieshouse.gov.uk/u/abhijeet.kate)\
**Post date:** [February 16, 2023, 11:26am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/1 "2023-02-16T11:26:33Z")

</div>

Hi all,

I am able to download PDF file using CHS document API by calling correct endpoint with document id as input, however, file size is just getting downloaded is 1KB only, however actual size of the file is more than 2 MB, also the 1 KB file is not getting opened, tried with Adobe and browser both.  
Please help to resolve if you can.

Thanks

---

<div class="post-metadata">

**Author:** ![voracityemail](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/v/b77776/32.png) [@voracityemail](http://forum.companieshouse.gov.uk/u/voracityemail)\
**Post date:** [February 16, 2023, 11:34am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/2 "2023-02-16T11:34:23Z")

</div>

First - did you check the contents of what you have downloaded? I suspect you’ll find it’s e.g. an error message.

The fetch documents API has some features which mean that some people find it difficult to get working on their first attempt. I’ve a post in the following thread which covers the whole process - hope this helps.

> **[Fetch a document API](https://forum.aws.chdev.org/t/fetch-a-document-api/978/4)**
>
> Welcome to the forum! If you’re trying to use the “Explore this API” form on the Document Download page I don’t think this works - see e.g. Otherwise, you should be able to find the answers in @MArkWilliams post above yours - as he says the...

---

<div class="post-metadata">

**Author:** ![abhijeet.kate](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@abhijeet.kate](http://forum.companieshouse.gov.uk/u/abhijeet.kate)\
**Post date:** [February 17, 2023, 12:35pm UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/3 "2023-02-17T12:35:31Z")

</div>

Thanks for responding Vora-  
Yes, file size is 1 KB and could see file metadata only when opened in notepad like, Date, type of document etc, however actual file has size more than 2 MB and has different data.  
couldn’t see any error, its just seems file is getting compressed at CHS end and its not getting decompress when it receives at our end.(its my assumption- correct me if i am wrong.)

---

<div class="post-metadata">

**Author:** ![voracityemail](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/v/b77776/32.png) [@voracityemail](http://forum.companieshouse.gov.uk/u/voracityemail)\
**Post date:** [February 17, 2023, 8:07pm UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/4 "2023-02-17T20:07:41Z")

</div>

Did you follow the steps as e.g. set out in my linked post? (Again - using curl for this may help because it’s really clear what is going on, you can get a more verbose output of the actual communication etc.)

Did you read [the documentation on the Document API](https://developer-specs.company-information.service.gov.uk/document-api/reference)?

If you do that then you will be able to see which step is not working as you expect and why - e.g. “I requested the document metadata using url [https://document-api.company-information.service.gov.uk/document/MzM1Mzk0MDk4MmFkaXF6a2N4](https://document-api.company-information.service.gov.uk/document/MzM1Mzk0MDk4MmFkaXF6a2N4) and I get a JSON response (response data), I parse that, ensure that the content type I want (e.g. PDF - application/pdf) is available, request the document using the link in the document metadata (link) but then I get back (some response)”

If you are getting back metadata it sounds like you have only got as far as requesting the entry from the Filing History List, or only requesting the document metadata. There are additional steps to get the actual document data as my comment notes.

If you’re certain you’re requesting the data in the correct way then you could try posting back here exactly what you’re doing, what document you’re trying to download (e.g. companies House document ref / url) the URL you request to download the document (as the actual filings are hosted on amazon servers) and the data you get back. Don’t post your own Companies House API key / username and password though!

---

<div class="post-metadata">

**Author:** ![abhijeet.kate](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@abhijeet.kate](http://forum.companieshouse.gov.uk/u/abhijeet.kate)\
**Post date:** [February 20, 2023, 10:34am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/5 "2023-02-20T10:34:11Z")

</div>

Thanks for your response.  
Please find below URls used to download document.  
“[https://document-api.company-information.service.gov.uk/document/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY](https://document-api.company-information.service.gov.uk/document/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY)”  
got this document ID from metadata column. i am able to download document but it just has metadata and file size is just 1 KB. please guide.  
also just to let you know we are not using curl here, we are calling those URLs from Blue prism studio (RPA tool)

---

<div class="post-metadata">

**Author:** ![voracityemail](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/v/b77776/32.png) [@voracityemail](http://forum.companieshouse.gov.uk/u/voracityemail)\
**Post date:** [February 20, 2023, 12:40pm UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/6 "2023-02-20T12:40:10Z")

</div>

As it should be clear above - it sounds like you’re getting to the last step but one. Downloading the document metadata is downloading the metadata. After that you need to parse that data to get the correct URL to download the document content.

Please see my linked post - the steps:

(3) [Request the actual document](https://developer.companieshouse.gov.uk/document/docs/document/id/content/fetchDocument.html), specifying the mime type (e.g. “application/pdf”).  
(4) CH send back a response which is a [redirect (http 302)](https://en.wikipedia.org/wiki/HTTP_302) to the document. The documents are stored on Amazon servers ATM.  
(5) Request this URI from Amazon again passing the content type you want again.  
(6) Amazon send the actual document data.

Again this is why I recommend using curl initially - it’s easier to see what is happening.

You requested:

`https://document-api.company-information.service.gov.uk/document/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY`

That should give you a JSON response, like shown below (I’ve snipped some parts marked “…”)

```
{
    "company_number": "00048839",
    ...
    "links": {
        "self": ...
        "document": "https://document-api.company-information.service.gov.uk/document/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY/content"
    },
    "resources": {
        "application/pdf": {
            "content_length": 437669
        }
    }
}

```

You need to check the “resources” member for available data types (mime types), then request the URL given in “document”. I think the resource type defaults to the given one if there is only one but it makes sense to always specify which one you want - and especially if there is more than one type of resource. You do that - as the documentation says - by setting that in the [http “Accept” header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept)).

So I’d request:

`curl -u YOUR_API_KEY_HERE: -H "Accept: application/pdf" https://document-api.company-information.service.gov.uk/document/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY/content`

Note the “/content” at the end there - that tells companies House you want the actual data.

You do not get back the actual data immediately. You get a redirect to Amazon’s AWS servers. So that’s an http 302:

```
< HTTP/1.1 302 Found
< Date: Mon, 20 Feb 2023 12:20:25 GMT
< Location: https://s3.eu-west-2.amazonaws.com/document-api-images-live.ch.gov.uk/docs/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY/application-pdf?...

```

(Again some data snipped here “…” to save space)

Depending on your language / tool you may have to catch that redirect URL in your own code. This is because if your tool / code incorrectly tries to send the Companies House authorisation (e.g. API key) to Amazon (e.g. by automatically following the link) that will cause Amazon to send you an error. You will also need to request the link from Amazon quickly as it is time-limited e.g. it will expire at some point.

So in your example I did this and correctly downloaded a PDF file of 437669 bytes.

Good luck.

---

<div class="post-metadata">

**Author:** ![abhijeet.kate](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@abhijeet.kate](http://forum.companieshouse.gov.uk/u/abhijeet.kate)\
**Post date:** [February 24, 2023, 7:12am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/7 "2023-02-24T07:12:16Z")

</div>

Thanks for your response and suggestions.

This is what i got in response when called Document API with content/ end point.

%PDF-1.1  
%����  
1 0 obj  
\<\<  
/Type /Catalog  
/Pages 3 0 R

> >

endobj  
2 0 obj  
\<\<  
/CreationDate (D:20210612050113)  
/ModDate (D:20210612050113)  
/Producer (libtiff / tiff2pdf - 20150912)  
/Creator (go-tiff2pdf)

> >

endobj  
3 0 obj  
\<\<  
/Type /Pages  
/Kids [ 4 0 R 9 0 R 14 0 R 19 0 R 24 0 R 29 0 R 34 0 R 39 0 R  
44 0 R ]  
/Count 9

> >

endobj  
4 0 obj  
\<\<  
/Type /Page  
/Parent 3 0 R  
/MediaBox [0.0000 0.0000 595.0000 842.0000]  
/Contents 5 0 R  
/Resources \<\<  
/XObject \<\<  
/Im1 7 0 R \>\>  
/ProcSet [/ImageB]

> >

endobj  
5 0 obj  
\<\<  
/Length 6 0 R

> >

stream  
q 595.0000 0.0000 0.0000 832.3821 0.0000 4.8089 cm /Im1 Do Q

endstream  
endobj  
6 0 obj  
62  
endobj  
7 0 obj  
\<\<  
/Length 8 0 R  
/Type /XObject  
/Subtype /Image  
/Name /Im1  
/Width 2504  
/Height 3503  
/BitsPerComponent 1  
/ColorSpace /DeviceGray  
/Filter /CCITTFaxDecode /DecodeParms \<\< /K -1 /Columns 2504 /Rows 3503\>\>

> >

stream  
�Y�Q���z�����|��\_��sT��1�d1D���mM�ק��  
�\*r��B�\_��\>7�ȱ�j�Y�N�`|\V'gڝ�%�Z�(��+=��Ы�DF�v7�WC,��r����dh���MZ٭t�U-rܛF?�r��)��@ҕ��x���G�7XMSL��,�jJ������MT��ep��TM� �TT���(Zt�B�,�J�hXw��^�s�>��Ɗh-q��gN�w�Y�b��n�PA�w��YeK�Y��i ��h?���Q���-0$Y��5����q\<KY�5oWOڏ�e�rq�gg�p8���B���9Pd�r����B$@�Ú�u���~��A�%g��9aع `�Qs0�A�3���A��A��k fr��"pM��a4a3 d``0g�#L’�N�[�i�wi�A��i��i��z��SN&��×�w���O(�4M�R���˺��Ne��G�7�~�8rЯT�Z�;˷2D𛧧����P7A�A�|6��&�|eA��i�|e�tN�9i��/֛I�m’K�I�o�O������"��dh� ��I��gWN�zi’ik��}}��������N�ΆjyOܘO4!��%�����U�!����\_�u�Jg�l �z3���!p\>;T��O�T���/�խ%�It�˿w�˵#������ ���S��]�MݢP���-R�K e˴�6҈h����8����-

---

<div class="post-metadata">

**Author:** ![voracityemail](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/v/b77776/32.png) [@voracityemail](http://forum.companieshouse.gov.uk/u/voracityemail)\
**Post date:** [February 24, 2023, 11:40am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/8 "2023-02-24T11:40:16Z")

</div>

Congratulations. That looks like the start of a PDF, does it not?

---

<div class="post-metadata">

**Author:** ![abhijeet.kate](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@abhijeet.kate](http://forum.companieshouse.gov.uk/u/abhijeet.kate)\
**Post date:** [February 28, 2023, 8:46am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/9 "2023-02-28T08:46:56Z")

</div>

Yes, but how do i get actual PDF the question remains same, i am not getting AWS URL in response.

Thanks

---

<div class="post-metadata">

**Author:** ![voracityemail](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/v/b77776/32.png) [@voracityemail](http://forum.companieshouse.gov.uk/u/voracityemail)\
**Post date:** [February 28, 2023, 9:47am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/10 "2023-02-28T09:47:09Z")

</div>

[@abhijeet.kate](https://forum.aws.chdev.org/u/abhijeet.kate) - I’m a bit confused. You said initially “i am able to download document but it just has metadata and file size is just 1 KB.” You then clarified that you called the endpoint:

`https://document-api.company-information.service.gov.uk/document/{the document id number}/content`

… and you posted what looks like a text dump of the (binary) initial content of a PDF.  
To me, that looks like you succeeded.  
What are you saying is the issue?  
That sounds like you are saying “I download a PDF but I am only getting the start of the file e.g. 1KB” - is that correct?  
Did you try to open the downloaded file in a PDF editor and if so what happens?  
Did you accidentally corrupt the output e.g. by printing it to screen then trying to copy-paste into a file to save it? You don’t want to display the bytes of a PDF as text, you need to save them as received then open in a PDF editor!  
Is only part of the content actually being downloaded e.g. the output is truncated - so you get the first part of the PDF file and then it is cut short? Can you check if this is actually your software doing this or is it actually downloading the whole data but only showing you the first part (because you have the system set up to display the output in a text window)?

I would suggest you check your tool / environment (Blue Prism Studio?) to see if there are any settings there which are limiting the size of the download.

As I suggested before if you have issues:  
a) I strongly recommend you follow the process through using some tool where you can do one step at a time (like [curl](https://curl.se/))  
This is so you can:  
i) ensure that this is actually works using the particular URLs you’re using! and  
ii) if there is an issue you can see _exactly_ where it occurs. Curl at least allows you to display extra information about every step (via the verbose flag).  
Requesting the `document/{the document id number}/content` endpoint triggers at least two steps. See my previous posts! However to repeat the first is that Companies House server sends back a redirect (to Amazon Web Server - the address is in the http header). You then download the document from there. (There is nothing to stop AWS also sending a redirect too of course!)  
It looks like your tool is automatically following the redirect. That’s fine - and indeed you seem to be receiving a PDF. That’s why I’m confused as you did not say clearly what the problem is. If you do have problems you may need to stop it doing so (e.g. catch the redirect in the http header)  
If none of the last paragraph makes sense then you may need to learn a bit about the http transport protocol ([Mozilla has some helpful info](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers), or [Wikipedia of course](https://en.wikipedia.org/wiki/HTTP)).

b) If you still cannot make this work again the more information the better.  
I probably will not be able to help but someone may. So if I were you I would list:

- the software / system / environment you are using e.g. “Using Blue prism studio”
- how you’re using it e.g. “I make the request using the e.g. [ResourcePC HTTP interface](https://bpdocs.blueprism.com/bp-6-8/en-us/helpHTTPInterface.htm?Highlight=http), with parameters x y z” (NOTE: do not list your actual API key here). This should include the Companies House / other URIs you’re requesting e.g. “[https://document-api.company-information.service.gov.uk/document/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY/content](https://document-api.company-information.service.gov.uk/document/vqCGbDqGHA8ckavsX1nssaN03JVhwAzF7i0p7fXEcuY/content)” or whatever
- the outcomes e.g. "The program downloads a file ‘foo-bar.pdf’ which is x bytes in size. When I try to open this with e.g. Adobe DC reader I get the error ‘…’ ", or “I see some text in a window (which looks like the start of a PDF file) but is only y bytes long. I tried pasting this into a text package and saving it but …”

Good luck.

---

<div class="post-metadata">

**Author:** ![abhijeet.kate](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@abhijeet.kate](http://forum.companieshouse.gov.uk/u/abhijeet.kate)\
**Post date:** [March 13, 2023, 8:06am UTC](http://forum.companieshouse.gov.uk/t/pdf-downloaded-through-chs-api-not-getting-opened/5919/11 "2023-03-13T08:06:11Z")

</div>

Hey, Thanks for your guidance, now i am able to download PDF file and its getting opened correctly, thank you so much for your support, it helped me a lot.

Thanks
