Streaming PDF into iframe using dataurl - works only in Chrome

假如想象 提交于 2019-12-18 09:07:03

问题


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

<html>
  <head></head>
  <body>
    <p>this n that</p>
    <iframe type="application/pdf"
            width="95%"
            height="95%"
            src="data:application/pdf;base64,<?php echo base64_encode(file_get_contents('memlist.pdf'));?>">
      Oops, you have no support for iframes.
    </iframe>
    <p>this n that</p>
  </body>
</html>

The problem seems to be the dataurl. If I change it to simply src="memlist.pdf" then it works everywhere. The PDF size is 75kb.

Is there some (current) browser limitations with dataurls?

(I'm trying to avoid the call back to the server with an http url because of some authentication complications)


回答1:


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;


来源:https://stackoverflow.com/questions/12848616/streaming-pdf-into-iframe-using-dataurl-works-only-in-chrome

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