Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 views
in Technique[技术] by (71.8m points)

php - Streaming PDF into iframe using dataurl - works only in Chrome

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...