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
Rani_RRani_R 

Display a pdf obtained in HTTP Response,

Hi,

 

I am calling a servlet from my apex code, i am getting a pdf stream as response, the whole pdf is available in the response.getBody()... But i dont know how to display the pdf now.

 

Here is what i did

 

public void GetDocument()
    {
        
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res;
        
        req.setEndpoint('myservlet?docid='+SelDocId);                           
        req.setMethod('GET');
        req.setTimeOut(30000);
        req.setHeader('Cookie',cookieString);
        res = http.send(req);
        pdfData = res.getBody();
    }

 

My VF Page:

 

<script> 
    function showDocument()
    {
        var htmlstr = '<html></head></head><body>sdfsfsdfsdf<object width="400" height="500" type="application/pdf" data="'+docData.value+'"/></body></html>';
        window.frames[0].document.body.innerHTML = htmlstr;          
    }
</script>

<apex:commandbutton action="{!GetDocument}" value="GetDocument" reRenderer="docpanel"/>

 

 <apex:outputpanel id="docpanel">

         <apex:inputHidden id="docData" value="{!pdfData}"/>

         <script>
                   docData = document.getElementById("{!$Component.docData}");
          </script>

 

        <iframe scrolling="true" id="theIframe" onLoad="showDocument()" type="application/pdf"/>
        <object width="400" height="500" type="application/pdf" data="{!pdfData}" id="pdf_content"/>
        <apex:outputtext value="{!pdfData}" id="outputtemp"/>
    </apex:outputpanel> 

 

when my apex method getDocument gets over, the outputPanel docPanel will get re-rendered, thereby calling my javascript method showDocument() which is in onLoad of my iframe...

 

I am trying to put the document inside iframe or in the object.. but it just stays empty, the pdf document is not getting displayed......

 

But in the outputText "outputtemp" the whole pdf is printed as a string, starting as %pdf- ... .............. <EOF>, this confirms i am getting the document.

 

Please help me in displaying the pdf in iframe or in the object tag.... what am i doing wrong here? if there is any workaround also please let me know.

 

 

Thankyou

Rani.

SteveBowerSteveBower

I'm not thinking hard about your question as I'm not sure this is the best way to do it.  But, this just caught my eye:

 

var htmlstr = '<html></head></head><body>sdfsfsdfsdf<object width="400" height="500" type="application/pdf" data="'+docData.value+'"/></body></html>';

 

vs.

 

var htmlstr = '<html><head></head><body>sdfsfsdfsdf<object width="400" height="500" type="application/pdf" data="'+docData.value+'"/></body></html>';

 

Best, Steve.

Rani_RRani_R

Hi,

 

Sorry it was a typo, i changed it, but still the problem persists

 

:(

alaschgarialaschgari

Any new insights considering this topic?