使用dataurl将PDF流式传输到iframe中 - 仅适用于Chrome [英] Streaming PDF into iframe using dataurl - works only in Chrome

查看:1653
本文介绍了使用dataurl将PDF流式传输到iframe中 - 仅适用于Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下在Chrome中有效,但Firefox 15和IE 9中的iframe内容是空白的。

 < html> 
< head>< / head>
< body>
< p>这n表示< / p>
< iframe type =application / pdf
width =95%
height =95%
src =data:application / pdf; base64,< ;?php echo base64_encode(file_get_contents('memlist.pdf'));?>>
糟糕,您不支持iframe。
< / iframe>
< p>这n表示< / p>
< / body>
< / html>

问题似乎是dataurl。如果我将它改为src =memlist.pdf,那么它可以在任何地方使用。 PDF格式的大小是75kb。

是否有一些(当前)浏览器的数据限制?



m试图避免由于一些身份验证问题而使用http url回拨给服务器)

解决方案

我刚刚复制了你的源代码是逐字的,交换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设置为php文件的URL,完整的GET参数。这对IE6(以及opera,chrome和ff--从未在移动设备上尝试过,它已经超过5年前)工作得很好。



一些旧的示例代码我希望能够帮助:
$ b

(1)使用Javascript插入iframe

  function showPdfTt(studentId)
{
var url,tgt;
title = byId(popupTitle);
title.innerHTML =时间表为+ 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); //只是为了调试目的
fwrite($ myFile,$ pdfStr);
fclose($ myFile);

$ pdf-> ezStream(); //将输出发送到stdout(浏览器) - 使用fpdf生成
exit;

fpdf的输出部分

  if(php_sapi_name()!='cli')
{
//我们发送到浏览器
header('Content-类型:application / pdf');
if(headers_sent())
$ this->错误('有些数据已经输出,不能发送PDF文件');
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;
休息;


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)

解决方案

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;

这篇关于使用dataurl将PDF流式传输到iframe中 - 仅适用于Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆