如何嵌入动态PDF?

时间:2011-07-25 06:52:41

标签: php html pdf-generation embed fpdf

我有一个网站,它使用FPDF(来自fpdf.org)基于传递给PHP脚本的HTML表单动态生成PDF。截至目前,它一切正常,信息通过,FPDF生成PDF。但是,我一直在寻找嵌入选项,并不能让它正常工作。我的页面的其他区域嵌入了PDF,但它们是我生成并保存到服务器的区域。我的问题是,是否可以动态生成PDF并将其直接输出到HTML页面的垃圾区域内的浏览器($pdf->Output();)?现在它只是生成并占用整个窗口,但我想包含其他信息以及PDF,例如说明和什么不是。我尝试通过以下方式将pdf作为字符串输出到变量中:

$output = $pdf->Output('','S');

这确实将信息作为字符串输出到$ output变量中;但是,我不确定我是否能够嵌入它。我尝试指定MIME类型(如application/pdf),但唯一的其他可用属性是src,所以我不确定是否可以在任何地方使用该字符串。我过去曾使用过两种不同的技术来嵌入PDF。标签和谷歌文档查看器,但在与他们玩了一段时间后,我无法让这个工作=(任何人有任何想法?

3 个答案:

答案 0 :(得分:0)

嵌入的PDF基于浏览器功能。 您可以使用框架并在页面的上部或左侧显示说明,并在其余部分显示pdf。

正如您所说,您可以使用Google文档,但它会将整个文档转换为嵌入图像。

答案 1 :(得分:0)

如果您使用的是相同的FPDF,那么您已经拥有了所需的一切!

只需将“我”改为“D”即可强行下载,而不是内联。

从:

function Output($name='', $dest='')
{
    //Output PDF to some destination
    if($this->state<3)
        $this->Close();
    $dest=strtoupper($dest);
    if($dest=='')
    {
        if($name=='')
        {
            $name='doc.pdf';
            $dest='I';
        }
        else
            $dest='F';
    }
    switch($dest)
    {
        case 'I':
            //Send to standard output
            if(ob_get_length())
                $this->Error('Some data has already been output. Can\'t send PDF file');
            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;
        case 'D':
            //Download file
            if(ob_get_length())
                $this->Error('Some data has already been output. Can\'t send PDF file');
            header('Content-Type: application/x-download');
            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: attachment; filename="'.$name.'"');
            header('Cache-Control: private, max-age=0, must-revalidate');
            header('Pragma: public');
            ini_set('zlib.output_compression','0');
            echo $this->buffer;
            break;
        case 'F':
            //Save to local file
            $f=fopen($name,'wb');
            if(!$f)
                $this->Error('Unable to create output file: '.$name);
            fwrite($f,$this->buffer,strlen($this->buffer));
            fclose($f);
            break;
        case 'S':
            //Return as a string
            return $this->buffer;
        default:
            $this->Error('Incorrect output destination: '.$dest);
    }
    return '';
}

答案 2 :(得分:0)

当您将fpdf保存为字符串时,可以使用php将其编码为base64,然后将其传递给您用于将pdf嵌入html文档的任何内容。 PHP:

$output = $pdf->Output('','S');
$output = base64_encode($output);

在你的html文档中:

<embed src="data:application/pdf;base64,<?php echo $output ?>" type='application/pdf'>

我知道这至少可以用于chrome。我没有在其他浏览器中测试它。您可能需要实现其他方法将pdf嵌入到html页面中,以实现跨浏览器支持。我假设你可以从这一点弄清楚这一点。希望这有助于某人。