Save PDF to disc from blob

Hi all, i have code as this:

    @keyword
    def download_pdf_and_compare_screenshosts(self):
        promise = self.browser.promise_to(
            "Wait For Response",
            "response => response.url().includes('api/projections/outputFile') && response.status() === 200",
            "timeout=20 s")
        self.builtin.sleep(2)
        self.browser.click("xpath=//button[@class='mat-focus-indicator pl-0 mat-button mat-button-base mat-primary ng-star-inserted']")
        res = self.browser.wait_for(promise)
        self.builtin.log(res["body"])
        with open("test.pdf", "wb") as fp:
            fp.write(res["body"])

What this do, is it just catch the response from api call, that contains in body pdf.

Problem is, that I can’t save it. If I save it as this, PDF is blank.
If I try to do bytes(res["body"], encoding="utf-8") it is the same, blank pdf

If I try to do b64decode(res["body"], validate=True) I got error message about non-ascii character.

I’m kind a stuck here… … Any ideas how to resolve this problem?

part of the pdf:

%PDF-1.3
%����
2 0 obj
<</Subtype/Type1/Type/Font/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>
endobj
1 0 obj
<</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix [1 0 0 1 0 0]/FormType 1/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 2 0 R>>>>/BBox[0 0 112.11 42.07]/Length 218>>stream
x���;N1Ds��!$M�M�����D��(o�J�do�~]vkV|�Ϥ�T�g���)$bS�M����U�G
��<ª�B�9N�E�<d�����P�$���FB!$��X'F��h%�J�CI��'m�.��>P\	��Uø).�l:�9cvi��L�2
�z�4�2Hz��������1Ư+������좖ݥM��e}�'U�kY�����V��ܗ�
endstream

Hi Tomas,

  • I’m no expert with this python module, but what do you get if you log the res variable does it contain a key with the filenpath (res["saveAs"])? you might find the file already was created and you simply need to get it’s filename so you can copy/move it or do whatever else you want with it?

  • Perhaps I’m missing something? why not just use the standard browser keywords as shown in the example in the documentation for Promise To Wait For Download?
    If you just need to control the path where the files are downloaded New Browser has a downloadsPath option for controlling that.
    Once the file is downloaded to disk by the browser you’ll have the file path (${file_obj}[saveAs] from the example) and with that you can do what every you need i.e. open it and read the content or pass to to a command line compare tool.

Dave.