2

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)

4

1 に答える 1

1

ソースをそのままコピーし、php がエンコードされた pdf を出力するセクションを、オンライン エンコーダーから取得した文字列に置き換えました。

生の pdf は 141kb で、エンコードされた文字列は 194,065 バイトで、URL としてはかなり大きい..

あなたと同じように、Chromeでも問題なく動作しました。同様に、Opera にはバーがありません。

そこで、iframe タグから type='application/pdf' 属性を削除しました。Chrome と Opera はどちらもファイルを表示しますが、IE9 は依然として拒否します。

念のため、javascript を使用して src を変更しようとしましたが、Opera/Chrome では問題ありませんが、IE では問題ありません。

過去に、私は php を使用してその場で pdf を作成し、iframe の src を GET パラメーターを使用して php ファイルの URL に設定するだけでした。これはIE6で問題なく動作しました(およびopera、chrome、およびff-モバイルデバイスで試したことはありません。5年以上前です)

私が役立つことを願っているいくつかの古いサンプルコード:

(1) iframeを挿入するJavascript

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>";
}

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;

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;
于 2012-10-11T23:28:49.747 に答える