Streaming PDF into iframe using dataurl - works only in Chrome

允我心安 提交于 2019-11-29 15:37:12

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!