Adding for others to understand how to identify the correct format of the response received from the api. Specifically when you expect a pdf file from the api. If not done correctly you will se blank pdf pages.
pasting my answer below incase link breaks
HttpResponse response = h.send(req);
Its important to understand why blob.valueof(response.getBody()) did not work and response.getbodyasblob() works.
When using this
response.getBody() is getting the body from the response in String format. the PDF file gets corrupted here itself. We the use blob.valueof() to convert the String to Blob for saving as contentversion.attachment.
this methods retrieves the body from response directly as Blob. So no conversion takes place and pdf file is intact.
Why EncodingUtil.Base64Decode(response.getBody()) does not work here is because the response from the API is not a Base64 string. If you actually check the response you will find strange characters. Base64 only supports these characters:
Uppercase letters (indices 0-25): ABCDEFGHIJKLMNOPQRSTUVWXYZ
Lowercase letters (indices 26-51): abcdefghijklmnopqrstuvwxyz
Digits (indices 52-61): 0123456789
Special symbols (indices 62-63): +/
So trying to using EncodingUtil.Base64Decode(response.getBody()) with our response will give error of invalid characters. If you dig deeper and check the response received using System.debug(response.getBody()) you will realize that it is actually a PDF file format (the PDF specification for creating pdf files here (https://resources.infosecinstitute.com/topic/pdf-file-format-basic-structure/)). So we do not have to decode or uncompress the response in any way. We have to just retrieve the file as it is (as a blob) and save it to salesforce.
Example of (response.getBody()) you will see for these api responses which return PDF files.
2 0 obj
<</ColorSpace/DeviceRGB/Subtype/Image/Height 200/Filter/FlateDecode/Type/XObject/DecodeParms<</Columns 420/Colors 3/Predictor 15/BitsPerComponent 8>>/Width 420/Length 6827/BitsPerComponent 8>>stream