function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
code developercode developer 

Display gzipped pdf attachment from Http response


Iam sending a request for a pdf document through rest api call.The response I got is of following format

  1. Status Code: 200 OK
  2. Content-Disposition: attachment; filename="xxxxx"
  3. Content-Encoding: gzip
  4. Content-Type: application/pdf
  5. Date: Wed, 16 May 2012 11:02:43 GMT
  6. Server: xxxxxxxxxxxx
  7. Transfer-Encoding: chunked
  8. Vary: Accept-Encoding

But how do I display the pdf response?
It is showing that the content encoding is gzip.How do I unzip it?
Below is the code for the request I have sent.


<apex:page controller="TestPdf" renderAs="pdf">


public with sharing class TestPdf
    public String getPdf()
        string  Url= 'url';
        Http h = new Http();
        HttpResponse res;
        HttpRequest request= new HttpRequest();
        request.setHeader('username', 'username');
        request.setHeader('password', 'password');
        request.setHeader('Content-Type', 'application/pdf');
        return res.getBody();

Please help me out.Thank you



The REST classes automatically unzip the file for you (and it also unchunks it for you). The resultant value is given to you in a binary format that is already a PDF. Therefore, using renderAs="PDF" will attempt to render the PDF as a PDF using the native HTML-to-PDF translator, which as you might now realize, won't work.


What you need is to specify a contentType on the page of "application/pdf", do not use a renderAs attribute, and somehow be able to output the binary string to the page. This is the primary sticking point that several developers have experienced in the past (and of which I sadly have no "straight" answer to)-- you can't choose to output binary data in any meaningful fashion in Visualforce.


There's simply no way to output a pre-rendered anything (zip file, pdf file, image, etc) to the output stream in a binary format, or by specifying a content transfer encoding that the browser would accept and decode correctly. The only "legal" way in the platform to even output raw binary data today is Base64 encoding, which would work if you could specify a Content-Transfer-Encoding header, which we can't (not that I've seen).


The short-term solution that may work, is as follows:


1) Retrieve your content from the server.

2) Save the file to a Document or an Attachment.

3) Return a PageReference to the Document or Attachment download link.


I've never actually tried this so I'm not 100% confident it would work this way; I don't have the time to write a full-scale mockup required to test this. The actual limitation would come down to if the REST classes can provide a binary response back (which I don't think they can at the moment). If your server can pre-encode to base64, then the response text would be binary-encoded, which you could then directly save into the aforementioned document or attachment; the EncodingUtil class would help you out here, as you could decode the base64 back into a native Blob format.

code developercode developer

But the data iam getting is in encoded format.It is not readable.please give me a suggestion


Any new insights considering this topic?