Streaming PDF into iframe using dataurl - works only in Chrome

后端 未结 1 1268
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 14:13

The following works in Chrome, but the iframe content is blank in Firefox 15 and IE 9.


  
  
    

t

相关标签:
1条回答
  • 2020-12-21 15:07

    I've just copied your source verbatim, swapping the section where the php outputs the encoded pdf for a string I got back from an online encoder.

    The raw pdf is 141kb, and the encoded string is 194,065 bytes - so quite large as a url..

    It worked just fine in Chrome as is, just as it did for you. Similarly, Opera wouldn't have a bar of it.

    So, I removed the type='application/pdf' attribute from the iframe tag. Now Chrome and Opera will both display the file, while IE9 still refuses.

    I tried changing the src via javascript (just in case), no problem with it through Opera/Chrome, but still no go with IE.

    In the past, I've created pdfs on the fly with php and simply set the src of the iframe to be the php file's url, complete with GET params. This worked just fine with IE6 (and opera, chrome and ff - never tried it on a mobile device, it was over 5 years ago)

    Some old, example code that I hope will help:

    (1) Javascript to insert the iframe

    function  showPdfTt(studentId)
    {
        var url, tgt;
        title = byId("popupTitle");
        title.innerHTML = "Timetable for " + studentId;
        tgt = byId("popupContent");
        url = "pdftimetable.php?";
        url += "id="+studentId;
        url += "&type=Student";
        tgt.innerHTML = "<iframe onload=\"centerElem(byId('box'))\" src='"+url+"' width=\"700px\" height=\"500px\"></iframe>";
    }
    

    Output section of pdfTimetable.php

    $pdfStr = $pdf->output();
    $myFile = fopen("lastRequested.pdf", "wb");  // just here for debugging purposes
    fwrite($myFile, $pdfStr);
    fclose($myFile);
    
    $pdf->ezStream();   // send output to stdout (the browser) - uses fpdf for generation
    exit;
    

    Output section of fpdf

            if(php_sapi_name()!='cli')
            {
                //We send to a browser
                header('Content-Type: application/pdf');
                if(headers_sent())
                    $this->Error('Some data has already been output, can\'t send PDF file');
                header('Content-Length: '.strlen($this->buffer));
                header('Content-Disposition: inline; filename="'.$name.'"');
                header('Cache-Control: private, max-age=0, must-revalidate');
                header('Pragma: public');
                ini_set('zlib.output_compression','0');
            }
            echo $this->buffer;
            break;
    
    0 讨论(0)
提交回复
热议问题